Let's learn to read data directly from the Arduino as text. We can see how it "talks"!
Open up File > Examples > Basics > DigitalReadSerial, and set up your button the way we had it for the Button tutorial. Upload the sketch to your board.
CODE
Things should be looking pretty familiar by now. The new stuff has to do with the serial monitor:
In setup(), look for Serial.begin(9600);
which tells the Arduino to start communicating via text, at 9600 baud. The baud rate controls how much information is sent per second.
And in the loop(), you'll see the command Serial.println(buttonState);
– this tells the Arduino to take the current value of the button and print it to the monitor as a single line of text.
The monitor itself can be opened with the button at the top-right of the IDE:
Open it up, and you'll start to see a column of 0s. Push your button, and they will turn into 1s! Magic!
If you don't see the pretty 1s and 0s, check the settings at the bottom of the window:
- Autoscroll should be selected
- Newline should be selected
- 9600 baud should be selected (the rate at which the monitor expects to receive data)
What else can we do?
- Get real-time readings from a sensor
- Embed debug messages in code: Print to the serial monitor at crucial points in execution, to help with troubleshooting
Interact with this circuit in real-time, using Javascript! Arduino + NodeBots
See the whole series of Hackster 101 tutorials on Hackster and YouTube
Comments