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 a byte because 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.

char grade = 'A';

byte

1 byte

Small positive numbers (0 to 255).

byte brightness = 200;

int

2 bytes

Whole numbers (-32,768 to 32,767).

int score = -50;

unsigned int

2 bytes

Positive whole numbers only (0 to 65,535).

unsigned int age = 15;

long

4 bytes

Large numbers (-2 billion to 2 billion).

long distance = 900000;

float

4 bytes

Numbers with decimals (high resolution).

float temp = 98.6;

String

Variable

Text sequences in double quotes.

String name = "Bot";

🛠 How to Name and Declare

When naming variables, follow the "Innovator Rules":

  1. Be Descriptive: Use buttonState instead of just b.

  2. No Numbers First: firstLED is okay; 1stLED will cause a crash.

  3. No Reserved Words: You cannot name a variable char, void, or loop.

  4. 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 1 or 0 randomly.

  • The Fix: Use pinMode(pin, INPUT_PULLUP);. This forces the pin to stay at 1 (HIGH) until you press the button to pull it to 0 (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)
}
  1. Error 1: char is a reserved data type and cannot be a variable name.

  2. Error 2: Missing semicolon after the number 8.

  3. Error 3: Missing semicolon after the pinMode command.


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

  1. What is the smallest addressable unit of memory in most computer architectures?

  2. Define a "Variable" in your own words.

  3. Which data type is best for storing the letter 'Z'?

  4. If you need to store the value 50,000, why can't you use a standard int on an Arduino Uno?

  5. What is the difference between float and double on an Arduino Uno?

  6. Explain the main advantage of a Global Variable.

  7. Write the line of code to declare an integer named motorSpeed and initialize it to 150.

  8. True or False: A variable name can start with a symbol or a number.

  9. What specific pinMode option prevents a pin from "floating" and picking up interference?

  10. How many bits are in a long data type?

  11. Why is it a bad idea to name your variable void?

  12. What does the digitalRead() function do?

  13. If you use INPUT_PULLUP, what number is returned in the Serial Monitor when the circuit is open (button NOT pressed)?

  14. Correct the syntax: float myTemp = 98,6

  15. Why should you use #define for pin numbers instead of a regular variable?