Sometimes you just can't figure out why your program doesn't work. In Revolution you can use the message window to check your variables, in Flash, the output window. With Arduino, you need to use the Serial Monitor.
Serial means that data is sent or received one bit at a time.
Serial communication is the most common form of communication between electronic devices.
Communicating serially involves sending a series of digital pulses back and forth between devices
at a mutually agreed upon rate.
There is a Serial
built-in class that comes with
Arduino.
Serial.print(20,BYTE); //Serial is the class and print is a function of that class.
To set up serial communication:
- Plug the board into the computer
- Open the Arduino application
- From the Tools menu select
Serial Port and select the port.
- Create a new program (+N). Paste in the following:
/*Arduino Example #3- Hit The Lights 2
Purpose: To make a specific number of user-controlled LEDs blink
by using switches or buttons.
*/
void setup(){
Serial.begin(9600); //open up a serial port at 9600 bits per second
}
void loop(){
Serial.println("hello");
Serial.print("hello\n"); // \is an escape sequence used to differentiate a
//character from that in the lanage
delay(500); //slows it down
}
- Compile the program
- Upload the program

- Open Serial Monitor
Another Serial Exercise:
-
Wire up the board to run this code:
int i=0;
void setup(){
pinMode(2,INPUT);
Serial.begin(9600);
}
void loop(){
if(digitalRead(2)){
i++;
Serial.print("the counter=\n");
Serial.print(i);
Serial.print("\n");
}
}
- Run the program
This code is not quite right. Why?
Add another variable that will keep track of the state (is the button being pressed -1 or not- 0).
In the
loop() check if:
- The button is being pressed
- The state of the button is 0
To test for two things use && inside the the parentheses of the conditional.
Also test if
- The button is not being pressed
- The state of the button is 1
Compile and run the code again.