RaDAR – Arduino controlled robotics

Something I started almost two decades ago but with Arduino’s and modern cheap sensors, my interest has once again been awakened. The knowledge will work it’s way into RaDAR for sure!!!

I’m building a self balancing (Two wheeler) rover robot using scrapped wiper motors I bought around the year 2001. Meet the new Mach 2018 roving robot 🙂

Infrared sensors have been installed at each corner to serve as obstacle avoidance sensors. Specific motor movements have been programmed in the event of an obstacle being detected within a 5cm range. This is the first intelligence the robot will carry on which I will build further. Ultrasonic range sensors will be next, a very effective solution to detect obstacles up to a few meters away which can be accurate up to 5mm!!!

I have installed a tiny screen to display simple diagnostics.

My Arduino code so far for this is as follows:

/*

Mach 2018 – driven by Arduino.

Eddie Leighton – 23rd March 2018

*/

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

// ———————————————————————————-

int RPWM_Output = 5;
int LPWM_Output = 6;

int M2RPWM_Output = 9;
int M2LPWM_Output = 10;

// ———————————————————————————-

int LED = 13; // Use the onboard Uno LED

int RBObstaclePin = 0; // This is our input pin
int RBObstacle = HIGH; // HIGH MEANS NO OBSTACLE

int LBObstaclePin = 1;
int LBObstacle = HIGH;

int LFObstaclePin = 2;
int LFObstacle = HIGH;

int RFObstaclePin = 3;
int RFObstacle = HIGH;

// ———————————————————————————-

void setup()

{

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C

display.clearDisplay();
display.display();

display.setTextColor(WHITE);
display.setTextSize(2);

display.setCursor(10,0);
display.print(“Mach 2018”);
display.display();

// ———————————————————————————–

pinMode(RPWM_Output, OUTPUT);
pinMode(LPWM_Output, OUTPUT);

pinMode(M2RPWM_Output, OUTPUT);
pinMode(M2LPWM_Output, OUTPUT);

// ———————————————————————————–

pinMode(RBObstaclePin, INPUT);
pinMode(LBObstaclePin, INPUT);

pinMode(RFObstaclePin, INPUT);
pinMode(LFObstaclePin, INPUT);

pinMode(LED, OUTPUT);

}

void loop()
{

display.clearDisplay();
display.display();

display.setTextColor(WHITE);
display.setTextSize(2);

display.setCursor(10,0);
display.print(“Mach 2018”);

display.setTextSize(1);

// ———————————————————————————–

RBObstacle = digitalRead(RBObstaclePin);

if (RBObstacle == LOW)
{

display.setCursor(10,40);
display.print(“Right back”);
digitalWrite(LED, HIGH);
display.display();

stopBOT(); // STOP
delay (100); // Motors still

slowFWD(); // Move forward slowly
delay(1000); // for one second

stopBOT();
delay (100);

slowSpinCW (); // Spin slowly clockwise
delay(1000); // for one second

stopBOT();
delay (100);

display.clearDisplay();
display.display();

}

// ———————————————————————————–

LBObstacle = digitalRead(LBObstaclePin);

if (LBObstacle == LOW)
{

display.setCursor(10,40);
display.print(“Left back”);
digitalWrite(LED, HIGH);
display.display();

stopBOT();
delay (100);

slowFWD();
delay(1000);

stopBOT();
delay (100);

slowSpinCCW (); // Spin anti-clockwise
delay(1000); // for one second

stopBOT();
delay (100);

display.clearDisplay();
display.display();

}

// ———————————————————————————–

LFObstacle = digitalRead(LFObstaclePin);

if (LFObstacle == LOW)
{

display.setCursor(10,40);
display.print(“Left front”);
digitalWrite(LED, HIGH);
display.display();

stopBOT();
delay (100);

slowREV(); // Move backwards slowly
delay(1000); // for one second

stopBOT();
delay (100);

slowSpinCW ();
delay(1000);

stopBOT();
delay (100);

display.clearDisplay();
display.display();

}

// ———————————————————————————–

RFObstacle = digitalRead(RFObstaclePin);

if (RFObstacle == LOW)
{

display.setCursor(10,40);
display.print(“Right front”);
digitalWrite(LED, HIGH);
display.display();

stopBOT();
delay (100);

slowREV();
delay(1000);

stopBOT();
delay (100);

slowSpinCCW ();
delay(1000);

stopBOT();
delay (100);

display.clearDisplay();
display.display();

}

}

// — Mach motor control for specific movements ————————————-

void slowFWD () {

int reversePWM = -(400 – 511) / 2; // Left motor slow forward
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2forwardPWM = -(400 – 511) / 2; // Right motor slow forward
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

// ———————————————————————————–

void slowREV () {

int forwardPWM = (600 – 512) / 2; // Left motor slow reverse
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);

int M2reversePWM = (600 – 512) / 2; // Right motor slow reverse
analogWrite(M2LPWM_Output, 0);
analogWrite(M2RPWM_Output, M2reversePWM);

}

// ———————————————————————————–

void stopBOT () {

int reversePWM = -(500 – 511) / 2; // Left motor STOP
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2forwardPWM = -(500 – 511) / 2; // Right motor STOP
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

// ———————————————————————————–

void fastFWD () {

int reversePWM = -(600 – 511) / 2; // Left motor fast forward
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2forwardPWM = -(600 – 511) / 2; // Right motor fast forward
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

// ———————————————————————————–

void fastREV () {

int forwardPWM = (400 – 512) / 2; // Left motor fast reverse
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);

int M2reversePWM = (400 – 512) / 2; // Right motor fast reverse
analogWrite(M2LPWM_Output, 0);
analogWrite(M2RPWM_Output, M2reversePWM);

}

// ———————————————————————————–

void slowSpinCW () {

int reversePWM = -(400 – 511) / 2; // Left motor slow forward
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2reversePWM = (600 – 512) / 2; // Right motor slow reverse
analogWrite(M2LPWM_Output, 0);
analogWrite(M2RPWM_Output, M2reversePWM);

}

// ———————————————————————————–

void slowSpinCCW () {

int forwardPWM = (600 – 512) / 2; // Left motor slow reverse
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);

int M2forwardPWM = -(400 – 511) / 2; // Right motor slow forward
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

===========================================================================================

Next test, hoping to send variable speeds to the motors, essential when implementing self balancing!!!

/*

Mach 2018 – driven by Arduino.

Eddie Leighton – 23rd March 2018

*/

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

// ———————————————————————————-

int RPWM_Output = 5;
int LPWM_Output = 6;

int M2RPWM_Output = 9;
int M2LPWM_Output = 10;

// ———————————————————————————-

int LED = 13; // Use the onboard Uno LED

int RBObstaclePin = 0; // This is our input pin
int RBObstacle = HIGH; // HIGH MEANS NO OBSTACLE

int LBObstaclePin = 1;
int LBObstacle = HIGH;

int LFObstaclePin = 2;
int LFObstacle = HIGH;

int RFObstaclePin = 3;
int RFObstacle = HIGH;

// ———————————————————————————-

void setup()

{

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C

display.clearDisplay();
display.display();

display.setTextColor(WHITE);
display.setTextSize(2);

display.setCursor(10,0);
display.print(“Mach 2018”);
display.display();

// ———————————————————————————–

pinMode(RPWM_Output, OUTPUT);
pinMode(LPWM_Output, OUTPUT);

pinMode(M2RPWM_Output, OUTPUT);
pinMode(M2LPWM_Output, OUTPUT);

// ———————————————————————————–

pinMode(RBObstaclePin, INPUT);
pinMode(LBObstaclePin, INPUT);

pinMode(RFObstaclePin, INPUT);
pinMode(LFObstaclePin, INPUT);

pinMode(LED, OUTPUT);

}

void loop()
{

display.clearDisplay();
display.display();

display.setTextColor(WHITE);
display.setTextSize(2);

display.setCursor(10,0);
display.print(“Mach 2018”);

display.setTextSize(1);

// — Slow to fast, forward ———————————————————

for (int i=50; i >= -50; i–){

FWD(i);

delay(10);

}

stopBOT();
delay (100);

// — Slow to fast, reverse ———————————————————

for (int i=50; i >= -50; i–){

REV(i);

delay(10);

}

stopBOT();
delay (100);

// ———————————————————————————–

RBObstacle = digitalRead(RBObstaclePin);

if (RBObstacle == LOW)
{

display.setCursor(10,40);
display.print(“Right back”);
digitalWrite(LED, HIGH);
display.display();

stopBOT(); // STOP
delay (100); // Motors still

slowFWD(); // Move forward slowly
delay(1000); // for one second

stopBOT();
delay (100);

slowSpinCW (); // Spin slowly clockwise
delay(1000); // for one second

stopBOT();
delay (100);

display.clearDisplay();
display.display();

}

// ———————————————————————————–

LBObstacle = digitalRead(LBObstaclePin);

if (LBObstacle == LOW)
{

display.setCursor(10,40);
display.print(“Left back”);
digitalWrite(LED, HIGH);
display.display();

stopBOT();
delay (100);

slowFWD();
delay(1000);

stopBOT();
delay (100);

slowSpinCCW (); // Spin anti-clockwise
delay(1000); // for one second

stopBOT();
delay (100);

display.clearDisplay();
display.display();

}

// ———————————————————————————–

LFObstacle = digitalRead(LFObstaclePin);

if (LFObstacle == LOW)
{

display.setCursor(10,40);
display.print(“Left front”);
digitalWrite(LED, HIGH);
display.display();

stopBOT();
delay (100);

slowREV(); // Move backwards slowly
delay(1000); // for one second

stopBOT();
delay (100);

slowSpinCW ();
delay(1000);

stopBOT();
delay (100);

display.clearDisplay();
display.display();

}

// ———————————————————————————–

RFObstacle = digitalRead(RFObstaclePin);

if (RFObstacle == LOW)
{

display.setCursor(10,40);
display.print(“Right front”);
digitalWrite(LED, HIGH);
display.display();

stopBOT();
delay (100);

slowREV();
delay(1000);

stopBOT();
delay (100);

slowSpinCCW ();
delay(1000);

stopBOT();
delay (100);

display.clearDisplay();
display.display();

}

}

// — Mach motor control for specific movements ————————————-

void slowFWD () {

int reversePWM = -(400 – 511) / 2; // Left motor slow forward
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2forwardPWM = -(400 – 511) / 2; // Right motor slow forward
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

// ———————————————————————————–

void slowREV () {

int forwardPWM = (600 – 512) / 2; // Left motor slow reverse
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);

int M2reversePWM = (600 – 512) / 2; // Right motor slow reverse
analogWrite(M2LPWM_Output, 0);
analogWrite(M2RPWM_Output, M2reversePWM);

}

// ———————————————————————————–

void stopBOT () {

int reversePWM = -(500 – 511) / 2; // Left motor STOP
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2forwardPWM = -(500 – 511) / 2; // Right motor STOP
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

// ———————————————————————————–

void fastFWD () {

int reversePWM = -(600 – 511) / 2; // Left motor fast forward
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2forwardPWM = -(600 – 511) / 2; // Right motor fast forward
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

// ———————————————————————————–

void fastREV () {

int forwardPWM = (400 – 512) / 2; // Left motor fast reverse
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);

int M2reversePWM = (400 – 512) / 2; // Right motor fast reverse
analogWrite(M2LPWM_Output, 0);
analogWrite(M2RPWM_Output, M2reversePWM);

}

// ———————————————————————————–

void slowSpinCW () {

int reversePWM = -(400 – 511) / 2; // Left motor slow forward
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2reversePWM = (600 – 512) / 2; // Right motor slow reverse
analogWrite(M2LPWM_Output, 0);
analogWrite(M2RPWM_Output, M2reversePWM);

}

// ———————————————————————————–

void slowSpinCCW () {

int forwardPWM = (600 – 512) / 2; // Left motor slow reverse
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);

int M2forwardPWM = -(400 – 511) / 2; // Right motor slow forward
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

// ———————————————————————————–

void FWD (int spoed) {

// Slow 50 ….. to …… fast -50

int reversePWM = spoed; // Left motor forward
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);

int M2forwardPWM = spoed; // Right motor forward
analogWrite(M2LPWM_Output, M2forwardPWM);
analogWrite(M2RPWM_Output, 0);

}

// ———————————————————————————–

void REV (int spoed) {

// Slow 50 ….. to …… fast -50

int forwardPWM = spoed; // Left motor reverse
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);

int M2reversePWM = spoed; // Right motor reverse
analogWrite(M2LPWM_Output, 0);
analogWrite(M2RPWM_Output, M2reversePWM);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s