top of page
light-bulb-is-like-lemon-blue.png

Biobattry Challenge

microchip-concept-with-electronic-circuit-components-human-characters-holding-transistor-f

Parallel Circuit Challenge

vector-illustration-ushaped-magnet-which-nail-sucks-cartoon-style.png

The Electro-Magnet

Challenge

372019478_2da8510b-8214-4b0c-9186-83f11f1e501b.jpg

The Resistor 

Challenge

177833792_a02adc23-f634-48e1-aad9-11419fdea7e0.jpg

The Rocket Challenge

A Code for You!

Paste the Following Code into Your IDE and See What Happen to Your Bot.


 

// ============================================================

//  OBSTACLE AVOIDING ROBOT  —  Imani Genius Lab

//  Hardware: Arduino Nano | L298N | HC-SR04 | 2x DC Motor

//  Power:    9V Battery → L298N +12V terminal

// ============================================================

 

// ── PIN DEFINITIONS ─────────────────────────────────────────

// L298N Motor Driver — Motor A (Left Motor)

const int IN1 = 2;   // Motor A direction pin 1

const int IN2 = 3;   // Motor A direction pin 2

const int ENA = 6;   // Motor A speed (PWM)

 

// L298N Motor Driver — Motor B (Right Motor)

const int IN3 = 4;   // Motor B direction pin 1

const int IN4 = 5;   // Motor B direction pin 2

const int ENB = 9;   // Motor B speed (PWM)

 

// HC-SR04 Ultrasonic Sensor

const int TRIG = 10; // Trigger pin — sends sound pulse OUT

const int ECHO = 11; // Echo pin   — receives bounce back IN

 

// ── SETTINGS ────────────────────────────────────────────────

const int MOTOR_SPEED   = 180;  // 0–255  (try 150–200 for 9V)

const int TURN_SPEED    = 160;  // Speed during turns

const int SAFE_DISTANCE = 20;   // Stop if obstacle within 20 cm

const int STOP_DISTANCE = 12;   // Reverse if obstacle under 12 cm

 

// ── SETUP ───────────────────────────────────────────────────

void setup() {

  // Motor control pins — all OUTPUT

  pinMode(IN1, OUTPUT);

  pinMode(IN2, OUTPUT);

  pinMode(IN3, OUTPUT);

  pinMode(IN4, OUTPUT);

  pinMode(ENA, OUTPUT);

  pinMode(ENB, OUTPUT);

 

  // Ultrasonic sensor pins

  pinMode(TRIG, OUTPUT);

  pinMode(ECHO, INPUT);

 

  Serial.begin(9600);

  Serial.println("=== Obstacle Avoiding Robot Ready! ===");

 

  stopMotors();

  delay(2000); // 2-second pause before starting

}

 

// ── MAIN LOOP ───────────────────────────────────────────────

void loop() {

  int distance = getDistance();

 

  // Print distance to Serial Monitor for debugging

  Serial.print("Distance: ");

  Serial.print(distance);

  Serial.println(" cm");

 

  if (distance > SAFE_DISTANCE) {

    // ✅ Path is clear — move forward

    moveForward();

 

  } else if (distance <= STOP_DISTANCE) {

    // 🚨 Too close! Reverse, then decide which way to turn

    stopMotors();

    delay(300);

    moveBackward();

    delay(400);

    stopMotors();

    delay(300);

    decideDirection(); // Look left/right and pick clearer side

 

  } else {

    // ⚠️  Obstacle detected within safe zone — stop and turn

    stopMotors();

    delay(300);

    decideDirection(); // Look left/right and pick clearer side

  }

}

 

// ── OBSTACLE AVOIDANCE DECISION ─────────────────────────────

// Without a servo, we swing the robot body left & right to

// "look" with the front-facing sensor, then pick the clear side.

void decideDirection() {

  // Look RIGHT — turn right briefly and measure

  turnRight();

  delay(400);

  stopMotors();

  delay(200);

  int distRight = getDistance();

  Serial.print("Right distance: ");

  Serial.println(distRight);

 

  // Return to center

  turnLeft();

  delay(400);

  stopMotors();

  delay(200);

 

  // Look LEFT — turn left briefly and measure

  turnLeft();

  delay(400);

  stopMotors();

  delay(200);

  int distLeft = getDistance();

  Serial.print("Left distance: ");

  Serial.println(distLeft);

 

  // Return to center

  turnRight();

  delay(400);

  stopMotors();

  delay(200);

 

  // ── Choose the clearer direction ──

  if (distRight >= distLeft && distRight > SAFE_DISTANCE) {

    Serial.println("→ Turning RIGHT (more space)");

    turnRight();

    delay(500);

 

  } else if (distLeft > distRight && distLeft > SAFE_DISTANCE) {

    Serial.println("← Turning LEFT (more space)");

    turnLeft();

    delay(500);

 

  } else {

    // Both sides blocked — do a U-turn

    Serial.println("↩  Both sides blocked — U-Turn!");

    turnRight();

    delay(900);

  }

 

  stopMotors();

  delay(200);

}

 

// ── DISTANCE MEASUREMENT ────────────────────────────────────

// Sends a 10µs pulse, measures how long echo takes to return.

// Formula: distance (cm) = pulse duration (µs) ÷ 58

int getDistance() {

  // Clear trigger pin

  digitalWrite(TRIG, LOW);

  delayMicroseconds(2);

 

  // Send 10-microsecond pulse

  digitalWrite(TRIG, HIGH);

  delayMicroseconds(10);

  digitalWrite(TRIG, LOW);

 

  // Read echo pulse duration (timeout = 30 ms = 510 cm max range)

  long duration = pulseIn(ECHO, HIGH, 30000);

 

  // Convert to centimeters

  int distance = duration / 58;

 

  // Safety check — if reading is 0 or out of range, treat as far

  if (distance == 0 || distance > 400) {

    distance = 400;

  }

 

  return distance;

}

 

// ── MOTOR CONTROL FUNCTIONS ─────────────────────────────────

 

// ▶  Move FORWARD — both motors spin forward

void moveForward() {

  analogWrite(ENA, MOTOR_SPEED);

  analogWrite(ENB, MOTOR_SPEED);

  digitalWrite(IN1, HIGH);

  digitalWrite(IN2, LOW);   // Motor A forward

  digitalWrite(IN3, HIGH);

  digitalWrite(IN4, LOW);   // Motor B forward

}

 

// ◀  Move BACKWARD — both motors spin in reverse

void moveBackward() {

  analogWrite(ENA, MOTOR_SPEED);

  analogWrite(ENB, MOTOR_SPEED);

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, HIGH);  // Motor A backward

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, HIGH);  // Motor B backward

}

 

// ↻  Turn RIGHT — left motor forward, right motor backward

void turnRight() {

  analogWrite(ENA, TURN_SPEED);

  analogWrite(ENB, TURN_SPEED);

  digitalWrite(IN1, HIGH);

  digitalWrite(IN2, LOW);   // Left motor forward

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, HIGH);  // Right motor backward

}

 

// ↺  Turn LEFT — right motor forward, left motor backward

void turnLeft() {

  analogWrite(ENA, TURN_SPEED);

  analogWrite(ENB, TURN_SPEED);

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, HIGH);  // Left motor backward

  digitalWrite(IN3, HIGH);

  digitalWrite(IN4, LOW);   // Right motor forward

}

 

// ■  STOP — cut power to both motors

void stopMotors() {

  analogWrite(ENA, 0);

  analogWrite(ENB, 0);

  digitalWrite(IN1, LOW);

  digitalWrite(IN2, LOW);

  digitalWrite(IN3, LOW);

  digitalWrite(IN4, LOW);

}

 

// ============================================================

//  QUICK FIXES:

//  • Robot drifts left/right → adjust MOTOR_SPEED values per motor

//  • Motors too slow on 9V  → increase MOTOR_SPEED toward 220

//  • Robot hits walls       → increase SAFE_DISTANCE (e.g. 25 or 30)

//  • False obstacle alerts  → place sensor flat, not angled down

// ============================================================

Resistor 

Challenge

bottom of page