Let's Make Robots!

Makerson the Robot

Navigate around via ultrasound, aka he avoids obstacles.

My first robot with very little to show. He's built on the top of a container and uses code I got off the internet. I want to learn more about programming and eventually make my own code for him. My next add-on will be bump switches for use when Makerson gets stuck because of the blind spot of the Parallax PING)). OH, and his name was inspired by make magazine, my favorite. I think he's pretty good for a fourteen year old to build. Please comment on how to make my code better and how to add bump switches. The code is in the link.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Bump switches are very easy. You choose 2 pins, for the left and right bump sensor. Let's say 2 and 3.

In the beginning of the code, you define variables:

const int left_bumpsensor=2;

const int right_bumpsensor=3;

 

Then, in the setup function, you make your pins input:

  pinMode(right_bumpsensor, INPUT);

  pinMode(left_bumpsensor, INPUT);

 

You connect two ~10K resistors from pin 2 and pin 3 to ground. You connect the switches from the pins to +5v while still having the resistors.

In the loop code, you read the status of every switch with:

digitalRead(left_bumpsensor);

or digitalRead(right_bumpsensor);

 

I give an example:

  left_bump_state = digitalRead(left_bumpsensor);

  right_bump_state = digitalRead(right_bumpsensor);

  // if the left bumpswitch has made contact, we shut off the right motor. this will make the thing go right

  if (left_bump_state == HIGH)    

    digitalWrite(right_motor_pin, LOW);  

  else

    digitalWrite(right_motor_pin, HIGH);

 

  // if the right bumpswitch has made contact, we shut off the left motor. this will make the thing go left

  if (right_bump_state == HIGH)    

    digitalWrite(left_motor_pin, LOW);  

  else

    digitalWrite(left_motor_pin, HIGH);

 

Modify this to whatever fits your code. If you still have difficulties, paste your code here.

That is totally what I needed. thanks. Do I need to be concerned with debouncing?

 

I tried to give it a go, but here's what i got. I have no idea what this means

In function 'void loop()':

error: 'right_bump_state' was not declared in this scope

 

The code:

const int left_bumpsensor=2;

  const int right_bumpsensor=3;

  #define leftDir1      7      // Left motor direction 1, to AIn1.

  #define leftDir2      6      // Left motor direction 2, to AIn2.

  #define rightDir1     5      // Right motor direction 1, to BIn1.

  #define rightDir2     4      // Right motor direction 2, to BIn2.

void setup()

{

  pinMode(right_bumpsensor, INPUT);

  pinMode(left_bumpsensor, INPUT);

  pinMode(leftDir1, OUTPUT);        // Set motor direction pins as outputs.

  pinMode(leftDir2, OUTPUT);      

  pinMode(rightDir1, OUTPUT);      

  pinMode(rightDir2, OUTPUT); 

  Serial.begin(9600);

}

 

void loop()

{

  digitalRead(right_bumpsensor);

 

  right_bump_state = digitalRead(right_bumpsensor)

  // if the right bumpswitch has made contact, we shut off the left motor. this will make the thing go left

  if (right_bump_state == HIGH)    

    digitalWrite(left_motor_pin, LOW);  

  else

    digitalWrite(left_motor_pin, HIGH);

}

 

Thats what i wrote. Please help

 

 

You need to declare the variable right_bump_state before you use it. Place it up at the top of your program above the void setup() line.

Something like:

int right_bump_state;

It better be:

bool right_bumpstate;

Bools can be only false or true, and they take much less footprint in the memory than a int.

This is the code i have been using:

/*

 * 06_Object_Avoidance

 * 

 * Makey robot roams around, while avoiding objects

 * sensed with the Ping ultrasonic rangefinder.

 *

 * Makey Robot, MAKE Magazine, Volume 19, p. 77

 * Created: June 2009 Kris Magri

 * Modified:

 * 

 */

 

  #define pingPin       9      // Ping ultrasonic sensor on pin D9.

  #define leftDir1      7      // Left motor direction 1, to AIn1.

  #define leftDir2      6      // Left motor direction 2, to AIn2.

  #define rightDir1     5      // Right motor direction 1, to BIn1.

  #define rightDir2     4      // Right motor direction 2, to BIn2.

 

  #define BOUNDARY     35     // (cm) Avoid objects closer than 35cm.

  #define INTERVAL     25      // (ms) Interval between distance readings.

 

// setup

// Set motor pins as OUTPUTS, initialize Serial

 

void setup()                                                 

{

  pinMode(leftDir1, OUTPUT);        // Set motor direction pins as outputs.

  pinMode(leftDir2, OUTPUT);      

  pinMode(rightDir1, OUTPUT);      

  pinMode(rightDir2, OUTPUT); 

  Serial.begin(9600);

}

 

 

// Main program

// Roam around while avoiding objects.

// 

// Set motors to move forward,

// Take distance readings over and over, 

// as long as no objects are too close (determined by BOUNDARY).

// If object is too close, avoid it -- back up and turn.

// Repeat.

 

void loop()                     

{

  long distance;                    // Distance reading from rangefinder.

 

  forward();                        // Robot moves forward continuously.

  do 

  {

    distance = readDistance();      // Take a distance reading.

    Serial.println(distance);       // Print it out.             

    delay(INTERVAL);                // Delay between readings.

  }

  while(distance >= BOUNDARY);      // Loop while no objects close-by.

 

  // Robot has sensed a nearby object and exited the while loop.

  // Take evasive action to avoid object.          

  backward();                       // Move backward 500ms.

  delay(500);               

  rightTurn(300);                   // Turn right for 300ms.

 

 } // end Main program

 

 

// forward

//

// Move robot forward by setting both wheels forward.

// Will persist until something else changes the

// motors' directions.

 

void forward()

{

  digitalWrite(leftDir1, LOW);      // Left motor forward.             

  digitalWrite(leftDir2, HIGH);

  digitalWrite(rightDir1, LOW);     // Right motor forward.

  digitalWrite(rightDir2, HIGH);

}  

 

 

// backward

//

// Move robot backward by setting both wheels backward.

// Will persist until something else changes the

// motors' directions.

 

void backward()

{

  digitalWrite(leftDir1, HIGH);     // Left motor backward.

  digitalWrite(leftDir2, LOW);

  digitalWrite(rightDir1, HIGH);    // Right motor backward.

  digitalWrite(rightDir2, LOW);

}

 

 

// rightTurn

//

// Turn robot to right by moving wheels in opposite directions.

// Amount of turning is determined by duration argument (ms).

 

void rightTurn(int duration)

{

  digitalWrite(leftDir1, HIGH);     // Left motor backward.

  digitalWrite(leftDir2, LOW);

  digitalWrite(rightDir1, LOW);     // Right motor forward.

  digitalWrite(rightDir2, HIGH);

  delay(duration);                  // Turning time (ms).

}

 

 

// readDistance

// Take a distance reading from Ping ultrasonic rangefinder.

// from http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

 

long readDistance()

{

  long duration, inches, cm;

 

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.

  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.

  pinMode(pingPin, OUTPUT);

  digitalWrite(pingPin, LOW);

  delayMicroseconds(2);

  digitalWrite(pingPin, HIGH);

  delayMicroseconds(5);

  digitalWrite(pingPin, LOW);

 

  // The same pin is used to read the signal from the Ping: a HIGH

  // pulse whose duration is the time (in microseconds) from the sending

  // of the ping to the reception of its echo off an object.

  pinMode(pingPin, INPUT);

  duration = pulseIn(pingPin, HIGH);

 

  // Convert the time into a distance.

  cm = microsecondsToCentimeters(duration);

  return(cm);

}

 

long microsecondsToCentimeters(long microseconds)

{

  // The speed of sound is 340 m/s or 29 microseconds per centimeter.

  // The ping travels out and back, so to find the distance of the

  // object we take half of the distance traveled.

  return microseconds / 29 / 2;

}

check out the link at the top of my bots profile, it goes to make magazine where i got the project

 

Please can you add your code as an attachment and I will be glad to have a look over to highlight any improvements you may be able to make.

 

Dan