Back to start
Arduino Robotic Car
//Include libraries for infrared, servomotor, wifi,
#include //infrared lib
#include //servo motor lib
#include "pitches.h" //music lib
#include //wifi
#include //improves serial reading
#include //infrared lib
#include
// wifi begin ------------------------------------------------------------------*/
#define CE_PIN 9 // set Pin
#define CSN_PIN 10 // set pin
// wifi end------------------------------------------------------------------*/


const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/* Joystick ------------------------------------------------------------------*/
int joystick[2]; // 2 element array holding Joystick readings
/* ---------------------------------------------------------------------------*/

void writeServo(int ServoValue); //write servo Value
long readIr(); // Read Infrared remote control
Servo myservo;
Servo mydrive;

int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
const int buzzerPin = 6;//the buzzer pin attach to
int fre;//set the variable to store the frequence value

const long TasteOn = 0xFFA25D; //all buttons send hex codes and are defined here as const long
const long TasteMode = 0xFF629D;
const long TasteSilent = 0xFFE21D;


const long TastePlay = 0xFF22DD;
const long TasteLeft = 0xFF02FD;
const long TasteRight = 0xFFC23D;

const long TasteEq = 0xFFE01F;
const long TasteMinus = 0xFFA857;
const long TastePlus = 0xFF906F;

const long Taste0 = 0xFF6897;
const long TasteRepeat = 0xFF9867;
const long TasteUsd = 0xFFB04F;

const long Taste1 = 0xFF30CF;
const long Taste2 = 0xFF18E7;
const long Taste3 = 0xFF7A85;

const long Taste4 = 0xFF10EF;
const long Taste5 = 0xFF38C7;
const long Taste6 = 0xFF5AA5;

const long Taste7 = 0xFF42BD;
const long Taste8 = 0xFF4AB5;
const long Taste9 = 0xFF52AD;

const int led1 = 2; // the pin that the LED is attached to
const int led2 = 3; // the pin that the LED is attached to
const int ServoPin = 7;
const int DrivePin = 8;
const int ObstaclePin = 6; // Obstacle Avoidance Sensor
const int irReceiverPin = 5;

long IrCode; // Code from remote Control
int i;
const int sensorMin = 0; // Min Sensorvalue for light
const int sensorMax = 1023; // Max Sensorvalue for light
const int MinServo = 90-40; // Min Servo Value
const int MaxServo = 90+40; // max Servo Value

IRrecv irrecv(irReceiverPin);
decode_results results;

int sensorReading1;
int sensorReading2;
int umgebungslichtL;
int umgebungslichtR;
int mySpeed = 90;
int SpeedOffset = 5; //step for speed
int TurnOffset = 90;
int MinSpeedOffset = 5;
int MaxSpeedOffset = 10;

bool DriveMode = false;
bool TestMode = false;
bool Forward = false;
bool Backward = false;
bool Left = false;
bool Right = false;

int range;
void setup()
{
myservo.attach(ServoPin);
mydrive.attach(DrivePin);
Serial.begin(9600);
delay(100);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(ObstaclePin, INPUT);
irrecv.enableIRIn();

Serial.println("nRF24l01 Receiver Starting");
radio.begin(); //for wifi control
radio.setRetries(0, 15);
radio.setPALevel(RF24_PA_HIGH);
radio.openReadingPipe(1,pipe);
radio.startListening();

myservo.write(MaxServo); //max range for navigation
digitalWrite(led2, HIGH);
delay(1000);

myservo.write(90);
digitalWrite(led2,LOW);
delay(100);

myservo.write(MinServo);
digitalWrite(led1, HIGH);
delay(1000);
myservo.write(90);
digitalWrite(led1, LOW);
}

void loop()
{
if ( radio.available() ) //if signal to wifi
{
// Read the data payload until we've received everything
bool done = false;
while (!done)
{
// Fetch the data payload
radio.read( joystick, sizeof(joystick) );

if (joystick[0] == 1){
Serial.print(" backwards ");
myservo.write(90); //straight
mydrive.write(95); //drives backwards
}
else if (joystick[0] == 2){
Serial.print(" forwards ");
myservo.write(90); //straight
mySpeed = 80; //drives forward
if (digitalRead(ObstaclePin)){
mydrive.write(mySpeed);
}
else {
mydrive.write(90); // stop Drive
}
}
else if (joystick[0] == 3){
myservo.write(MaxServo); //Wheels turn to right
mydrive.write(82); //good number for not fast driving
digitalWrite(led2, HIGH);
Serial.print(" Right ");
}

else if (joystick[0] == 4){
myservo.write(MinServo); //Wheels turn left
Serial.print(" Left ");
digitalWrite(led1, HIGH);
mydrive.write(82);
}

else{
myservo.write(90); //if no signal, do nothing
mydrive.write(90);
}

//infrared remote
IrCode = readIr(); //if push button 1: set led left on
if (IrCode == Taste1){
digitalWrite(led1, HIGH);
}
if (IrCode == Taste2){ //if push button 2: set led right on
digitalWrite(led2, HIGH);
}
if (IrCode == TasteOn){ //if push on button: set both leds on
digitalWrite(led2, LOW);
digitalWrite(led1, LOW);
}
}
}
else //if no radio signal --> infrared remote control
{
Serial.println("No radio available");
IrCode = readIr();
if (IrCode == Taste1){ //button 1: test mode, no driving
TestMode = true;
DriveMode = false;
}
else if (IrCode == Taste2){
TestMode = false; //button2: no test mode, driving
DriveMode = true;
}
else if (IrCode == TasteMode & Forward==false){
Forward = true;
Backward = false;
mySpeed = 90;
SpeedOffset = 0;
}
else if (IrCode == TasteMode & Backward==false){
Forward = false;
Backward = true;
mySpeed = 90;
SpeedOffset = 0;
}
else if (IrCode == TasteRight){
Left = false;
Right = true;
}
else if (IrCode == TasteLeft){
Left = true;
Right = false;
}
else if (IrCode == TasteOn){
mySpeed = 90;
SpeedOffset = 0;
}
}
if (TestMode){
mydrive.write(90); // stop Drive
sensorReading1 = analogRead(A0);//- umgebungslichtL; //Light sensors
int range1 = map(sensorReading1, sensorMin, sensorMax, 0, 90); //lightensor
sensorReading2 = analogRead(A1);// -umgebungslichtR;
int range2 = map(sensorReading2, sensorMin, sensorMax, 0, 90);
range = range1 - range2;
writeServo(90 -range);
}
else if (DriveMode){ //increase or decrease speed
if (IrCode == TasteMinus){ SpeedOffset = SpeedOffset - 1;} // Decrease Speed
else if (IrCode == TastePlus) { SpeedOffset = SpeedOffset + 1;} // Increase Speed
else if (IrCode == TasteLeft){ TurnOffset = TurnOffset - 10;} // Decrease turnoffset
else if (IrCode == TasteRight) { TurnOffset = TurnOffset + 10;} // Increase turnoffset
else if (IrCode == TastePlay) { TurnOffset = 90;} // set turnoffset to 90
else if (IrCode == Taste1) { TestMode = false;} // Stop Drivemode
if (SpeedOffset >= MaxSpeedOffset) {SpeedOffset = MaxSpeedOffset;} else if (SpeedOffset <= MinSpeedOffset){ SpeedOffset = MinSpeedOffset;} // set Speed offset
if (TurnOffset >= MaxServo) {TurnOffset = MaxServo;} else if (TurnOffset <= MinServo){ TurnOffset = MinServo;} // set Turn offset
writeServo(TurnOffset);
if (Forward) {
mySpeed = ( 90 - SpeedOffset);}
else if (Backward) {
mySpeed = ( 90 + SpeedOffset);}
if (digitalRead(ObtaclePin)){
mydrive.write(mySpeed);
}
else {
mydrive.write(90); // stop Drive
}

}
delay(500);
}
void writeServo(int ServoValue) // write servo with limits
{
if (ServoValue > MaxServo){
myservo.write(MaxServo);
}
else if (ServoValue < MinServo){
myservo.write(MinServo);
}
else {
myservo.write(ServoValue);
}
}

// Read Infrared remote control
long readIr()
{
long temp;
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
temp= results.value;
irrecv.resume(); // Receive the next value
}else
{
temp= 0;

}
return temp;
}