1.
The "Skeleton" of Every Arduino Program
Every single Arduino sketch has two mandatory sections. Think of it like a play: you set the
stage first, then the action happens repeatedly.
• void setup(): This runs once when you turn the robot on. You use it to tell the Arduino
which pins are inputs (sensors) and which are outputs (motors/LEDs).
• void loop(): This runs forever in a circle. As soon as it hits the bottom, it jumps back
to the top. This is where the robot "thinks" and reacts.
2. Understanding Variables (The Robot's Memory)
Before the setup, we usually name our pins. It is much easier to read digitalWrite(motorPin,
HIGH) than digitalWrite(7, HIGH).
• int: Stores integers (whole numbers).
• float: Stores decimals (useful for precise sensor readings).
3. The "Big Four" Commands
Most beginner robotics projects rely on just four main commands to interact with the world:
Command What it does Example
pinMode() Sets a pin as INPUT or OUTPUT. pinMode(13, OUTPUT);
digitalWrite() Sends "On" (HIGH) or "Off" (LOW) power. digitalWrite(13, HIGH);
digitalRead() Checks if a button/sensor is On or Off. int val = digitalRead(2);
analogRead() Gets a range of values (0 to 1023). int light = analogRead(A0);
4. Logic: Making Decisions
In a competition, your robot needs to make choices (e.g., "If I see a wall, turn left").
C++
if (distance < 10) {
// Code to stop motors or turn
} else {
// Code to move forward
}
5. Helpful "Pro-Tips" for the Competition
The Serial Monitor (The Debugger)
If the robot isn't working, your students need to "see" what it's thinking.
1. In setup, add: [Link](9600);
2. In loop, add: [Link](sensorValue);
3. Open the Serial Monitor in the Arduino IDE to see the live data.
Common Syntax "Gotchas"
• Semicolons ;: Every line of instruction must end with one.
• Curly Braces { }: These group code together. If you open one, you must close it.
• Case Sensitivity: digitalwrite will fail; it must be digitalWrite.
6. Sample "Starter" Code (Obstacle Avoidance)
Here is a template they can study to see how everything fits together:
C++
int motorPin = 9;
int sensorPin = A0;
void setup() {
pinMode(motorPin, OUTPUT);
pinMode(sensorPin, INPUT);
[Link](9600);
}
void loop() {
int sensorData = analogRead(sensorPin);
if (sensorData > 500) { // If something is close
digitalWrite(motorPin, LOW); // Stop
} else {
digitalWrite(motorPin, HIGH); // Go
}
}
2. The "Inputs" (The Robot's Senses)
In a competition, the robot must react to its environment. These components provide the data.
Component What it "Senses" Coding Logic
Ultrasonic Sensor (HC- Distance (Eyes) pulseIn() - Measures time for sound to
SR04) bounce back.
Line Follower (IR Contrast (Black vs. digitalRead() - High for dark, Low for
Sensor) White) light.
Potentiometer Rotation/Knob analogRead() - Returns a value from 0
position to 1023.
Push Button Physical touch/Impact digitalRead() - Checks if the circuit is
closed.
Photoresistor (LDR) Light brightness analogRead() - Resistance changes
with light.
Competition Tip: Use the Serial Monitor ([Link]) to print these sensor values. If the
robot isn't moving, check if the sensors are actually "seeing" what you think they are.
3. The "Outputs" (The Robot's Actions)
These parts do the work once the code makes a decision.
• LEDs: Use these as "status lights." (e.g., Red LED = Obstacle detected, Green LED =
Path clear).
o Note: Always use a 220-ohm resistor with LEDs so they don't burn out.
• DC Motors: The wheels. They require a Motor Driver (like the L298N or L293D)
because the Arduino cannot provide enough power to turn a motor directly.
• Servo Motor: Used for precise movement (0 to 180 degrees). Great for robot arms or
steering.
o Code: [Link](90); moves it to the center.
• Buzzer: Used for audio feedback (start of a race or an error alarm).
1. Data Types and Memory
In C++, you must tell the computer exactly what kind of data you are storing. Using the wrong
type can waste memory or cause the robot to crash.
Type Range / Use Example
bool true or false (1 or 0) bool isMoving = true;
int Whole numbers (-32,768 to 32,767) int speed = 255;
long Very large whole numbers long duration = pulseIn(pin);
Type Range / Use Example
float Decimal numbers float distance = 12.5;
const A value that never changes const int ledPin = 13;
Best Practice: Use const int for pin numbers. It prevents the code from accidentally changing
the pin number while the robot is running.
2. The Logic Gates (Control Flow)
In a competition, the robot must evaluate its surroundings using Boolean Logic.
The if / else if / else Structure
This is the "Decision Tree" of the robot.
C++
if (sensorValue < 200) {
// Action A (If condition is true)
}
else if (sensorValue < 500) {
// Action B (If first was false, but this is true)
}
else {
// Action C (If nothing above was true)
}
The for Loop (Repeated Actions)
If you need a robot to beep 5 times or move a servo slowly, use a for loop.
C++
// (Start at 0; stop at 5; add 1 each time)
for (int i = 0; i < 5; i++) {
digitalWrite(buzzer, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
delay(100);
}
3. Advanced Input/Output Functions
To get "great" at coding, they must move beyond simple on/off.
analogRead() vs analogWrite()
• analogRead(pin): Returns a value from 0 to 1023 (sensing voltage from 0V to 5V).
• analogWrite(pin, value): Uses PWM (Pulse Width Modulation) to simulate a voltage
from 0 to 255. This is how you control motor speed.
map() - The "Translation" Function
Often, your sensor gives you one range (0-1023) but your motor needs another (0-255). The
map function does the math for you.
C++
int sensorVal = analogRead(A0);
int speed = map(sensorVal, 0, 1023, 0, 255);
analogWrite(motorPin, speed);
4. Arithmetic and Comparison Operators
Robotics is math in motion. Students need to be comfortable with these symbols:
• == (Equal to): if (x == 10)
• != (Not equal to): if (x != 0)
• > / < (Greater/Less than)
• && (Logical AND): if (dist < 10 && speed > 0) — Both must be true.
• || (Logical OR): if (button == HIGH || timer > 1000) — Either can be true.
• % (Modulo): Returns the remainder of a division. Great for making things happen
every "X" number of loops.
5. Working with Libraries
For an international competition, they shouldn't "reinvent the wheel." C++ allows the use of
Libraries—pre-written code by experts.
Standard Robotics Libraries:
• <Servo.h>: For controlling servo motors.
• <Wire.h>: For I2C communication (advanced sensors/LCD screens).
• <NewPing.h>: A better way to use Ultrasonic sensors.
How to include them:
C++
#include <Servo.h> // Must be at the very top
Servo myServo; // Create the object
void setup() {
[Link](9); // Connect the object to a pin
}
6. The "Golden" Debugging Tool: Serial Communication
The biggest hurdle in a competition is not knowing why the robot stopped. They must master
the Serial Monitor.
1. [Link](9600); — Put this in void setup().
2. [Link]("Distance: "); — Prints text.
3. [Link](distance); — Prints the variable value and starts a new line.
Step-by-Step Exercise for the Students:
Ask them to write a program that:
1. Reads a Potentiometer (Analog Input).
2. If the value is over 512, an LED turns on.
3. Simultaneously, it prints the exact value to the Serial Monitor.