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