Lesson 4
In this lesson we will start to write some programs on the Ardunio. They are not typical Ardunio programs in that we will be using the serial console to send input from a keyboard to the Ardunio, this is a very rare use-case for the ardunio, but it is the easiest way to enter information for learning some basic programming algorithms. These will be the algorithms from Lesson 3 and I will expect you to write the code yourself for them. This page will give you the correct syntax for everything you need.
First you need to know how to read input from the Serial Monitor
In order to use the serial monitor on the Ardunio you need to do two things. The first is to initalize the serial port in the Setup block:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // open seral port and set data rate
}
What this code does is turns on the Serial port and tells it that you want to communicate at a rate of 9600 bytes per second.
Next within your loop block you need set and wait until the serial port connects:
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available() == 0) {
//waits forever unless a serial cable is connected
}
// All your other code goes here
}
Since all of our examples are using integers we will use only one of the Serial read functions, the Serial.parseInt, This one lookwis at a line that you type and reads until it finds a character that is not a number. If you enter more than one number it will read each number and continue the loop. It must always be assigned to a variable and variables must always have a type. In this case integer.
int n = Serial.parseInt();
Variables only need to be declared once and can be reused so you could have done
int n=0;
n = Serial.parseInt();
You will need to print output to the Serial Monitor
We will use two command for this print and println. print prints the values you pass to it without moving to the next line and println causes us to move to the next line:
Serial.print("You entered: ");
Serial.print(n);
You will use some math functions
| + | Adds to values |
| – | Subtracts to values |
| ++ | Adds one to the variable example n++; |
| — | Subtracts one from the variable n–; |
| * | Multiplies two numbers |
| % | Module operation gives you the remainder when you divide the first number by the second. |
| / | Divides two numbers |
| sqrt() | Finds the square root of a number |
| ^ | Raises a number to a power. Think exponent. |
| () change order of operations | Remember things in () happen first. |
You will use some comparison operators.
| == | Checks if two variables are equal |
| != | Checks if they are not equal |
| > | Greater than check |
| < | Less than check |
| >= | Great than or equal to |
| <= | Less than or equal to |
You will use IF THEN ELSE statements
Notice that in c++ which is the language of the Ardunio we do not actually type the “THEN” of these statements.
if (m>6) {
Serial.println(" ***ERROR you entered a number not on a dice*** ");
}
else
{
Serial.println(m);
}
The example above checks to see if you entered a number bigger than 6 and reports the issue.
You will use SWITCH CASE statements
This is kind of a shortcut from using a bunch of if statements, you could do the project that needs this without it. In ardunio the switch only works on integer values. Can you figure out what this one does?
switch (n) {
case 1:
m=6;
break;
case 2:
m=5;
break;
case 3:
m=4;
break;
case 4:
m=3;
break;
case 5:
m=2;
break;
case 6:
m=1;
break;
default:
m=7;
break;
}
Serial Monitor Settings on your Computer
The last thing you need to know is that you have to setup your serial monitor correctly or you will get strange behavior on these examples. You have to tell the monitor you don’t want an endline character.
You will want to select Tools –> Serial Monitor on the Ardunio IDE
This will open a window at the bottom have of the application screen where you will see messages from the ardunio.

Notice the dropdown boxes on the right hand corner of the bottom half of the screen you need to set them to “No Line Ending” and “9600 baud” You can change the baud rate to a higher or lower value, but then you have to change it in your code and it’s not a great idea for these early projects.
Homework
I would like you to try and write the code for running the homework examples from Lesson 3, you can find my answers on the previous page.
