Wednesday, March 17, 2010

Read LDR after Button Press (Task 38,39)

This code waits for a button to be pressed, then reads a value from an LDR and out puts it over serial.
Code:
int reading;
int push;

void setup() {
Serial.begin(9600);
pinMode(2,INPUT);
count = 0;
}

void loop() {
push = digitalRead(2);
if(push == HIGH){
delay(500); //This avoids multiple values being read per push
reading = analogRead(0);
Serial.println(reading);
}
}
Layout:


You can customize the output to make it look better and provide a count to track readings:
int reading;
int push;
int count;

void setup() {
Serial.begin(9600);
pinMode(2,INPUT);
count = 0;
}

void loop() {
push = digitalRead(2);
if(push == HIGH){
count ++;
delay(500);
reading = analogRead(0);
Serial.print("Reading ");
Serial.print(count);
Serial.print(" is ");
Serial.println(reading);
}
}

No comments:

Post a Comment