// C4_Pushbutton_LED_state_change // Changes the LED state each time the // pushbutton is pressed. // Constants used to set pin numbers const int buttonPin = 12; // the number of the pushbutton pin const int ledPin = 11; // the number of the LED pin // variables int buttonState = 0; // variable the pushbutton status int ledState = 0; // variable the LED 0 is off, 1 is on void setup() { // set the buttonPin mode to INPUT pinMode(buttonPin, INPUT); // set the ledPin mode to OUTPUT pinMode(ledPin, OUTPUT); } void loop(){ // get the state of the pushbutton buttonState = digitalRead(buttonPin); // set the LED state based on the pushbutton // state if(buttonState) { if(ledState) { ledState = 0; } else ledState = 1; } delay(500); // Give time to release the button
// is the button pressed? // if it is, the buttonState is HIGH: if (ledState == 1) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
this. The microcontroller will be running through the
loop() function, and each time through it will need to
decide to turn the LED on or off as with the earlier
programs; in them, however, it checked the state of the
pushbutton to make the on/off decision.
What we want is a variable that the loop() can
examine to see if the LED should be on or off. For
instance, we may create a global variable ledState and set
it to zero. Then, when we are in the loop() function, we
will check the pushbutton. If it is pressed, we will check
the ledState; if that state is zero we will change it to one,
and if it is one we will change it to zero. This action is
called toggling; we are said to toggle the state.
Lab 4: Testing Your Reaction Time.
We will also use a delay so we can get our finger off
the pushbutton before the loop() goes around and checks
it again.
Parts required:
1 Arduino
1 Arduino proto shield
Check off when complete:
; Make sure the power is off before building the circuit.
; Build the circuit shown in Figures 16 and 17.
Check off when complete:
; Plug the USB cable into the Arduino.
; Make sure the power is off before building the circuit.
; Build the circuit shown in Figures 16 and 17.
; Apply power to the circuit.
; Plug the USB cable into the Arduino.
; Open the Arduino IDE and load the
; Apply power to the circuit.
C4_Pushbutton_LED_state_change program.
; Open the Arduino IDE and load the C4_Time_Tester
; Verify and upload the program to your Arduino.
program.
// C4_Reaction_Time_Tester // Reports over the serial port how many milli // seconds it takes you to remove your finger // from the pushbutton after the LED turns on. // Constants used to set pin numbers const int buttonPin = 12; // the number of the pushbutton pin // variables int buttonState = 0; // pushbutton status variable const int ledPin = 11; // the number of the LED pin int reactionTime = 0; // reaction time variable unsigned long startTime = 0; // unsigned long endTime = 0; // long randNumber; void setup() { // initialize Serial communications Serial.begin(57600); Serial.println(“Test your reaction time.”); Serial.println(“Hold down button and release when LED comes on.”); // set the buttonPin mode to INPUT pinMode(buttonPin, INPUT); // set the ledPin mode to OUTPUT pinMode(ledPin, OUTPUT); } void loop(){ // get the state of the pushbutton buttonState = digitalRead(buttonPin);
April 2014 65