1:00- 2:30 PM: Load test code. for forward function, correct wiring as needed
Program 1: Drive Forward for 5 seconds, then stop; adjust motor wiring
Program 2: Turn left for 5 seconds, then stop; adjust motor wiring
Concepts:
Writing and calling functions;
Using the serial monitor
PWM; analogWrite command
// --- Motor A (Left Motor) Pins ---
const int AIN1 = 5;
const int AIN2 = 6;
// --- Motor B (Right Motor) Pins ---
const int BIN1 = 9;
const int BIN2 = 10;
void setup() {
// Configure all motor control pins as outputs
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
stopRobot();
}
void loop() {
moveForward();
}
void moveForward() {
// Left Motor Forward
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
// Right Motor Forward
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
}
2:30 to 4 PM: Full Robot Test
Have students write additional functions and main loop. This can be done quickly using copy-and-paste.
If motors turn to fast, the digitalWrite( AINx, HIGH) statements can be replaced with analogWrite(AINx, 0-255)
// --- Motor A (Left Motor) Pins ---
const int AIN1 = 5;
const int AIN2 = 6;
// --- Motor B (Right Motor) Pins ---
const int BIN1 = 9;
const int BIN2 = 10;
void setup() {
// Configure all motor control pins as outputs
pinMode(AIN1, OUTPUT);
pinMode(AIN2, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
// Initialize by ensuring the robot is completely stopped
stopRobot();
// Optional: Start serial communication for debugging
Serial.begin(9600);
Serial.println("Robot Ready!");
}
void loop() {
// --- Demonstration Sequence ---
// This loop will cycle through all directions to test your wiring.
Serial.println("Moving Forward");
moveForward();
delay(2000);
Serial.println("Stopping");
stopRobot();
delay(1000);
Serial.println("Moving Reverse");
moveReverse();
delay(2000);
Serial.println("Stopping");
stopRobot();
delay(1000);
Serial.println("Turning Left");
turnLeft();
delay(1000);
Serial.println("Stopping");
stopRobot();
delay(1000);
Serial.println("Turning Right");
turnRight();
delay(1000);
Serial.println("Stopping");
stopRobot();
delay(3000); // Wait 3 seconds before repeating the sequence
}
// ==========================================
// MOVEMENT HELPER FUNCTIONS
// ==========================================
void moveForward() {
// Left Motor Forward
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
// Right Motor Forward
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
}
void moveReverse() {
// Left Motor Reverse
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
// Right Motor Reverse
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
}
void turnLeft() {
// Left Motor Reverse
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, HIGH);
// Right Motor Forward
digitalWrite(BIN1, HIGH);
digitalWrite(BIN2, LOW);
}
void turnRight() {
// Left Motor Forward
digitalWrite(AIN1, HIGH);
digitalWrite(AIN2, LOW);
// Right Motor Reverse
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, HIGH);
}
void stopRobot() {
// Set all pins LOW to let the motors coast to a stop
digitalWrite(AIN1, LOW);
digitalWrite(AIN2, LOW);
digitalWrite(BIN1, LOW);
digitalWrite(BIN2, LOW);
}