Vision of the Arduino Dalek

Ok, so that post title isn’t based on a Doctor Who episode title. Sue me.

Recently I’ve bought two <a href="http://robosavvy viagra without a doctor prescription.com/store/product_info.php/cPath/27/products_id/320″>Sharp GP2Y0A21YK Infrared Proximity Sensors from RoboSavvy. These little things can detect object in a range of 10cm to 80cm.

irsensorSo, the task now is to wire them up, and use them to detect when the Dalek get within 20 cm of an obstacle.

The circuitry is fairly easy. The Red wire goes to the 5v connection, the Black goes to GND, and the yellow wire goes to one of the Analogue inputs on the Arduno. I’m using pins A2 and A3, one for each sensor.

My soldering is, as ever, attrocious, so we’ll not have a picture of of that. Instead, here is a picture of the sensors Blu-Taked on to the front of the thing.

irmounted

And, as a bonus, a picture of the button that I’ve also fitted (and wired to Digital Pin 8).

dalekbutton

So, what we are going to do now is have the Dalek spin left if detects an obstable to the right, and spin right if it sees something to the left.

Also, if the button on the back is pressed, it will sleep for five seconds.

Portions of this code come from Lucky Larry’s website.

// Project: Dalek control system
// Version 0.3 - IR Sernsors
// Tony Blews tony@tonyblews.co.uk

int ButtonPin       = 8;
int MotorDirectionR = 10;
int MotorDirectionL = 11;
int MotorPowerR     = 12;
int MotorPowerL     = 13;
int IRPinLeft       = 2;
int IRPinRight      = 3;

void setup()
{
 pinMode(MotorDirectionR, OUTPUT);
 pinMode(MotorDirectionL, OUTPUT);
 pinMode(MotorPowerR, OUTPUT);
 pinMode(MotorPowerL, OUTPUT);
 pinMode(ButtonPin, INPUT);    // declare pushbutton as input

 Serial.begin(9600);
 Serial.println("Serial control Dalek system starting...");

}

float check_distance(int IRpin) {
 float volts = analogRead(IRpin)*0.0048828125;   // value from sensor * (5/1024) - if running 3.3.volts then change 5 to 3.3
 float distance = 30*pow(volts, -1.10);          // worked out from graph 65 = theretical distance / (1/Volts)S - luckylarry.co.uk
 return(distance);                               // http://luckylarry.co.uk/arduino-projects/arduino-using-a-sharp-ir-sensor-for-distance-calculation/     
}

// modes for the motor control
// convention here is modeXX - where X is F for forward, S for stationary and B for backwards
// first X is the left motor, second X is the right one
// for direction control, the LOW if forward and HIGH is backward
// for power control, LOW is off and HIGH is on

// all stop
void modeSS()
{
 digitalWrite(MotorDirectionR, LOW);
 digitalWrite(MotorDirectionL, LOW);
 digitalWrite(MotorPowerR,LOW);
 digitalWrite(MotorPowerL,LOW);
}

// move straight ahead
void modeFF()
{
 digitalWrite(MotorDirectionR, LOW);
 digitalWrite(MotorDirectionL, LOW);
 digitalWrite(MotorPowerR,HIGH);
 digitalWrite(MotorPowerL,HIGH);
}

// move straight backwards
void modeBB()
{
 digitalWrite(MotorDirectionR, HIGH);
 digitalWrite(MotorDirectionL, HIGH);
 digitalWrite(MotorPowerR,HIGH);
 digitalWrite(MotorPowerL,HIGH);
}

// spin left
void modeBF()
{
 digitalWrite(MotorDirectionR, LOW);
 digitalWrite(MotorDirectionL, HIGH);
 digitalWrite(MotorPowerR,HIGH);
 digitalWrite(MotorPowerL,HIGH);
}

//spin right
void modeFB()
{
 digitalWrite(MotorDirectionR, HIGH);
 digitalWrite(MotorDirectionL, LOW);
 digitalWrite(MotorPowerR,HIGH);
 digitalWrite(MotorPowerL,HIGH);
}

//main program loop
void loop()
{
 if (digitalRead(ButtonPin) == HIGH)
 {
 modeSS();
 delay(5000); // sleep for 5 seconds if button pressed.
 }
 modeFF();
 if ((check_distance(IRPinLeft) <20) && (check_distance(IRPinLeft) <20) )
 {
 //back up a bit
 modeBB();
 delay(1000);
 //rotate on spot for 2 sec (approx timing for 180 degrees)
 modeFB();
 delay(2000);
 }

 if (check_distance(IRPinLeft) <20)
 {
 // rotate on spot for 1 second (approx timing for 90 degrees)
 modeFB();
 delay(1000);
 }
 if (check_distance(IRPinRight) <20)
 {
 // rotate on spot for 1 second (approx timing for 90 degrees)
 modeBF();
 delay(1000);
 }
}

Video coming soon, I promise.

Leave a Reply