Recent Changes - Search:

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

Links:

Final Projects


Midterm Projects

PmWiki

edit SideBar

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
  }

}

Edit - History - Print - Recent Changes - Search
Page last modified on October 06, 2006, at 01:00 AM