Let's Make Robots!

My First Bot - SUMBA

Hi everyone, im new to robot building and this will be my first robot, i do not have any materials for it yet i only have the completed code and a deadline of 4 weeks because of it being a school project aswell as a new hobby.

 

The basic idea of the robot is that it will have 3 infrared distance sensors, 1 facing forwards and 2 facing diagonaly to the side, if the right sensor picks something up then the robot will turn right, the left sensor is the same exept in reverse, if the centre sensor gets a reading then the robot drives forward, if nothing is registered then the robot enters scan mode and turns right continuously.

The wheels will be driven by 3 volt DC motors with gear boxes, they will be powered by a seperate power supply of 4 AA batterys that are attatched to the arduino and motors via 2 npn transistors.

 

Here is the code with coments, if you can see anything i can improve on please let me know and feel free to use the code yourself.

 

this code has been edited by birdmun, i added the PWM parts.

 

 

#define RIGHTDC 5

#define LEFTDC 6

#define MIDDLEIR A0

#define LEFTIR A1

#define RIGHTIR A2

#define DANGER 1000

#define HALFSPEED 127

 

void setup() {

  pinMode(RIGHTDC, OUTPUT);

  pinMode(LEFTDC, OUTPUT);

}

 

void loop() {

  if (analogRead(MIDDLEIR) < DANGER) {

    analogWrite(RIGHTDC, HALFSPEED);

    analogWrite(LEFTDC, HALFSPEED);

  } else if (analogRead(RIGHTIR) < DANGER) {

    analogWrite (LEFTDC, HALFSPEED);

  } else if (analogRead(LEFTIR) < DANGER) {

    analogWrite(RIGHTDC, HALFSPEED);

  } else if ((analogRead(LEFTIR) < DANGER) &&

             (analogRead(MIDDLEIR) < DANGER)) {

               analogWrite(RIGHTDC, HALFSPEED);

    analogWrite(LEFTDC, HALFSPEED);

 

  } else if ((analogRead(RIGHTIR) < DANGER) && 

             (analogRead(MIDDLEIR) < DANGER)) {

               analogWrite(RIGHTDC, HALFSPEED);

    analogWrite(LEFTDC, HALFSPEED);

  } else if ((analogRead(LEFTIR) < DANGER) && 

             (analogRead(MIDDLEIR) < DANGER) && 

             (analogRead(RIGHTIR) < DANGER)) {

    analogWrite(RIGHTDC, HALFSPEED);

    analogWrite(LEFTDC, HALFSPEED);

  } else if ((analogRead(LEFTIR) < DANGER) && 

             (analogRead(RIGHTIR) < DANGER)) {

    analogWrite(RIGHTDC, HALFSPEED);

    analogWrite(LEFTDC, HALFSPEED);

  } else {

    analogWrite(LEFTDC, 1);

  }

}

 

 

 

OLDER VERSIONS OF CODE

int rightdc = 5; //name the right dc motor pin

int leftdc = 4; //name the left dc motor pin

int middleir = A0; //name the infrared scanner in the centre

int rightir = A1; //name the infrared scanner on the right

int leftir = A2; //name the infrared scanner on the left

 

void setup()

{

  pinMode (rightdc, OUTPUT); //register the right motor as an output

  pinMode (leftdc, OUTPUT); //register the left motor as an output

}

void loop()

{

if (analogRead(middleir) < 1000)  

  digitalWrite(rightdc && leftdc, HIGH); //this says that if the centre scanner sees anything then bvoth motors are activated

else digitalWrite(rightdc && leftdc, LOW); //if not then both motors are turned off

 

if (analogRead(rightir) < 1000)

digitalWrite(leftdc, HIGH); //this says that if the right scanner sees anything then the left motor is activated

else digitalWrite(leftdc, LOW); //if not then left motor is turned off

 

if(analogRead(leftir) < 1000)

digitalWrite (rightdc, HIGH); //this says that if the left scanner sees anything then the righ motor is activated

else (digitalWrite(rightdc, LOW)); ////if not then right motor is turned off

 

if (analogRead(rightir && middleir && leftir) > 1000)

digitalWrite(leftdc, HIGH); //this says that if nothing is seen by anything then the left DC is activated this is its 'search mode'

}

 

 

 

(EDIT 1)

 

This is an edit that takes chicken parmi and birdmun's sugestions into acount

 

 

 

 

int rightdc = 5; //name the right dc motor pin

int leftdc = 4; //name the left dc motor pin

int middleir = A0; //name the infrared scanner in the centre

int rightir = A1; //name the infrared scanner on the right

int leftir = A2; //name the infrared scanner on the left

 

int danger = 1000;

 

void setup()

{

  pinMode (rightdc, OUTPUT); //register the right motor as an output

  pinMode (leftdc, OUTPUT); //register the left motor as an output

}

void loop()

{

if (analogRead(middleir) < danger)  

  digitalWrite(rightdc && leftdc, HIGH); //this says that if the centre scanner sees anything then both motors are activated

else digitalWrite(rightdc && leftdc, LOW); //if not then both motors are turned off

 

if (analogRead(rightir) < danger)

digitalWrite(leftdc, HIGH); //this says that if the right scanner sees anything then the left motor is activated

else digitalWrite(leftdc, LOW); //if not then left motor is turned off

 

if(analogRead(leftir) < danger)

digitalWrite (rightdc, HIGH); //this says that if the left scanner sees anything then the righ motor is activated

else (digitalWrite(rightdc, LOW)); ////if not then right motor is turned off

 

if(analogRead(leftir && middleir) < danger)

digitalWrite(rightdc && leftdc, HIGH);

else(rightdc && leftdc, LOW);

 

if(analogRead(rightir && middleir) < danger)

digitalWrite(rightdc && leftdc, HIGH);

else(digitalWrite(rightdc && leftdc, LOW));

 

if(analogRead(rightir && middleir && leftir) < danger)

digitalWrite(rightdc && leftdc, HIGH);

else (digitalWrite(rightdc && leftdc, LOW));

 

if(analogRead(leftir && rightir) < danger)

digitalWrite(rightdc && leftdc, HIGH);

else(digitalWrite(rightdc && leftdc, LOW));

 

if (analogRead(rightir && middleir && leftir) > danger)

digitalWrite(leftdc, HIGH); //this says that if nothing is seen by anything then the left DC is activated this is its 'search mode'

}

 

 

 

(EDIT 2)

 

 

int rightdc = 5; //name the right dc motor pin

int leftdc = 4; //name the left dc motor pin

int middleir = A0; //name the infrared scanner in the centre

int rightir = A1; //name the infrared scanner on the right

int leftir = A2; //name the infrared scanner on the left

 

int danger = 1000;

 

void setup()

{

  pinMode (rightdc, OUTPUT); //register the right motor as an output

  pinMode (leftdc, OUTPUT); //register the left motor as an output

}

void loop()

{{

if (analogRead(middleir) < danger) 

  digitalWrite(rightdc &&leftdc, HIGH); //this says that if the centre scanner sees anything then both motors are activated

else digitalWrite(rightdc, LOW);

digitalWrite(leftdc, LOW); //if not then both motors are turned off

}

{

if (analogRead(rightir) < danger)

digitalWrite(leftdc, HIGH); //this says that if the right scanner sees anything then the left motor is activated

else digitalWrite(leftdc, LOW); //if not then left motor is turned off

}{

if(analogRead(leftir) < danger)

digitalWrite (rightdc, HIGH); //this says that if the left scanner sees anything then the righ motor is activated

else (digitalWrite(rightdc, LOW)); ////if not then right motor is turned off

}{

if(analogRead(leftir && middleir) < danger)

digitalWrite(rightdc && leftdc, HIGH);

else digitalWrite(rightdc, LOW);

digitalWrite(leftdc, LOW); //if not then both motors are turned off

}{

if(analogRead(rightir && middleir) < danger)

digitalWrite(rightdc && leftdc, HIGH);

else digitalWrite(rightdc, LOW);

digitalWrite(leftdc, LOW);

}{

if(analogRead(rightir && middleir && leftir) < danger)

digitalWrite(rightdc && leftdc, HIGH);

else digitalWrite(rightdc, LOW);

digitalWrite(leftdc, LOW);

}{

if(analogRead(leftir && rightir) < danger)

digitalWrite(rightdc && leftdc, HIGH);

else digitalWrite(rightdc, LOW);

digitalWrite(leftdc, LOW);

}

if (analogRead(rightir && middleir && leftir) > danger)

digitalWrite(leftdc, HIGH); //this says that if nothing is seen by anything then the left DC is activated this is its 'search mode'

}

 

 

 

 

I have an arduino ordered and on the way here but the only way that i know this works is the helpful verify button in the arduino IDE

As I said before this is my very first robot  and i would apreciate any sugestion anyone has for me.

 

I would like to give a mention to chickenparmi and the arduino tutorials he has made, they were a great help to me and i recomend them to anyone.

here is a link to the youtube playlist   https://www.youtube.com/playlist?list=PLnesnfu5SD7Zowk43Pu7rytpFuBcJ7mxa


Comment viewing options

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

A little tip:

To not use an int (is big for a micro) define a macro!:)

instead of:

const int rightdc = 5; //name the right dc motor pin
const int leftdc = 4; //name the left dc motor pin


try using

#define rightdc 5
#define leftdc 4

i will keep that in mind for future projects but as i have never made a robot before i am trying to keep this simple.

thankyou for your input though

Also use the "else if" so the micro will skip instead of working for an already known thing.

 

for example:

 

 

if (condition){
   action;
}
else if (condition1){
   right;
}
else if (condition2){
   left;
}

else{ forward; }

/* another good thing to do is using the switch
for the same reason let's assume */

int scan(){
   sensorscan;
   if (sensorscan > X){ return 1;}
   else if (sensorscan <= X) { return 2;}
   else {return 4;}
}     void ourfunction(){    int value = scan();    switch(value){       case 1:          action1;          break;       case 2:          action2;          break;       case 3:          action3;          break;    } }

is this a way to make it smaller or will my bot not work without it?

It is a way to make the code cleaner

ok thank you

//The const keyword was added, because, you don't want to be changing
//the connection between the variable names and the pin numbers.
//Some code you find online will have this part as #defines.
//All lines were adjusted to be 80 chars or less.
//You would do well to use brackets with all statements that require
//them. It makes it easier to go back and add something to a section at
//a later date.
const int rightdc = 5; //name the right dc motor pin
const int leftdc = 4; //name the left dc motor pin
const int middleir = A0; //name the infrared scanner in the centre
const int rightir = A1; //name the infrared scanner on the right
const int leftir = A2; //name the infrared scanner on the left 
//Some programmers will suggest that you use CAPS for const names. It
//helps them stand out in code when you are reading through it.
//Also, "magic numbers" are bad. Bonus, if you decide to change the 
//number, you only need to change it in one place.
const int DANGER = 1000; //this is the level we are comparing against

void setup() {
  pinMode (rightdc, OUTPUT); //register the right motor as an output
  pinMode (leftdc, OUTPUT); //register the left motor as an output
}

void loop() {
  if (analogRead(middleir) < DANGER) {
	//this says that if the centre scanner sees anything then both
	//motors are activated
    //digitalWrite(rightdc && leftdc, HIGH);I don't think this will work
    digitalWrite(rightdc, HIGH);
    digitalWrite(leftdc, HIGH);
  } else {
	//if not then both motors are turned off
    //digitalWrite(rightdc && leftdc, LOW); 
    digitalWrite(rightdc, LOW);
    digitalWrite(leftdc, LOW);
  }
  
  if (analogRead(rightir) < DANGER) {
	//this says that if the right scanner sees anything then the left 
	//motor is activated
    digitalWrite(leftdc, HIGH); 
  } else {
    digitalWrite(leftdc, LOW); //if not then left motor is turned off
  }

  if (analogRead(leftir) < DANGER) {
	//this says that if the left scanner sees anything then the right
	//motor is activated
    digitalWrite (rightdc, HIGH); 
  } else {
    digitalWrite(rightdc, LOW); //if not then right motor is turned off
  }

  if (analogRead(rightir && middleir && leftir) > DANGER) {
	//this says that if nothing is seen by anything then the left DC is
	//activated this is its 'search mode'
    digitalWrite(leftdc, HIGH); 
  }
}

I have made some adjustments. Most were readability. Another coding expert popped up to point out why digitalWrite(leftdc && rightdc, HIGH); works; leftdc = true, and, rightdc = true, so, true && true = true = 1. Your statement would have attempted to make pin 1 high according to him. chickenparmi even breadboarded it, and, got the same response.

**I also checked. They have fixed the copy as HTML in 1.0.4

I will change some of my code to make it more readable, good idea.

thanks for the help.

one question though,. what does the const you put in front of int mean constant and what does it do. 

...

Not sure if you remember in my DC motor video, but just to make sure, remember to not hook DC motors up to the pins on the arduino directly as they draw to much current! (edit: just noticed you said you are going to use transistors, sorry) Pick yourself up a cheap motor driver board and that'll do the trick.

With the code you have at the moment you will be getting some conflicts. For example you have:

if (analogRead(middleir) < 1000)  and if(analogRead(leftir) < 1000)

What happens if both these statements are true?

Doing something like this: 

if ((analogRead(middleir) < 1000)  && (analogRead(leftir) > 1000) && if (analogRead(rightir) > 1000){

blah blah

}

else if{

blah blah

}

else{

blah blah

Doing it this way means that all possible conditions have something they can do.

Also adding a small delay in there won't hurt as you are reading from the analog pins.

I won't tell you how to do all of this exactly, but I think you are very capable of doing this.