« that's the end of that | Home | bike shop: good and bad »
May 14, 2008
discrete circuits are for lusers who don't know C
my arduino arrived yesterday. i dusted off my brain a little (since it's been 2 years since i've done any C, well, not really, i did one small bugfix to my C code a couple months ago and bungled it) and whipped up an LED flasher with teh arduino.
i've been banging my head against the desk trying to flash an LED with 2 transistors and a capacitor. arduino makes it a little easier. okay, a lot easier. flashing LED was too simple. I made mine talk in morse code.
following is the embarrassingly brittle source code (oh, how nice it was to use ?: again!):
#include
int ledPin = 9; // LED connected to digital pin 9
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void ditdit(char *code)
{
int dot = 50 ;
int dash = 300 ;
int pause = 300 ;
for (; *code; ++code)
{
int the_delay = *code == '.' ? dot : dash ;
digitalWrite(ledPin, HIGH) ;
delay(the_delay) ;
digitalWrite(ledPin, LOW) ;
delay(pause) ;
}
delay(pause*2) ;
}
void morse(char *message)
{
char *codes[] = {
".-", "-...", "-.-.", "-..",
".", "..-.", "--.", "....",
"..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.",
"--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-",
"-.--", "--.." } ;
for (; *message; ++message)
{
char letter = tolower(*message) ;
// here we should verify that the letter is in our table and skip it
// if it's not.
char *code = codes[letter - 'a'] ;
ditdit(code) ;
}
}
void loop() // run over and over again
{
morse("SoS") ;
delay(1000) ;
morse("HelpImTrappedInAnArduino") ;
delay(5000) ;
}
hey, teh internets hosed my indentation.
good thing this is C and not python!
wait, no, that's a bad thing.