Paul Makepeace ;-)

March 2, 2008

My first Arduino project

Posted in: Gadgets, Software, Tech, What am I up to

I've spent a very pleasant Sunday afternoon playing with an Arduino microcontroller board. After a trip to Maplin I plugged together a little array of assorted LEDs from their "lucky bags" and got hacking.


The Arduino board is fabulous: for an amazingly cheap €22 you get a USB-programmable 16Kb microcontroller with a bunch of digital and analog input and outputs, and the outputs are capable of driving useful things like LEDs. The software's not just free but open source too--and it's decent, with excellent documentation. I'd say from downloading the software and getting my first "hello world" program loaded was about five minutes. It Just Worked™. This might not sound like a big deal, but interfacing to computers has generally been expensive, esoteric, fiddly, and error-prone. The Arduino board is none of these.

The tutorials are great and introduce the important concepts with example circuits and code. In fact, the set of example code is all accessible directly from the Arduino app's main menu, which is a nice touch. (I.e. you don't have to dig around trying to open a file buried on your hard drive.)

So anyway here it is. Eleven LEDs connected to digital pins 0-10, and a 22k linear pot (variable resistor) feeding analog pin 5. The LEDs "chase" up and down with a speed taken from the pot position.

It was fairly painless, at least after an initial scare where programs wouldn't upload,

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_recv(): programmer is not responding

I read all sorts of scary solutions online where folks were reprogramming their bootloaders and other horrors. One Arduino hacker mentioned running the board off an external power supply (rather than rely on USB) during programming as the current draw is apparently higher. This was enough for me to try pull the LED's ground pin out so the lights turned off: Bingo! No problem programming. It became a bit annoying pulling the ground lead out every program: next trip to Maplin I get a switch...

The other issue I had was remembering how C does casting. My function to scale the analog input into a minimum and maximum delay was being simultaneously confused by my code not casting up to a float early enough, and a persistently dodgy connection with the pot.

(The fifth pin/LED didn't seem to light very strongly. Tried different LEDs and lower resistance. Didn't fix it or figure out why it was dim; I'll try a different board soon.)

So what led me to Arduino? The brand new Science Gallery at Trinity College in Dublin is running a programme to introduce high school kids who wouldn't otherwise have had the educational opportunity to do so to learn about electronics. I'm helping out there on Wednesday afternoons, which so far has mostly involved teaching them how to solder, and debugging electronics problems. Where possible I have them learn from my mistakes: my room in my parents' house, for example, was so badly burnt from soldering iron accidents and riddled with little lumps of solder they had to replace it after I left... (I suppose one useful legacy I left was installing about two dozen plug sockets :-))

So far the kids have created some very cool LED pictures each with a fierce amount of soldering and now are ready to hook them up to Arduinos. Hence the need to stay one step ahead of the younger generation ;-)


If you're curious, here's the code for the LED chasing. It's C with some special built-in functions like setup(), delay(), analogRead(), etc.
#define LED_COUNT 11
#define DELAY_MIN 10
#define DELAY_MAX 400
#define DELAY_STEP 50
#define POT_PIN 5
#define INTERNAL_LED_PIN 13
void setup()
{
  for (int i = 0; i < LED_COUNT; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
  pinMode(POT_PIN, INPUT);
  pinMode(INTERNAL_LED_PIN, OUTPUT);
}
int ScaleDelay(int in)
/* Calculates a delay between DELAY_{MIN,MAX} from an analogRead.
 */
{
  float d = (DELAY_MAX - DELAY_MIN) * (float)in;
  d = d / 1024 + DELAY_MIN;
  return (int)d;
}
void LightLED(int led_pin, int last)
/* Reads a delay, lights an LED on led_pin; turns off the last one a short time later.
 */
{  
  int d = ScaleDelay(analogRead(POT_PIN));
  digitalWrite(led_pin, HIGH);
  delay(d / 10);
  digitalWrite(last, LOW);
  delay(d);
}
#define TAIL_LENGTH 3
int tail[TAIL_LENGTH];
int tail_pos = 0;
int LastLED(int current)
/* Maintains a list of lit LEDs. Returns the very last one.
 */
{
  tail[tail_pos] = current;
  tail_pos = (tail_pos + 1) % TAIL_LENGTH;
  return tail[tail_pos];
}
void loop()
{
  int last;
  for (int i = 0; i < LED_COUNT; ++i) {
    last = LastLED(i);
    LightLED(i, last);
  }
  digitalWrite(INTERNAL_LED_PIN, HIGH);
  for (int i = LED_COUNT-1; i >= 0; --i) {
    last = LastLED(i);
    LightLED(i, last);
  }
  digitalWrite(INTERNAL_LED_PIN, LOW);
}
Posted by Paul Makepeace at March 2, 2008 21:03 | TrackBack
Comments

Thanks for the link here, some interesting stuff! (The green code is super-hard to read on the white background though)

Posted by: Phil Wilson at May 5, 2008 18:43
Post a comment









Remember personal info?