State change detection
A pushbutton lets the user send a signal to the system, but when the button is released the signal is lost. Usually we need to remember that the button was pressed. We save the information in a state variable. This is described in a little more detail in the original tutorial on the Arduino web site.
The code for this example is shown in the box below. You can load it into
your Arduino IDE from the File menu: File → Examples → 02.Digital →
StateChangeDetection
The aim is to count how many times the button has been pressed, and to turn on the LED if the number is divisible by four (0, 4, 8, ...).
This example also demonstrates the use of the serial monitor to get output from your sketch.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
You can use the same hardware configuration as for the button examples if you still have it made up. The layout shown below includes an external LED.
If you upload and run the sketch, the LED should light up because the button has not yet been pressed and zero is divisible by four (0 / 4 = 0). If you press the button, the LED will go out, and if you press it three more times it will come back on. That way you can see that the sketch is working correctly. Sometimes, however, you need to get more information about how your sketch is working. The serial monitor is a good way to do that.
What does bouncing mean?
The loop()
function repeats so quickly
that it may read the same button press more than once before you remove
your finger. This is known as bouncing.
A small delay is usually enough to prevent this from happening.
The serial monitor
The Arduino board can send and receive data using a serial communications connection. The serial monitor lets you see data that is being sent by the Arduino, and you can also use it to send data to the Arduino. In this example, we will just look at message generated from the sketch.
You can open the serial monitor using the button in the top right-hand side of the IDE window:
The example sketch contains three commands that allow it to send serial data. They are summarised below:
Line | Command | Description |
---|---|---|
16 | Serial.begin(9600); | Start serial communication at 9600 bits per second (bps or baud) |
31 | Serial.println( message ); | Prints message in the monitor window and moves to the next line |
32 | Serial.print( message ); | Prints message in the monitor window but does not move to the next line |
A baud rate of 9600 bps is the default speed; you should not need to change this.
You can use the two forms of output command to construct a wide range of useful messages.
All of the serial communication commands are containing in a library called Serial. The commands used here are prefixed with the name of the library.
Run the example again with the serial monitor open and watch what happens when you press the button.
Binary
You can use the state variable concept to represent a number in binary format. The layout below uses two LEDS to represent the binary numbers 0 - 3.
Copy and upload the code below, and see what happens when you press the button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|