discrete circuits are for lusers who don't know C

| 1 Comment

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) ;
}

1 Comment

hey, teh internets hosed my indentation.

good thing this is C and not python!

wait, no, that's a bad thing.

Leave a comment

About this Entry

This page contains a single entry by sainttoad published on May 14, 2008 9:22 AM.

that's the end of that was the previous entry in this blog.

bike shop: good and bad is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.32-en