RobotC:
while(SensorValue(touchSensor) == 0) // while the Touch Sensor is inactive (hasn't been pressed):
{
// DO NOTHING (wait for press)
}
and thought something similar should be possible in Arduino. Here is what we worked out:
while(digitalRead(buttonPin) == HIGH) {
//program will sit here until button is pressed (LOW)
}
We turned that into a function and added a little debounce wait time and had ourselves a very nice waitForPress() function to call in our programs. The button is HIGH, or not pressed, by default, which we achieve by setting the pinMode to INPUT_PULLUP in setup, using the Arduino's internal pullup resistor. Here is what the function looks like, with an added delay to provide a debounce:
void waitForPress() {
while(digitalRead(buttonPin) == HIGH) {
//program will sit here until button is pressed (LOW)
}
delay(1000); //delay for button debounce
}
Now students could call waitForPress() in setup to run a calibration routine in setup, like
Serial.println("Turn lights ON and press the button.");
waitForPress();
recordMaxLight();
Serial.println("Turn lights OFF and press the button.");
waitForPress();
recordMinLight();
calibrateLightSensor();
with the function calls defined as:
void recordMaxLight() {
maxLightVal = analogRead(ldrPin); //ldrPin is light dependent resistor
}
void recordMinLight() {
minLightVal = analogRead(ldrPin);
}
int calibrateLightSensor() {
ldrThreshhold = (maxLightVal + minLightVal) / 2;
Serial.print("calibrated threshhold: ");
Serial.println(ldrThreshhold);
return ldrThreshhold;
}
1 comment :
Great readinng your post
Post a Comment