ZUMI CODE:
from zumi.zumi import Zumi
import time
zumi = Zumi()
BASE_SPEED = 30
CORRECTION = 5 # tweak to fix drift
DELAY = 0.1 # seconds to move forward per loop
# Helper: move forward with correction
def move_forward():
left_speed = BASE_SPEED - CORRECTION
right_speed = BASE_SPEED
zumi.control_motors(left_speed, right_speed)
try:
while True:
front_left = zumi.front_left_detect()
front_right = zumi.front_right_detect()
if front_left or front_right:
print("Obstacle detected. Trying to escape...")
# Try turning right
zumi.stop()
zumi.turn_right(90)
time.sleep(0.5)
front_left = zumi.front_left_detect()
front_right = zumi.front_right_detect()
if not (front_left or front_right):
continue # right worked
# Try turning left 180
zumi.turn_left(180)
time.sleep(0.5)
front_left = zumi.front_left_detect()
front_right = zumi.front_right_detect()
if not (front_left or front_right):
continue # opposite way worked
# Try turning right 270 (now facing 90° from original direction)
zumi.turn_right(270)
time.sleep(0.5)
# If this doesn't work either, it'll just try again in next loop
else:
move_forward()
time.sleep(DELAY)
except KeyboardInterrupt:
zumi.stop()
print("Program stopped manually.")
LINE SENSING ROBOT CODE:
#include <RedBot.h>
// RedBot core
RedBotMotors motors;
// IR line sensors
RedBotSensor leftIR(A3);
RedBotSensor centerIR(A6);
RedBotSensor rightIR(A7);
// Bumpers
RedBotBumper leftBumper(3);
RedBotBumper rightBumper(11);
// Buzzer
const int buzzerPin = 9;
// Line detection threshold (adjust as needed)
#define LINETHRESHOLD 800
void setup() {
Serial.begin(9600);
pinMode(buzzerPin, OUTPUT);
Serial.println("RedBot starting...");
}
void loop() {
// Check for bumper press (collision)
if (!leftBumper.read() || !rightBumper.read()) {
motors.stop();
tone(buzzerPin, 1000, 500); // Buzzer: 1kHz tone for 0.5 sec
Serial.println("Bumper triggered. Stopping.");
while (true); // Stop forever
}
// Read line sensor values
int leftVal = leftIR.read();
int centerVal = centerIR.read();
int rightVal = rightIR.read();
// Debug output
Serial.print("L: "); Serial.print(leftVal);
Serial.print(" C: "); Serial.print(centerVal);
Serial.print(" R: "); Serial.println(rightVal);
// Line following logic
if (centerVal > LINETHRESHOLD && leftVal < LINETHRESHOLD && rightVal < LINETHRESHOLD) {
// Line under center — go straight
motors.leftDrive(50);
motors.rightDrive(50);
}
else if (leftVal > LINETHRESHOLD) {
// Line under left sensor — turn left
motors.leftDrive(10);
motors.rightDrive(170);
}
else if (rightVal > LINETHRESHOLD) {
// Line under right sensor — turn right
motors.leftDrive(170);
motors.rightDrive(10);
}
else {
// No line detected — keep moving forward
motors.leftDrive(120);
motors.rightDrive(120);
}
delay(20);
}