Thursday, May 28, 2009

Analog input lab


After doing the lab using the potentiometer, I tried hooking up a flex sensor to the circuit using the same code, to see if I could use the flex input to dim and brighten the LED. Testing the flex senor, I found the input that I read from it to be very small in range, from about 1017 to 1023, with occaisional very small outliers like 10 or 107. Because of the tiny range, bending the flex sensor made no visible difference in the output of the LED. I decided to try and just make the flex sensor turn the LED on and off. Because of the erratic nature of the flex sensor readings, I decided to use a for loop to generate an average of many readings in each program loop. The for loop takes 30 readings and finds the mean average, and the program then either powers the LED or not, depending on weather or not there is a discernable bend in the sensor.
Here's the code I ended up with:
int flexPin = 0;
int flexValue = 0;
int led = 9;
int flexSum = 0;
int flexAverage = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
flexSum = 0;
for(int j = 0; j < flexvalue =" analogRead(flexPin);" flexsum =" flexSum" flexaverage =" flexSum/30;">

No comments:

Post a Comment