Spring08Shakar
Intro to PComp Home
Weekly Topics & Assignments
Wednesday Class Dates

Links:

PmWiki

edit SideBar

Button Click Counting Code From Class


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

     delay(10);  // delay for debouncing of button
  }

  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

    delay(10);   // delay for debouncing of button
  }

}


  Edit | View | History | Print | Recent Changes | Search Page last modified on February 27, 2008, at 11:50 PM