Welcome to Open-Lab VEI

Explore and download VEI's curated robotic and Arduino project codes.

Open-Lab VEI is a collaborative, hands-on innovation space developed by Voca Edge Institute (VEI). It is designed to empower students, creators, and innovators with the freedom to experiment, build, and learn in a real-world lab environment. Whether you’re a beginner exploring new skills or an expert developing prototypes, Open-Lab is your launchpad for creativity and technical excellence.

    
// Maze solving robot with tuned parameters

const int motorAForward = 7;   // Right Motor
const int motorABackward = 8;
const int enA = 9;
const int motorBForward = 3;    // Left Motor
const int motorBBackward = 4;
const int enB = 5;

// Ultrasonic pins
const int trigPinFront = 10;
const int echoPinFront = 11;
const int trigPinLeft = 12;
const int echoPinLeft = 13;
const int trigPinRight = A0;
const int echoPinRight = A1;

// Adjusted Distance thresholds (cm)
const int WALL_DISTANCE = 20;  // Increased from 10
const int WALL_TOO_CLOSE = 8;  // Slightly increased from 5
const int CORRECTION_DISTANCE = 15; // Increased from 12

// Adjusted Motor speeds (0-255)
const int BASE_SPEED = 250;        
const int SLOW_SPEED = 200;        
const int TURN_SPEED = 200;        

// Timing constants
const int TURN_DELAY = 600;  
const int CORRECTION_DELAY = 150;
const int BACKUP_DELAY = 250;

void setup() {
  // Initialize motor and sensor pins
  pinMode(motorAForward, OUTPUT);
  pinMode(motorABackward, OUTPUT);
  pinMode(enA, OUTPUT);
  pinMode(motorBForward, OUTPUT);
  pinMode(motorBBackward, OUTPUT);
  pinMode(enB, OUTPUT);

  pinMode(trigPinFront, OUTPUT);
  pinMode(echoPinFront, INPUT);
  pinMode(trigPinLeft, OUTPUT);
  pinMode(echoPinLeft, INPUT);
  pinMode(trigPinRight, OUTPUT);
  pinMode(echoPinRight, INPUT);

  // Set higher PWM frequency for smoother operation
  TCCR0B = (TCCR0B & 0b11111000) | 0x01;
  TCCR1B = (TCCR1B & 0b11111000) | 0x01;

  delay(1000);
}

void loop() {
  int frontDist = getDistance(trigPinFront, echoPinFront);
  int leftDist = getDistance(trigPinLeft, echoPinLeft);
  int rightDist = getDistance(trigPinRight, echoPinRight);

  // Emergency behavior
  if (frontDist <= WALL_TOO_CLOSE || leftDist <= WALL_TOO_CLOSE || rightDist <= WALL_TOO_CLOSE) {
    emergencyStop();
    adjustPosition(leftDist, rightDist);
  }

  // Navigation logic
  if (frontDist <= WALL_DISTANCE) {
    if (leftDist > WALL_DISTANCE) {
      turnLeft();
      delay(TURN_DELAY);
    } else if (rightDist > WALL_DISTANCE) {
      turnRight();
      delay(TURN_DELAY);
    } else {
      turnAround();
      delay(TURN_DELAY * 2);
    }
  } else {
    if (leftDist > WALL_DISTANCE) {
      turnLeft();
      delay(TURN_DELAY);
    } else {
      if (leftDist <= CORRECTION_DISTANCE) {
        slightRight();
        delay(CORRECTION_DELAY);
      } 
      else if (rightDist <= CORRECTION_DISTANCE) {
        slightLeft();
        delay(CORRECTION_DELAY);
      } else {
        moveForward();
      }
    }
  }
}

void adjustPosition(int leftDist, int rightDist) {
  // Back up if too close to front wall
  if (getDistance(trigPinFront, echoPinFront) <= WALL_TOO_CLOSE) {
    moveBackward();
    delay(BACKUP_DELAY);
    stopMotors();
  }
  
  // Adjust position based on which side is closer
  if (leftDist < rightDist) {
    slightRight();
    delay(CORRECTION_DELAY);
  } else {
    slightLeft();
    delay(CORRECTION_DELAY);
  }
  stopMotors();
}

int getDistance(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2; // in cm
}

// Movement
void moveForward() {
  digitalWrite(motorAForward, HIGH);
  digitalWrite(motorABackward, LOW);
  analogWrite(enA, BASE_SPEED);
  
  digitalWrite(motorBForward, HIGH);
  digitalWrite(motorBBackward, LOW);
  analogWrite(enB, BASE_SPEED);
}

void moveBackward() {
  digitalWrite(motorAForward, LOW);
  digitalWrite(motorABackward, HIGH);
  analogWrite(enA, SLOW_SPEED);
  
  digitalWrite(motorBForward, LOW);
  digitalWrite(motorBBackward, HIGH);
  analogWrite(enB, SLOW_SPEED);
}

void turnLeft() {
  // Right motor forward
  digitalWrite(motorAForward, HIGH);
  digitalWrite(motorABackward, LOW);
  analogWrite(enA, TURN_SPEED);
  
  // Left motor backward for sharper turn
  digitalWrite(motorBForward, LOW);
  digitalWrite(motorBBackward, HIGH);
  analogWrite(enB, TURN_SPEED/2);
}

void turnRight() {
  // Right motor backward
  digitalWrite(motorAForward, LOW);
  digitalWrite(motorABackward, HIGH);
  analogWrite(enA, TURN_SPEED/2);
  
  // Left motor forward
  digitalWrite(motorBForward, HIGH);
  digitalWrite(motorBBackward, LOW);
  analogWrite(enB, TURN_SPEED);
}

void slightLeft() {
  digitalWrite(motorAForward, HIGH);
  digitalWrite(motorABackward, LOW);
  analogWrite(enA, BASE_SPEED);
  
  digitalWrite(motorBForward, HIGH);
  digitalWrite(motorBBackward, LOW);
  analogWrite(enB, SLOW_SPEED);
}

void slightRight() {
  digitalWrite(motorAForward, HIGH);
  digitalWrite(motorABackward, LOW);
  analogWrite(enA, SLOW_SPEED);
  
  digitalWrite(motorBForward, HIGH);
  digitalWrite(motorBBackward, LOW);
  analogWrite(enB, BASE_SPEED);
}

void turnAround() {
  // Right motor backward
  digitalWrite(motorAForward, LOW);
  digitalWrite(motorABackward, HIGH);
  analogWrite(enA, TURN_SPEED);
  
  // Left motor forward
  digitalWrite(motorBForward, HIGH);
  digitalWrite(motorBBackward, LOW);
  analogWrite(enB, TURN_SPEED);
}

void emergencyStop() {
  digitalWrite(motorAForward, LOW);
  digitalWrite(motorABackward, LOW);
  analogWrite(enA, 0);
  
  digitalWrite(motorBForward, LOW);
  digitalWrite(motorBBackward, LOW);
  analogWrite(enB, 0);
  delay(100);
}

void stopMotors() {
  emergencyStop(); // same as stop
}


    
    
    
 /*------ Arduino Fire Fighting Robot Code----- */
#include 
Servo myservo;
int pos = 0;    
boolean fire = false;
/*-------defining Inputs------*/
#define Left_Sensor A0      // left sensor
#define Right_Sensor A1      // right sensor
#define Forward_Sensor A2   //forward sensor
/*-------defining Outputs------*/
#define LeftMotor1 7       // left motor
#define LeftMotor2 8       // left motor
#define enA 5
#define RightMotor1 9       // right motor
#define RightMotor2 10       // right motor
#define enB 6
#define pump 12
int speed = 120 ; //  speed of motor1 (0-255)

void setup()
{
    pinMode(Left_Sensor, INPUT);
    pinMode(Right_Sensor, INPUT);
    pinMode(Forward_Sensor, INPUT);
    pinMode(LeftMotor1, OUTPUT);
    pinMode(LeftMotor2, OUTPUT);
    pinMode(enA, OUTPUT);
    pinMode(RightMotor1, OUTPUT);
    pinMode(RightMotor2, OUTPUT);
    pinMode(enB, OUTPUT);
    pinMode(pump, OUTPUT);
    myservo.attach(11);
    myservo.write(90); 
}
void put_off_fire()
{
    delay (500);
    digitalWrite(LeftMotor1, HIGH);
    digitalWrite(LeftMotor2, HIGH);
    digitalWrite(RightMotor1, HIGH);
    digitalWrite(RightMotor2, HIGH);
    analogWrite(enA, 0);    
    analogWrite(enB, 0);   
    digitalWrite(pump, HIGH);
    delay(500);
    for (pos = 90; pos >= 50; pos -= 1) { 
        myservo.write(pos); 
        delay(10);  
    }
  
    for (pos = 50; pos <= 130; pos += 1) { 
        myservo.write(pos); 
        delay(10);  
    }
    for (pos = 130; pos >= 50; pos -= 1) { 
        myservo.write(pos); 
        delay(10);
    }
    digitalWrite(pump,LOW);
      for (pos = 50; pos <= 90; pos += 1) { 
      myservo.write(pos); 
      delay(10);  
    }
    //myservo.write(90);
    fire=false;
}

void loop()
{
    myservo.write(90); //Sweep_Servo();  
    if (digitalRead(Left_Sensor) ==1 && digitalRead(Right_Sensor)==1 && digitalRead(Forward_Sensor) ==1) //If no fire is detect all sensors are zero
    {
      //Do not move the robot
      digitalWrite(LeftMotor1, HIGH);
      digitalWrite(LeftMotor2, HIGH);
      digitalWrite(RightMotor1, HIGH);
      digitalWrite(RightMotor2, HIGH);
      analogWrite(enA, 0);
      analogWrite(enB, 0);
    }
    else if (digitalRead(Forward_Sensor) ==0) //If Fire is detected straight ahead
    {
      //Move the robot forward
      digitalWrite(LeftMotor1, HIGH);
      digitalWrite(LeftMotor2,LOW);
      analogWrite(enA,speed);
      digitalWrite(RightMotor1, HIGH);
      digitalWrite(RightMotor2, LOW);
      analogWrite(enB,speed);
      fire = true;
    }
    else if (digitalRead(Left_Sensor) ==0) //If Fire is detected to the left
    {
      //Move the robot left
      digitalWrite(LeftMotor1, HIGH);
      digitalWrite(LeftMotor2, LOW);
      analogWrite(enA, speed);
      digitalWrite(RightMotor1, HIGH);
      digitalWrite(RightMotor2, HIGH);
      analogWrite(enB, 0 );
      fire = true;
    }
    else if (digitalRead(Right_Sensor) ==0) //If Fire is detected to the right
    {
      //Move the robot right
      digitalWrite(LeftMotor1, HIGH);
      digitalWrite(LeftMotor2, HIGH);
      analogWrite(enA, 0);
      digitalWrite(RightMotor1, HIGH);
      digitalWrite(RightMotor2, LOW);
      analogWrite(enB, speed);
      fire = true;
    }
    delay(300); //Slow down the speed of robot
    while (fire == true)
    {
        put_off_fire();
    }
  }


    
    
    
// Motor Pins
const int IN1 = 8;
const int IN2 = 9;
const int IN3 = 10;
const int IN4 = 11;

// Sensor Pins
const int rightSensor = 2;
const int leftSensor = 3;

void setup() {
  // Set motor pins as output
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);

  // Set sensor pins as input
  pinMode(rightSensor, INPUT);
  pinMode(leftSensor, INPUT);

  // Optional: Start Serial Monitor
  Serial.begin(9600);

  // Wait for 5 seconds
  delay(5000); 
}

void loop() {
  // Read sensors
  int rightStatus = digitalRead(rightSensor);
  int leftStatus = digitalRead(leftSensor);

  // Debugging output (optional)
  Serial.print("Left: ");
  Serial.print(leftStatus);
  Serial.print(" | Right: ");
  Serial.println(rightStatus);

  if (leftStatus == LOW && rightStatus == LOW) {
    // Move forward
    moveForward();
  }
  else if (leftStatus == LOW && rightStatus == HIGH) {
    // Turn Left
    turnLeft();
  }
  else if (leftStatus == HIGH && rightStatus == LOW) {
    // Turn Right
    turnRight();
  }
  else {
    // Stop if both sensors detect black (no line)
    stopMotors();
  }
}

// Motor control functions
void moveForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void turnLeft() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void turnRight() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}


void stopMotors() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

    
    
    
int leftSensor = A0;
int rightSensor = A1;
int motorLeft = 9;
int motorRight = 10;

void setup() {
  pinMode(motorLeft, OUTPUT);
  pinMode(motorRight, OUTPUT);
  pinMode(leftSensor, INPUT);
  pinMode(rightSensor, INPUT);
}
....
    
    
    
//Bluetooth RC Car Coding        
        

SoftwareSerial Bluetooth(8,9);//tx, rx

//Motor Pin L298N DC
//--------------------

//Input Pin Signal
const int in1=10;
const int in2=11;
const int in3=12;
const int in4=13;

/**
* Jumper Enable A and Enabled B for  Speed Controller
* Value=> 0%=0 , 25%=64, 50%=127, 75%=191, 100% = 255
*/
const int ena=A0; //enabled A


void setup() {
  pinMode(in1,OUTPUT);
  pinMode(in2,OUTPUT);
  pinMode(in3,OUTPUT);
  pinMode(in4,OUTPUT);
  Serial.begin(9600);///* Define baud rate for serial communication */..USB communication and serial key id
  Bluetooth.begin(9600);/* Define baud rate for software serial communication */
  //Serial.println("Started");
}

void loop() {

      if(Bluetooth.available()) {
        Data=Bluetooth.read();
        if (Data==('L')) {
            //LEFT WHEEL
            digitalWrite(in4, HIGH);
            digitalWrite(in3, LOW);
            digitalWrite(in2, LOW);
            digitalWrite(in1, LOW);
        }
        else if (Data == 'R') {
            //RIGHT WHEEL
            digitalWrite(in4, LOW);
            digitalWrite(in3, LOW);
            digitalWrite(in2, HIGH);
            digitalWrite(in1, LOW);

        }
        else if (Data == 'F') {
          //FORWARD WHEEL
          digitalWrite(in4, LOW);
          digitalWrite(in3, HIGH);
          digitalWrite(in2, LOW);
          digitalWrite(in1, HIGH);
        }
        else if (Data == 'B') {
          //BACKWARD WHEEL
          digitalWrite(in4, HIGH);
          digitalWrite(in3, LOW);
          digitalWrite(in2, HIGH);
          digitalWrite(in1, LOW);
        }
        else if (Data==('S')) {
          //STOP WHEEL
          digitalWrite(in4, LOW);
          digitalWrite(in3, LOW);
          digitalWrite(in2, LOW);
          digitalWrite(in1, LOW);
        }
        else if (Data=='T'){
          analogWrite(ena,255); //Turbo
          //analogWrite(enb,255); Turbo
        }
        else {
          //STOP WHEEL
          digitalWrite(in4, LOW);
          digitalWrite(in3, LOW);
          digitalWrite(in2, LOW);
          digitalWrite(in1, LOW);
        }
      }
}