MOD 10
Page 1: Introduction – Key Words & Objectives
The Foundation of Robotics Programming
Welcome to the heart of your robotics journey. Before a robot can move a motor or sense a wall, it needs to understand data. This guide breaks down how we name, store, and manipulate that data using the Arduino C language.
🔑 Power Words & Definitions
Variable: Think of this as a labeled box in the computer's memory. You give the box a name (like
sensorValue) and store a number inside. You can change what’s in the box at any time.Byte: A digital "chunk" of information. It consists of 8 bits. It is the smallest addressable unit of memory in most computers.
Constant: A value that is "locked." Once defined (often using
#define), it cannot be changed while the program is running. We use these for things like Pin Numbers.Data Type: This tells the Arduino how much memory to set aside. An
int(integer) takes more space than abytebecause it can hold much larger numbers.Declaration: The act of "introducing" a variable to the program (e.g.,
int ledPin;).Initialization: Giving a variable its very first value (e.g.,
ledPin = 13;).
🎯 Learning Objectives
Master Syntax: Learn to describe variables and name C types without errors.
Scope Control: Understand the critical difference between Global and Local variables to prevent "bugs."
Hardware Integration: Write code that correctly assigns data types to physical pins.
Troubleshooting: Develop the "minds-on" ability to trace errors in code and fix them before uploading.
Professionalism: Practice saving files using industry-standard naming conventions (e.g.,
P4_Name_Grade).
Page 2: The Body – The Core Lesson
Understanding Data Types & Variable Logic
In Arduino programming, every piece of data must have a "Type." If you try to put a decimal number into an integer variable, the computer will get confused or cut off the data.
📊 The Arduino Data Type Map
Type | Memory Size | Range / Usage | Example Code |
char | 1 byte | Single characters in single quotes. |
|
byte | 1 byte | Small positive numbers (0 to 255). |
|
int | 2 bytes | Whole numbers (-32,768 to 32,767). |
|
unsigned int | 2 bytes | Positive whole numbers only (0 to 65,535). |
|
long | 4 bytes | Large numbers (-2 billion to 2 billion). |
|
float | 4 bytes | Numbers with decimals (high resolution). |
|
String | Variable | Text sequences in double quotes. |
|
🛠 How to Name and Declare
When naming variables, follow the "Innovator Rules":
Be Descriptive: Use
buttonStateinstead of justb.No Numbers First:
firstLEDis okay;1stLEDwill cause a crash.No Reserved Words: You cannot name a variable
char,void, orloop.Camel Case: Capitalize the second word for readability (e.g.,
myTotalValue).
🌍 Global vs. Local Scope
Global Variables: Declared at the very top of your code. Every function (setup, loop, etc.) can see and change them.
Local Variables: Declared inside a specific function (like
void loop). Only that function knows they exist. Once the function finishes, the variable is deleted.
Page 3: "What If?" – Scenarios & Error Tracing
Predicting Program Behavior
To be a great programmer, you must play "detective." Here are scenarios you will encounter in the lab:
Scenario A: The "Floating" Pin
What if you set a pin to INPUT but don't press the button?
Result: The pin is "floating." It picks up static electricity from the air and might read
1or0randomly.The Fix: Use
pinMode(pin, INPUT_PULLUP);. This forces the pin to stay at1(HIGH) until you press the button to pull it to0(LOW).
Scenario B: The Data Overflow
What if you have a byte variable set to 255 and you add 1 to it?
Result: It "rolls over" back to
0! Always choose a data type large enough for your highest possible calculation.
Scenario C: The Case of the Missing Semicolon
What if you write int ledPin = 13 without a ;?
Result: The compiler will point to the next line and say there is an error. C expects a semicolon to "close" the thought.
🔍 Error Tracing Practice
Look at this code snippet. Can you find the 3 errors?
C++
void setup() {
int char = 8
pinMode(char, OUTPUT)
}
Error 1:
charis a reserved data type and cannot be a variable name.Error 2: Missing semicolon after the number
8.Error 3: Missing semicolon after the
pinModecommand.
Page 4: Why Is This Important?
The "Why" Behind the Code
You might ask, "Why can't I just use long for everything since it holds the biggest numbers?" Here is why precision matters in Robotics:
1. Memory is Limited
An Arduino Uno is not a gaming PC. It has very little RAM (only 2KB). If you use long (4 bytes) for a simple On/Off switch that only needs a boolean or byte (1 byte), you are wasting 75% of that memory space. Efficient code allows for bigger, more complex robot builds.
2. Speed and Performance
Processing large data types takes more "clock cycles" from the CPU. For a robot that needs to make split-second decisions—like an autonomous car braking before a wall—fast, efficient variables can be the difference between a stop and a crash.
3. Collaboration & Maintenance
Professional engineers use #define and descriptive variable names so that other people can read their work. If you use int x = 8;, no one knows what x is. If you use #define BUTTON_PIN 8, any teammate can immediately understand your circuit.
4. Real-World Integration
In the "Lights On, Lights Off" project, variables act as the bridge between the physical world (the button press) and the digital world (the code logic). Understanding these basics prepares you for advanced topics like Sensors, PID Control, and AI.
Page 5: Prep Quiz (15 Questions)
Test Your Readiness for the Graded Assessment
What is the smallest addressable unit of memory in most computer architectures?
Define a "Variable" in your own words.
Which data type is best for storing the letter 'Z'?
If you need to store the value
50,000, why can't you use a standardinton an Arduino Uno?What is the difference between
floatanddoubleon an Arduino Uno?Explain the main advantage of a Global Variable.
Write the line of code to declare an integer named
motorSpeedand initialize it to150.True or False: A variable name can start with a symbol or a number.
What specific
pinModeoption prevents a pin from "floating" and picking up interference?How many bits are in a
longdata type?Why is it a bad idea to name your variable
void?What does the
digitalRead()function do?If you use
INPUT_PULLUP, what number is returned in the Serial Monitor when the circuit is open (button NOT pressed)?Correct the syntax:
float myTemp = 98,6Why should you use
#definefor pin numbers instead of a regular variable?