header-image

Istvan Lengyel's Blog

Just another Level 3 Computing Sites site

Blog

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
 
 
}

 

Fritzing Diagram

This is my Fritzing diagram for my RoverBot. I have made all attempts to ensure the standardisation of colour coding of my connections to the circuit board +5v is colour Red, Gnd is Black and the Data signals for the ultrasonic and line tracking sensors are orange and blue, and the H-bridge wiring for data signals to the Arduino are Blue, Purple and Green.

 

The connected pin numbers on the Arduino will be how I wire up my RoverBot and those same pin numbers will be used as the bases of my Arduino code. My variable settings will be as follows:

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.

Week of the 1st June 2017

This week students learned how to navigate and use Fusion 360 to create a Fidget Spinners. Students were walked through on how to create a simple spinner. They were all then tasked to create original pieces. An example of a simple fidget spinner design.

The final render for this spinner is using the Ash texture.

This spinner has been design to work with bearings that are 22mm in width and height of 7mm. The design can either be 3D printed or milled using a CNC machine.

Week of the 8th June 2017

This week students worked on preliminary design concepts for their autonomous robotics project. This week many students created an original base unit using Autodesk Fusion 360. Their base structural design should have made use of the following dimensions on the example drawing.

Once the design has been completed students should then render the component using the rendering tools in Fusion 360. Students may choose any material to render with as these textures will not be applied to the final product. An Example is illustrated below.

As you can see I have chosen a wood grain texture for my base unit. The base unit is preconfigured to support two encoder motors in the rear, a caster wheel in the front and four support rods for our components layer. It is highly advisable during the design phase to  analyze all constraints to our autonomous navigating robot project. The ultrasonic sensor housing will need to be designed and taken into consideration when developing your base structure, as the sensor needs to be able to bounce off the walls in our maze.