//hold down a button to make the LED blink //count the number of blinks //release the button and send the number of blinks serialy to the computer int led = 13; //make a variable to hold the pin number the LED is connected to int button = 7; //make a variable to hold the pin number the button is connected to int count = 0; //make a variable to hold the number of LED blinks int buttonState = 0; //make a variable to keep track of the button state, 1 pressed, 0 released void setup(){ pinMode(button, INPUT);//set the button pin to input pinMode(led, OUTPUT);//set the led pin to output Serial.begin(9600);//initalize serial communication at 9600baud startBlink(); //call the start blink function to blink an led at the begining of the program } void loop(){ if(digitalRead(button)==HIGH){//read the button pin, if the button is pressed and the pin is HIGH, then... buttonState = 1;//keep track of the button state, 1 = presses digitalWrite(led, HIGH);// blink the LED by bringing it high delay(250);//wait 250ms digitalWrite(led, LOW);//bring the LED low delay(250);//wait 250ms count++;//increment the count by 1, ++ means add one to the variable } if((digitalRead(button)==LOW) && (buttonState == 1)){//read the button pin. if the button pin is low and the the last time we read the button the state was 1 then... && is how we say and in a condional statement Serial.print("the LED has blinked ");//send the begining of a message out the serial port Serial.print(count);//send the LED count out the serial port Serial.print(" times. \n");//send the end of the message out the serial port followed by a new line, \n buttonState = 0;//reset the button state to 0 count = 0;//reset the led blink count to 0 } } //start blink function void startBlink(){ pinMode(led, OUTPUT); for(int i = 0; i < 4; i++){ digitalWrite(led, HIGH); delay(250); digitalWrite(led, LOW); delay(250); } }