Posted by Joe at May 19th, 2008

It’s only been, what, 9 weeks since I finished the Blink project for Physical computing? *sighs* I didn’t do myself any favors this Spring quarter at RIT. Seriously.

Anyway, as a “Hello, world!” with the Arduino, our task was to modify the basic LED Blink program to do something somewhat more interesting. For mine, I used the volume slider from an old walkman, and a bend sensor to modify the blinking pattern of a series of LEDs. The volume slider controlled how many LEDs were lit, and the bend sensor affected how quickly the blink occurred.

(could’ve sworn that image was around here somewheres…)

Blink Code

//PROJECT 1 - BLINK
//Physical Computing
//Joe Pietruch
//———————————–
// This program should show a line of LEDs
// The number of LEDs lit in the line will
// depend upon the position of the slider.
//
// The LEDs will blink at a rate controlled
// by the bend sensor.
//———————————–

int sliderPin = 5; //the analog in pin for the slider
int bendyPin = 2; //the analog in pin for the bend-sensor
int val = 0; //a starting value for the slider
int count = 0; //a starting value for the counter

void setup()                    // run once, when the sketch starts
{
pinMode(2, OUTPUT);      // sets a lot of digital pins to output
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);

//Serial.begin(9600); //maybe begin the serializer
}

void loop()                     // run over and over again
{
count = count + 1; //incrememnt count
allOff(); //turn off all LEDs
sample(); //sample the slider (which will turn some LEDs back on)
if(count > analogRead(2)*4){ //if count is greater than the bendy’s sample
allOff(); //turn everything off
count = 0; //reset the counter
delay(100); //and wait a tenth of a second
}
}

void sample(){
val = analogRead(5); //take a reading
if(val > 0){ //if the value is greater than zero
digitalWrite(9,HIGH); //turn on the lowest LED
} if (val > 100) {// ” 100
digitalWrite(8,HIGH); //second LED
} if (val > 200){// ” 200
digitalWrite(7,HIGH); //third LED
} if (val > 300){// ” 300
digitalWrite(6,HIGH); //fourth LED
} if (val > 400){// ” 400
digitalWrite(5,HIGH); //fifth LED
} if (val > 500){// ” 500
digitalWrite(4,HIGH); //sixth LED
} if (val > 600){// ” 600
digitalWrite(3,HIGH); //seventh LED
} if (val > 650){// ” 650
digitalWrite(2,HIGH); //eighth LED
}
}

void allOff(){
digitalWrite(2,LOW);//Turn off all the LEDs!
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
}