This short series takes learners through videos and exercises about programming a robot vehicle, in this case, an Elegoo Smart Robot Car.
This mini-series follows on from our Electronics and Introduction to Arduino C series.
Please select a session heading to get started.
As a reminder for how to log-in and use Tinkercad, please see Session One of our Online Electronics Series
We have designed and provided a Tinkercad simulation of the robot car's electronics for these exercises.
Select the option to "Tinker This" to save the design to your account for editing.
There are some changes to the design for this circuit. The simulation only has two motors as the robot's wheels are paired, and the line following sensors have been replaced with PIRs which are programmed in the same way.
These challenges involve getting started with our Elegoo Smart Robot Car programming.
All learners should start with the bronze level and work their way up as far as they can.
Click on each challenge heading to expand.
The answers for each stage show only the altered part of the code. The complete program can be viewed at the end of this section.
If you are still stuck, please use the answer button below.
Your program should look like this:
//C++ code
//
void setup()
{
}
void loop()
{
}
#define constantName value
If you are still stuck, please use the answer button below.
//C++ code
//
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
void setup()
If you are still stuck, please use the answer button below.
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
}
If you are still stuck, please use the answer button below.
#define rightForwardPin 11
int carSpeed = 200;
void setup()
If you are still stuck, please use the answer button below.
int carSpeed = 200;
void robotForward()
{
}
void robotReverse()
{
}
void robotLeft()
{
}
void robotRight()
{
}
void robotStop()
{
}
void setup()
void robotForward(){
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
Tip: You will need to initiate the serial monitor in the void setup() as we are printing to it.
If you are still stuck, please use the answer button below.
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
robotForward();
}
If you are still stuck, please use the answer button below.
void loop()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
}
//C++ code
//
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
int carSpeed = 200;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
}
These exercises continue to build on the same program.
The answers for each stage show only the altered part of the code. The complete program can be viewed at the end of this section.
If you are still stuck, please use the answer button below.
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
void robotForward()
If you are still stuck, please use the answer button below.
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
}
int Distance_test(){
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
If you are still stuck, please use the answer button below.
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
If you are still stuck, please use the answer button below.
void loop()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
Serial.print(Distance_test());
Serial.println("cm");
}
//C++ code
//
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
}
void loop()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
Serial.print(Distance_test());
Serial.println("cm");
}
These exercises continue to build on the same program.
The answers for each stage show only the altered part of the code. The complete program can be viewed at the end of this section.
If you are still stuck, please use the answer button below.
//C++ code
//
#include <Servo.h>
Servo myServo; //creates a servo which can be called by name
#define leftDrivePin 5
If you are still stuck, please use the answer button below.
#define rightReversePin 9
#define servoPin 3
int carSpeed = 200;
servoName.attach(pinName);
If you are still stuck, please use the answer button below.
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
}
If you are still stuck, please use the answer button below.
int Trig = A5;
int rightDistance = 0;
int middleDistance = 0;
int leftDistance = 0;
void robotForward()
If you are still stuck, please use the answer button below.
void loop()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
}
void loop()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
}
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest Direction");
}
If you are still stuck, please use the answer button below.
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
//C++ code
//
#include <Servo.h>
Servo myServo; //creates a servo which can be called by name
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
#define servoPin 3
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
int rightDistance = 0;
int middleDistance = 0;
int leftDistance = 0;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
}
void loop()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
}
If you haven't already, remove the movement program inside the void loop() from the Bronze Level exercises. Then, see if you can write a program that has the robot drive itself along the clearest route. Remember, turning involves spinning on the spot, so you will need to work out when to stop turning to continue forward safely.
We shall be continuing with your program from last session.
Make sure you've finished Session One (up to and including Gold) before starting these.
These challenges involve programming our Elegoo Smart Robot Car to follow a line.
All learners should start with the bronze level and work their way up as far as they can.
Click on each challenge heading to expand.
The Elegoo Smart Robot Car has three line follow sensors - these are not available on Tinkercad. Instead, we have used three PIR sensors which work with the same programming.
If you are still stuck, please use the answer button below.
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
#define servoPin 3
#define leftPIRPin 2
#define middlePIRPin 4
#define rightPIRPin 10
If you are still stuck, please use the answer button below.
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
pinMode(leftPIRPin, INPUT);
pinMode(middlePIRPin, INPUT);
pinMode(rightPIRPin, INPUT);
}
If you are still stuck, please use the answer button below.
This is just one way you could split up the functions.
void setPath()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
}
void ultrasonicCheck()
{
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
}
void loop()
{
}
void lineFollow()
{
}
void loop ()
{
lineFollow();
}
//C++ code
//
#include <Servo.h>
Servo myServo; //creates a servo which can be called by name
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
#define servoPin 3
#define leftPIRPin 2
#define middlePIRPin 4
#define rightPIRPin 10
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
int rightDistance = 0;
int middleDistance = 0;
int leftDistance = 0;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
pinMode(leftPIRPin, INPUT);
pinMode(middlePIRPin, INPUT);
pinMode(rightPIRPin, INPUT);
}
void setPath()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
}
void ultrasonicCheck()
{
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
}
void lineFollow()
{
}
void loop ()
{
lineFollow();
}
bool variableName;
If you are still stuck, please use the answer button below.
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
}
variableName = digitalRead(pinName);
If you are still stuck, please use the answer button below.
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
}
Serial.print("First Sensor: ");
Serial.println(variableName);
If you are still stuck, please use the answer button below.
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
}
If you are still stuck, please use the answer button below.
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
if(leftLine && !midLine && !rightLine)
{
}
}
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
if(leftLine && !midLine && !rightLine)
{
}
else if(!leftLine && midLine && !rightLine)
{
}
}
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
if(leftLine && !midLine && !rightLine)
{
}
else if(!leftLine && midLine && !rightLine)
{
}
else if(!leftLine && !midLine && rightLine)
{
}
}
If you are still stuck, please use the answer button below.
if(leftLine && !midLine && !rightLine)
{
robotLeft();
delay(100);
robotStop();
}
else if(!leftLine && midLine && !rightLine)
{
robotForward();
delay(100);
robotStop();
}
else if(!leftLine && !midLine && rightLine)
{
robotRight();
delay(100);
robotStop();
}
//C++ code
//
#include <Servo.h>
Servo myServo; //creates a servo which can be called by name
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
#define servoPin 3
#define leftPIRPin 2
#define middlePIRPin 4
#define rightPIRPin 10
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
int rightDistance = 0;
int middleDistance = 0;
int leftDistance = 0;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
pinMode(leftPIRPin, INPUT);
pinMode(middlePIRPin, INPUT);
pinMode(rightPIRPin, INPUT);
}
void setPath()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
}
void ultrasonicCheck()
{
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
}
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
if(leftLine && !midLine && !rightLine)
{
robotLeft();
delay(100);
robotStop();
}
else if(!leftLine && midLine && !rightLine)
{
robotForward();
delay(100);
robotStop();
}
else if(!leftLine && !midLine && rightLine)
{
robotRight();
delay(100);
robotStop();
}
}
void loop ()
{
lineFollow();
}
If you are still stuck, please use the answer button below.
else if(leftLine && midLine && !rightLine)
{
robotLeft();
delay(50);
robotStop();
}
else if(!leftLine && midLine && rightLine)
{
robotRight();
delay(50);
robotStop();
}
If you are still stuck, please use the answer button below.
else if (!leftLine && !midLine && !rightLine)
{
robotRight();
delay(200);
robotStop();
}
else if (leftLine && midLine && rightLine)
{
robotForward();
delay(100);
robotStop();
}
Bug Alert: The above answer is correct. However, there is a bug within Tinkercad that means the comparison for all three being positive, triggers an error that stops us from running the simulator.
Be aware the error does not point to this, but to another part of the program that is not related.
To fix this bug, use the below version of the answer. The extra pair of brackets makes no difference to the comparison but allows Tinkercad to run the code successfully.
else if (!leftLine && !midLine && !rightLine)
{
robotRight();
delay(200);
robotStop();
}
else if ((leftLine && midLine) && rightLine)
{
robotForward();
delay(100);
robotStop();
}
If you are still stuck, please use the answer button below.
else
{
Serial.print("Error: Unexpected PIR readings");
}
//C++ code
//
#include <Servo.h>
Servo myServo; //creates a servo which can be called by name
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
#define servoPin 3
#define leftPIRPin 2
#define middlePIRPin 4
#define rightPIRPin 10
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
int rightDistance = 0;
int middleDistance = 0;
int leftDistance = 0;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
pinMode(leftPIRPin, INPUT);
pinMode(middlePIRPin, INPUT);
pinMode(rightPIRPin, INPUT);
}
void setPath()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
}
void ultrasonicCheck()
{
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
}
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
if(leftLine && !midLine && !rightLine)
{
robotLeft();
delay(100);
robotStop();
}
else if(!leftLine && midLine && !rightLine)
{
robotForward();
delay(100);
robotStop();
}
else if(!leftLine && !midLine && rightLine)
{
robotRight();
delay(100);
robotStop();
}
else if(leftLine && midLine && !rightLine)
{
robotLeft();
delay(50);
robotStop();
}
else if(!leftLine && midLine && rightLine)
{
robotRight();
delay(50);
robotStop();
}
else if (!leftLine && !midLine && !rightLine)
{
robotRight();
delay(200);
robotStop();
}
else if (leftLine && midLine && rightLine)
{
robotForward();
delay(100);
robotStop();
}
else
{
Serial.print("Error: Unexpected PIR readings")
}
}
void loop ()
{
lineFollow();
}
We shall be continung with your program from last session.
Make sure you've finished Gold Level for both Session One and Two before starting these.
These challenges involve programming our Elegoo Smart Robot Car to respond to an IR remote control.
All learners should start with the bronze level and work their way up as far as they can.
Click on each challenge heading to expand.
#define irReceiverPin 12
#include
command as we did for the servo library.IRremote.hpp
#include
instruction.If you are still stuck, please use the answer button below.
#include <IRremote.hpp>
pinMode
for the receiver.
IrReceiver.begin(pinName, ENABLE_LED_FEEDBACK);
If you are still stuck, please use the answer button below.
IrReceiver.begin(irReceiverPin, ENABLE_LED_FEEDBACK);
IrReceiver.decode()
for checking if a signal is received.If you are still stuck, please use the answer button below.
if (IrReceiver.decode())
{
}
Hex Values: Until now, we have been only working with the decimal counting system, using the digits 0-9. The Hex system has 16 digits in it's counting instead of 10, these are; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.
Remember, that to identify a hex value inside our Arduino C code, we need to put a '0x' before the value.
Serial.print()
line needs to be inside the if statement for when an IR signal is received.IrReceiver.decodedIRData.command
.
Serial.println(value, HEX);
If you are still stuck, please use the answer button below.
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
}
IrReceiver.resume();
If you are still stuck, please use the answer button below.
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
IrReceiver.resume();
}
//C++ code
//
#include <Servo.h>
#include <IRremote.hpp>
Servo myServo; //creates a servo which can be called by name
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
#define servoPin 3
#define leftPIRPin 2
#define middlePIRPin 4
#define rightPIRPin 10
#define irReceiverPin 12
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
int rightDistance = 0;
int middleDistance = 0;
int leftDistance = 0;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
pinMode(leftPIRPin, INPUT);
pinMode(middlePIRPin, INPUT);
pinMode(rightPIRPin, INPUT);
IrReceiver.begin(irReceiverPin, ENABLE_LED_FEEDBACK);
}
void setPath()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
}
void ultrasonicCheck()
{
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
}
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
if(leftLine && !midLine && !rightLine)
{
robotLeft();
delay(100);
robotStop();
}
else if(!leftLine && midLine && !rightLine)
{
robotForward();
delay(100);
robotStop();
}
else if(!leftLine && !midLine && rightLine)
{
robotRight();
delay(100);
robotStop();
}
else if(leftLine && midLine && !rightLine)
{
robotLeft();
delay(50);
robotStop();
}
else if(!leftLine && midLine && rightLine)
{
robotRight();
delay(50);
robotStop();
}
else if (!leftLine && !midLine && !rightLine)
{
robotRight();
delay(200);
robotStop();
}
else if (leftLine && midLine && rightLine)
{
robotForward();
delay(100);
robotStop();
}
else
{
Serial.print("Error: Unexpected PIR readings");
}
}
void loop ()
{
//lineFollow(); blocked out to allow easy read of remote values in serial monitor.
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
IrReceiver.resume();
}
}
Here are the HEX values for some of the buttons on the IR remote:
if (IrReceiver.decodedIRData.command == 0x15)
{
chosenAction();
}
If you are still stuck, please use the answer button below.
New function:
void remoteControl()
{
}
Remote command if statement:
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
if (IrReceiver.decodedIRData.command == 0xC) // button 0 on remote
{
lineFollow();
}
else if (IrReceiver.decodedIRData.command == 0x10) // button 1 on remote
{
ultrasonicCheck();
}
else if (IrReceiver.decodedIRData.command == 0x18) // button 7 on remote
{
setPath();
}
else if (IrReceiver.decodedIRData.command == 0x1A) // button 9 on remote
{
remoteControl();
}
IrReceiver.resume();
}
//C++ code
//
#include <Servo.h>
#include <IRremote.hpp>
Servo myServo; //creates a servo which can be called by name
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
#define servoPin 3
#define leftPIRPin 2
#define middlePIRPin 4
#define rightPIRPin 10
#define irReceiverPin 12
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
int rightDistance = 0;
int middleDistance = 0;
int leftDistance = 0;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
pinMode(leftPIRPin, INPUT);
pinMode(middlePIRPin, INPUT);
pinMode(rightPIRPin, INPUT);
IrReceiver.begin(irReceiverPin, ENABLE_LED_FEEDBACK);
}
void setPath()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
}
void ultrasonicCheck()
{
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
}
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
if(leftLine && !midLine && !rightLine)
{
robotLeft();
delay(100);
robotStop();
}
else if(!leftLine && midLine && !rightLine)
{
robotForward();
delay(100);
robotStop();
}
else if(!leftLine && !midLine && rightLine)
{
robotRight();
delay(100);
robotStop();
}
else if(leftLine && midLine && !rightLine)
{
robotLeft();
delay(50);
robotStop();
}
else if(!leftLine && midLine && rightLine)
{
robotRight();
delay(50);
robotStop();
}
else if (!leftLine && !midLine && !rightLine)
{
robotRight();
delay(200);
robotStop();
}
else if (leftLine && midLine && rightLine)
{
robotForward();
delay(100);
robotStop();
}
else
{
Serial.print("Error: Unexpected PIR readings");
}
}
void remoteControl()
{
}
void loop ()
{
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
if (IrReceiver.decodedIRData.command == 0xC) // button 0 on remote
{
lineFollow();
}
else if (IrReceiver.decodedIRData.command == 0x10) // button 1 on remote
{
ultrasonicCheck();
}
else if (IrReceiver.decodedIRData.command == 0x18) // button 7 on remote
{
setPath();
}
else if (IrReceiver.decodedIRData.command == 0x1A) // button 9 on remote
{
remoteControl();
}
IrReceiver.resume();
}
}
void loop()
, will assist with any debugging and checks.If you are still stuck, please use the answer button below.
void remoteControl()
{
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
if (IrReceiver.decodedIRData.command == 0x11) // button 2 on remote
{
robotForward();
}
else if (IrReceiver.decodedIRData.command == 0x14) // button 4 on remote
{
robotLeft();
}
else if (IrReceiver.decodedIRData.command == 0x15) // button 5 on remote
{
robotStop();
}
else if (IrReceiver.decodedIRData.command == 0x16) // button 6 on remote
{
robotRight();
}
else if (IrReceiver.decodedIRData.command == 0x19) // button 8 on remote
{
robotReverse();
}
}
IrReceiver.resume();
}
void loop()
after each action. Let us create a global variable we can use to store the mode selected in and use this to tell it when to exit the remote control program.
byte variableName = 0x0;
This type of variable is needed to hold the HEX command value.
if (IrReceiver.decode)
to keep the selected mode operating.If you are still stuck, please use the answer button below.
Global variable:
int robotMode = 0;
The if (IrReceiver.decode)
statement using new command and mode variables:
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
byte cmd = IrReceiver.decodedIRData.command;
IrReceiver.resume();
if (cmd == 0xC) // button 0 on remote
{
robotMode = 1;
lineFollow();
}
else if (cmd == 0x10) // button 1 on remote
{
robotMode = 2;
ultrasonicCheck();
}
else if (cmd == 0x18) // button 7 on remote
{
robotMode = 3;
setPath();
}
else if (cmd == 0x1A) // button 9 on remote
{
robotMode = 4;
remoteControl();
}
else if (cmd == 0x2) // FUNC/STOP on remote
{
robotMode = 0;
}
}
New else if statements:
else if(robotMode == 1){
lineFollow();
}
else if(robotMode == 2){
ultrasonicCheck();
}
else if(robotMode == 3){
setPath();
}
else if(robotMode == 4){
remoteControl();
}
//C++ code
//
#include
#include
Servo myServo; //creates a servo which can be called by name
#define leftDrivePin 5
#define rightDrivePin 6
#define leftForwardPin 7
#define leftReversePin 8
#define rightForwardPin 11
#define rightReversePin 9
#define servoPin 3
#define leftPIRPin 2
#define middlePIRPin 4
#define rightPIRPin 10
#define irReceiverPin 12
int carSpeed = 200;
int Echo = A4;
int Trig = A5;
int rightDistance = 0;
int middleDistance = 0;
int leftDistance = 0;
int robotMode = 0;
void robotForward()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Forward");
}
void robotReverse()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Reverse");
}
void robotLeft()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, HIGH);
digitalWrite(rightForwardPin, HIGH);
digitalWrite(rightReversePin, LOW);
Serial.println("Spin Left");
}
void robotRight()
{
analogWrite(leftDrivePin, carSpeed);
analogWrite(rightDrivePin, carSpeed);
digitalWrite(leftForwardPin, HIGH);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, HIGH);
Serial.println("Spin Right");
}
void robotStop()
{
analogWrite(leftDrivePin, 0);
analogWrite(rightDrivePin, 0);
digitalWrite(leftForwardPin, LOW);
digitalWrite(leftReversePin, LOW);
digitalWrite(rightForwardPin, LOW);
digitalWrite(rightReversePin, LOW);
Serial.println("Stop!");
}
int Distance_test()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
float distance = pulseIn(Echo, HIGH);
distance = distance / 2 / 29;
return (int)distance;
}
void setup()
{
pinMode(leftForwardPin, OUTPUT);
pinMode(leftReversePin, OUTPUT);
pinMode(rightReversePin, OUTPUT);
pinMode(rightForwardPin, OUTPUT);
pinMode(leftDrivePin, OUTPUT);
pinMode(rightDrivePin, OUTPUT);
Serial.begin(9600);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
myServo.attach(servoPin);
pinMode(leftPIRPin, INPUT);
pinMode(middlePIRPin, INPUT);
pinMode(rightPIRPin, INPUT);
IrReceiver.begin(irReceiverPin, ENABLE_LED_FEEDBACK);
}
void setPath()
{
robotForward();
delay(2000);
robotLeft();
delay(1000);
robotStop();
delay(3000);
robotReverse();
delay(5000);
robotRight();
delay (4000);
robotForward();
delay(3000);
robotStop();
myServo.write(0);
delay(500);
}
void ultrasonicCheck()
{
leftDistance = Distance_test();
Serial.print(leftDistance);
Serial.println("cm on left");
myServo.write(90);
delay(500);
middleDistance = Distance_test();
Serial.print(middleDistance);
Serial.println("cm ahead");
myServo.write(180);
delay(500);
rightDistance = Distance_test();
Serial.print(rightDistance);
Serial.println("cm on right");
if (rightDistance > middleDistance && rightDistance > leftDistance)
{
Serial.print("Right is the safest direction");
if (middleDistance < leftDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go left");
}
}
else if (leftDistance > middleDistance)
{
Serial.print("Left is the safest direction");
if (middleDistance < rightDistance)
{
Serial.print("Do not go forwards");
}
else
{
Serial.print("Do not go right");
}
}
else
{
Serial.print("Forwards is the safest direction");
if (leftDistance < rightDistance)
{
Serial.print("Do not go left");
}
else
{
Serial.print("Do not go right");
}
}
}
void lineFollow()
{
bool leftLine;
bool midLine;
bool rightLine;
leftLine = digitalRead(leftPIRPin);
midLine = digitalRead(middlePIRPin);
rightLine = digitalRead(rightPIRPin);
Serial.print("Left PIR: ");
Serial.println(leftLine);
Serial.print("Middle PIR: ");
Serial.println(midLine);
Serial.print("Right PIR: ");
Serial.println(rightLine);
if(leftLine && !midLine && !rightLine)
{
robotLeft();
delay(100);
robotStop();
}
else if(!leftLine && midLine && !rightLine)
{
robotForward();
delay(100);
robotStop();
}
else if(!leftLine && !midLine && rightLine)
{
robotRight();
delay(100);
robotStop();
}
else if(leftLine && midLine && !rightLine)
{
robotLeft();
delay(50);
robotStop();
}
else if(!leftLine && midLine && rightLine)
{
robotRight();
delay(50);
robotStop();
}
else if (!leftLine && !midLine && !rightLine)
{
robotRight();
delay(200);
robotStop();
}
else if ((leftLine && midLine) && rightLine)
{
robotForward();
delay(100);
robotStop();
}
else
{
Serial.print("Error: Unexpected PIR readings");
}
}
void remoteControl()
{
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
if (IrReceiver.decodedIRData.command == 0x11) // button 2 on remote
{
robotForward();
}
else if (IrReceiver.decodedIRData.command == 0x14) // button 4 on remote
{
robotLeft();
}
else if (IrReceiver.decodedIRData.command == 0x15) // button 5 on remote
{
robotStop();
}
else if (IrReceiver.decodedIRData.command == 0x16) // button 6 on remote
{
robotRight();
}
else if (IrReceiver.decodedIRData.command == 0x19) // button 8 on remote
{
robotReverse();
}
else if (IrReceiver.decodedIRData.command == 0x2) // FUNC/STOP on remote
{
robotMode = 0;
}
IrReceiver.resume();
}
}
void loop ()
{
if (IrReceiver.decode())
{
Serial.print("IR command received: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
byte cmd = IrReceiver.decodedIRData.command;
IrReceiver.resume();
if (cmd == 0xC) // button 0 on remote
{
robotMode = 1;
lineFollow();
}
else if (cmd == 0x10) // button 1 on remote
{
robotMode = 2;
ultrasonicCheck();
}
else if (cmd == 0x18) // button 7 on remote
{
robotMode = 3;
setPath();
}
else if (cmd == 0x1A) // button 9 on remote
{
robotMode = 4;
remoteControl();
}
else if (cmd == 0x2) // FUNC/STOP on remote
{
robotMode = 0;
}
}
else if(robotMode == 1){
lineFollow();
}
else if(robotMode == 2){
ultrasonicCheck();
}
else if(robotMode == 3){
setPath();
}
else if(robotMode == 4){
remoteControl();
}
}