RoverBot Code

RoverBot Code

http://communityofrobots.com/tutorial/kawal/how-make-your-first-robot-using-arduino

My code for the RoverBot now includes functions for movement with output data to the 1602 LCD screen.

#include <NewPing.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

 
int lineL = 4; // Left line Tracker
int lineC = 3; // Center Line Tracker
int lineR = 2; // Right Line Tracker

int enableA = 13;      // Enable Motor A use pulse width modulation to change speed
int enableB = 5;       // Enable Motor B use pulse width modulation to change speed
int leftForward = 12;  // Left Motor Forward
int leftBackward = 11; // Left Motor Backward
int rightForward = 6;  // Right Motor Forward
int rigthBackward = 7; // Right Motor Backward

#define TRIGGER_PINL  A0  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PINL     A1  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PINC  A2  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PINC     A3  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PINR  A4  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PINR     A5  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200  // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
 
NewPing sonarLeft(TRIGGER_PINL, ECHO_PINL, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonarCenter(TRIGGER_PINC, ECHO_PINC, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonarRight(TRIGGER_PINR, ECHO_PINR, MAX_DISTANCE); // NewPing setup of pins and maximum distance.


void setup() {
  // put your setup code here, to run once:
pinMode(lineL, INPUT);
pinMode(lineC, INPUT);
pinMode(lineR, INPUT);

}

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

}


void forward(){
lcd.setCursor(0,0);
lcd.print("RoverBot is moving");
lcd.setCursor(0,1);
lcd.print("Forwards");
digitalWrite(rightBackward,LOW); // we set rightBackward to LOW indicating an off for the right motor not to go backward
digitalWrite(rightForward,HIGH); // we set rightForward to HIGH indicating an on for the right motor to move forward
digitalWrite(leftBackward,LOW); // we set leftBackward to LOW indicating an off for the left motor not to go backward
digitalWrite(leftForward,HIGH); // we set leftForward to HIGH indicating an on for the left motor to move forward
digitalWrite(enableA,100);    // motor A speed
digitalWrite(enableB,100);    // motor B speed
 
}
void backward(){
lcd.setCursor(0,0);
lcd.print("RoverBot is moving");
lcd.setCursor(0,1);
lcd.print("Backwards");
digitalWrite(rightBackward,HIGH); // we set rightBackward to HIGH indicating an on for the right motor to move backward
digitalWrite(rightForward,LOW);   // we set rightForward to LOW indicating an off for the right motor not to go forward
digitalWrite(leftBackward,HIGH); // we set leftBackward to HIGH indicating an on for the left motor to move  backward
digitalWrite(leftForward,LOW);   // we set leftForward to LOW indicating an off for the left motor not to go forward
digitalWrite(enableA,100);     // motor A speed
digitalWrite(enableB,100);     // motor B speed
 
}

void left(){
lcd.setCursor(0,0);
lcd.print("RoverBot is moving");
lcd.setCursor(0,1);
lcd.print("Left"); 
digitalWrite(rightBackward,HIGH); // we set pinRB to HIGH indicating an on for the right motor to move backward
digitalWrite(rightForward,LOW);  // we set pinRF to LOW indicating an off for the right motor not to go forward
digitalWrite(leftBackward,LOW); // we set pinLB to LOW indicating an off for the left motor not to go backward
digitalWrite(leftForward,HIGH); // we set pinLF to HIGH indicating an on for the left motor to move forward
digitalWrite(enableA,100);    // motor A speed 100
digitalWrite(enableB,100);    // motor B speed 100
 
}

void right(){
lcd.setCursor(0,0);
lcd.print("RoverBot is moving");
lcd.setCursor(0,1);
lcd.print("Right"); 
digitalWrite(rightBackward,LOW);  // we set pinRB to LOW indicating an off for the right motor not to go backward
digitalWrite(rightForward,HIGH);  // we set pinRF to HIGH indicating an on for the right motor to move forward
digitalWrite(leftBackward,HIGH);  // we set pinLB to HIGH indicating an on for the left motor to move  backward
digitalWrite(leftForward,LOW);    // we set pinLF to LOW indicating an off for the left motor not to go forward
digitalWrite(enableA,100);      // motor A speed
digitalWrite(enableB,100);      // motor B speed
}

 //Function to stop movement
 
void stop(){
lcd.setCursor(0,0);
lcd.print("RoverBot has ");
lcd.setCursor(0,1);
lcd.print("Stopped");
digitalWrite(enableA,0);  //motor A speed set to 0
digitalWrite(enableB,0);  //motor B speed set to 0
 
 
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Post navigation

  Next Post :
Previous Post :