|
Main Links:
Final Projects
|
Main /
ButtonClickCountingCodeFromClass
int count = 0;
boolean buttonAlreadyPressed = false; // "flag" variable
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
int count = 0;
}
void loop() {
//only count if the button is pressed AND the flag is false
if (digitalRead(2)==1 && !buttonAlreadyPressed) {
count = count + 1; // do the count (add one)
Serial.println(count,DEC); // send count value:
buttonAlreadyPressed = true; //set flag true so that it doesn't
}
if (digitalRead(2)==0) { // if the button is un-pressed
buttonAlreadyPressed = false; // set the flag to false so it will
// be able to count on the next press
}
}
|