« altered photos | Main | good morning »

analog in - digital out

Here's a varation of the analog lab. The basic idea is to convert analog input to digital output. I hooked up a flex sensor and a photocell both as analog input, and combine their values to determine which LED's to light up.

photocell on the left, flex sensor at the right.
analog3.JPG

rows of LEDs.....
analog4.JPG

here's the code that runs it

#define OFFSET 9 // where to start on digital out

// analog
int flexPin = 0;
int flexValue = 0;
int photoPin = 1;
int photoValue = 0;

// digital outs
int one = 9;
int two = 10;
int three = 11;
int four = 12;
int five = 13;

int index = 0;


void lightLEDs ( int to_which ) {

int ii;
for ( ii = 0; ii < to_which; ii++ ) {
digitalWrite(ii + OFFSET,HIGH);
}

}

void setup() {

Serial.begin(9600);

// digital output
pinMode(one,OUTPUT);
pinMode(two,OUTPUT);
pinMode(three,OUTPUT);
pinMode(four,OUTPUT);
pinMode(five,OUTPUT);
}

void loop() {
flexValue = analogRead(flexPin); // read the flex value
index = ( flexValue - 500 ) / 100; // convert

photoValue = analogRead(photoPin); // read the photo value
index = index + ( photoValue - 200 ) / 100; // convert yet again

// all off
digitalWrite(one,LOW);
digitalWrite(two,LOW);
digitalWrite(three,LOW);
digitalWrite(four,LOW);
digitalWrite(five,LOW);

// see if we want to light something up
if (index > 0 && index <= 5 )
lightLEDs(index);
else if ( index > 5 ) {
lightLEDs(5); // light them all
}

/* debug code
Serial.print("index value ");
Serial.println(index); */

delay(10); // halt for next loop
}