DIY Robotics for Educators, Session # 3 – Writing Code

Session 3 of DIY Robotics for Educators is when we start writing actual code to control our robot, if you are feeling overwhelmed or have never coded before, relax, take a deep breath and type the code that you see in the video – do not COPY PASTE it, type it so that your mind can have the time to absorb it and don’t worry, we will explain more and have a more in-depth code related tutorial out as well very soon.

Robot Code 1 will help you test your motors and Robot Code 2 codes below will give you an idea about making FUNCTIONS when writing programs.

We hope that these tutorials are helpful for all educators, parents, and teachers who are following our classes, if you wish to join LIVE sessions for Robotics and other STEAM Activities, please contact +92 317 777 6787

Pop Up Learning – www.popuplearning.pk

Robot Code 01

int leftMotorEn = 3;
int leftMotorIn1= 2;
int leftMotorIn2= 4;

int rightMotorEn = 5;
int rightMotorIn1= 7; //IN 3 on L298
int rightMotorIn2= 8; //IN 4 on L298


void setup() {
  pinMode(leftMotorEn, OUTPUT);
  pinMode(leftMotorIn1, OUTPUT);
  pinMode(leftMotorIn2, OUTPUT);

  pinMode(rightMotorEn, OUTPUT);
  pinMode(rightMotorIn1, OUTPUT);
  pinMode(rightMotorIn2, OUTPUT);

  // Move Robot Forward
  analogWrite(leftMotorEn, 150);
  digitalWrite(leftMotorIn1, HIGH);
  digitalWrite(leftMotorIn2, LOW);
  analogWrite(rightMotorEn, 150);
  digitalWrite(rightMotorIn1, HIGH);
  digitalWrite(rightMotorIn2, LOW);

  delay(5000);

  // Stop Robot 
  analogWrite(leftMotorEn, 0);
  digitalWrite(leftMotorIn1, HIGH);
  digitalWrite(leftMotorIn2, LOW);
  analogWrite(rightMotorEn, 0);
  digitalWrite(rightMotorIn1, HIGH);
  digitalWrite(rightMotorIn2, LOW);

  delay(5000);
  
  // Move Robot Backward
  analogWrite(leftMotorEn, 150);
  digitalWrite(leftMotorIn1, LOW);
  digitalWrite(leftMotorIn2, HIGH);
  analogWrite(rightMotorEn, 150);
  digitalWrite(rightMotorIn1, LOW);
  digitalWrite(rightMotorIn2, HIGH);

  delay(5000);

  // Stop Robot
  analogWrite(leftMotorEn, 0);
  digitalWrite(leftMotorIn1, HIGH);
  digitalWrite(leftMotorIn2, LOW);
  analogWrite(rightMotorEn, 0);
  digitalWrite(rightMotorIn1, HIGH);
  digitalWrite(rightMotorIn2, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Robot Code 02

int leftMotorEn = 3;
int leftMotorIn1= 2;
int leftMotorIn2= 4;

int rightMotorEn = 5;
int rightMotorIn1= 7; //IN 3 on L298
int rightMotorIn2= 8; //IN 4 on L298


void setup() {
  pinMode(leftMotorEn, OUTPUT);
  pinMode(leftMotorIn1, OUTPUT);
  pinMode(leftMotorIn2, OUTPUT);

  pinMode(rightMotorEn, OUTPUT);
  pinMode(rightMotorIn1, OUTPUT);
  pinMode(rightMotorIn2, OUTPUT);

  moveForward(150);
  delay(5000);
  stopRobot();
  delay(5000);
  moveBackward(200);
  delay(5000);
  stopRobot();
  delay(2000);
  turnRight(200);
  delay(1000);  
  turnLeft(225);
  delay(1000);  
  stopRobot();
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

void turnRight(int spd){
  analogWrite(leftMotorEn, spd);
  digitalWrite(leftMotorIn1, HIGH);
  digitalWrite(leftMotorIn2, LOW);
  analogWrite(rightMotorEn, spd-50);
  digitalWrite(rightMotorIn1, HIGH);
  digitalWrite(rightMotorIn2, LOW);  
}

void turnLeft(int spd){
  analogWrite(leftMotorEn, spd-50);
  digitalWrite(leftMotorIn1, HIGH);
  digitalWrite(leftMotorIn2, LOW);
  analogWrite(rightMotorEn, spd);
  digitalWrite(rightMotorIn1, HIGH);
  digitalWrite(rightMotorIn2, LOW);  
}

void moveForward(int spd){
  analogWrite(leftMotorEn, spd);
  digitalWrite(leftMotorIn1, HIGH);
  digitalWrite(leftMotorIn2, LOW);
  analogWrite(rightMotorEn, spd);
  digitalWrite(rightMotorIn1, HIGH);
  digitalWrite(rightMotorIn2, LOW);  
}

void stopRobot(void){
  analogWrite(leftMotorEn, 0);
  digitalWrite(leftMotorIn1, LOW);
  digitalWrite(leftMotorIn2, LOW);
  analogWrite(rightMotorEn, 0);
  digitalWrite(rightMotorIn1, LOW);
  digitalWrite(rightMotorIn2, LOW);  
}

void moveBackward(int spd){
  analogWrite(leftMotorEn, spd);
  digitalWrite(leftMotorIn1, LOW);
  digitalWrite(leftMotorIn2, HIGH);
  analogWrite(rightMotorEn, spd);
  digitalWrite(rightMotorIn1, LOW);
  digitalWrite(rightMotorIn2, HIGH);  
}

You Might Also Like

× How can I help you?