Arduino Tutorial Common Syntax
Arduino Tutorial Common Syntax
NOTE: All Loops (for, while, do/while), if/then, if/else syntax is the same as learned in previous
lessons
Basic Anatomy of the Arduino Board
Breadboard Setup
Reading a Resistor
Workspace Setup
NOTE: It is not necessary to declare the <iostream> library or to int main() in your program.
Those codes are prebuilt into the structure of the Arduino program.
Verify: Compiles
program to check for Upload program to
errors board
Yellow Wire =
Ground Wire
White Wire =
Power Wire
connects into
Port 13 to
Negative column
on breadboard
NOTE: Connection ports DO NOT need to be the same as long as the wires
and electrical components match up properly.
NOTE: LED Anode (Long Leg) should always be linked to the resistor and
Cathode (short Side) linked to the ground or the direction the circuit is
going.
Long Leg = Electricity In, + (Positive)
Short Leg = Electricity Out, - (Negative)
Program Code
Type following code
NOTE: LED Anode (Long Leg) should always be linked to the resistor and
Cathode (short Side) linked to the ground or the direction the circuit is
going.
Long Leg = Electricty In, + (Positive)
Short Leg = Electricity Out, - (Negative)
Program Code
Switch statement is like an If/Else/If/Else Statement. It only allows the user to compare discrete
values (one thing at a time). It is not possible to compare ranges of objects. (For that an If
statement is needed).
Program uses the Serial Monitor. Type in the characters a, b, c, d, e will turn on the LEDS they
are connected to.
Upload the program to test it.
Notice how when one key is pressed then another the light of the previous
key does not turn off.
Assignment: LED Lights
Modify the program to do the following
1. Only have the inputted LED Lamp Turned ON (All other LED’s turn off with each new input) I.E
When ‘a’ is pressed ‘a’ LED light is on, then when ‘b’ is pressed the ‘b’ LED light is on and the ‘a’ LED
is turned off.
2. Output if Input is NOT a, b, c, d, or e
a. Blink all of the LED Lamps for a duration of 2 seconds
b. Output in the Serial Monitor “ERROR, INPUT INVALID!”
NOTE: Serial.read converts all input to ASCCII code form. See below of values
Program 3: Push Button
Source Code
Circuit Diagram
Assignment: Push Button
1. Modify the program so the serial monitor screen shows
a. Text On (Button Pressed) and Off (Button Released)
b. Counter showing how many times the button has been released
2. Limit of On/Off
a. Maximum presses = 5
b. Once Limit is met do the following
i. Stop the light from turning on and off when button is pressed
ii. Serial Port Print “ALL DONE!”
iii. Only print “ALL DONE!” twice on separate lines (Serial.println)
Program 4: Ultrasonic Sensor
Source Code
Upload Program and Test it. NOTE the program will print the distance in the serial port
Assignment: Ultrasonic Sensor
1. Add a 220 Ohm resistor and LED on the circuit
2. Turn the light on when the an object gets with 20cm of ultrasonic sensor and turns off
when it is out of range