//including the libraries
#include <SoftwareSerial.h> // TX RX software library for bluetooth
//Defining pins for RGB led
#define GREEN 12
#define RED 11
#define delayTime 3
//Initializing pins for bluetooth Module
int bluetoothTx = 8; // bluetooth tx to 2 pin
int bluetoothRx = 7; // bluetooth rx to 3 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
//Front Motor Pins
int Enable1 = 3;
int Motor1_Pin1 = 2;
int Motor1_Pin2 = 4;
//Back Motor Pins
int Motor2_Pin1 = 5;
int Motor2_Pin2 = 9;
int Enable2 = 10;
//Front Light pins
int front_light1 = GREEN;
//Back light pins
int back_light1 = RED;
int horn = 6;
char command ; //variable to store the data
int velocity = 0; //Variable to control the speed of motor
void setup()
{
//Set the baud rate of serial communication and bluetooth module at same rate.
Serial.begin(9600);
bluetooth.begin(9600);
//Setting the L298N, LED and RGB LED pins as output pins.
pinMode(Motor1_Pin1, OUTPUT);
pinMode(Motor1_Pin2, OUTPUT);
pinMode(Enable1, OUTPUT);
pinMode(Motor2_Pin1, OUTPUT);
pinMode(Motor2_Pin2, OUTPUT);
pinMode(Enable2, OUTPUT);
pinMode(front_light1, OUTPUT);
pinMode(back_light1, OUTPUT);
pinMode(horn, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
//Setting the enable and LED pins as HIGH.
digitalWrite(GREEN, HIGH);
digitalWrite(RED, HIGH);
digitalWrite(Enable2, HIGH);
digitalWrite(back_light1, LOW);
digitalWrite(front_light1, LOW);
}
void loop(){
if(bluetooth.available() > 0){ //Checking if there is some data available or not
command = bluetooth.read(); //Storing the data in the 'command' variable
Serial.println(command); //Printing it on the serial monitor
//Change pin mode only if new command is different from previous.
switch(command){
case 'F': //Moving the Car Forward
digitalWrite(Motor1_Pin1, LOW);
digitalWrite(Motor1_Pin2, HIGH);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, LOW);
break;
case 'L': //Moving the Car Forward Left90
digitalWrite(Motor1_Pin1, LOW);
digitalWrite(Motor1_Pin2, HIGH);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, HIGH);
delay (30);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, LOW);
break;
case 'G': //Moving the Car Forward Left45
digitalWrite(Motor1_Pin1, LOW);
digitalWrite(Motor1_Pin2, HIGH);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, HIGH);
delay (30);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, LOW);
break;
case 'R': //Moving the Car Forward Right90
digitalWrite(Motor1_Pin1, LOW);
digitalWrite(Motor1_Pin2, HIGH);
digitalWrite(Motor2_Pin1, HIGH);
digitalWrite(Motor2_Pin2, LOW);
delay (30);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, LOW);
break;
case 'I': //Moving the Car Forward Right45
digitalWrite(Motor1_Pin1, LOW);
digitalWrite(Motor1_Pin2, HIGH);
digitalWrite(Motor2_Pin1, HIGH);
digitalWrite(Motor2_Pin2, LOW);
delay (30);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, LOW);
break;
case 'S': //Stop
digitalWrite(Motor1_Pin2, LOW);
digitalWrite(Motor1_Pin1, LOW);
digitalWrite(Motor2_Pin2, LOW);
digitalWrite(Motor2_Pin1, LOW);
break;
case 'B': //Moving the Car Backward
digitalWrite(Motor1_Pin1, HIGH);
digitalWrite(Motor1_Pin2, LOW);
break;
case 'J': //Moving the Car backward Right90
digitalWrite(Motor1_Pin2, LOW);
digitalWrite(Motor1_Pin1, HIGH);
digitalWrite(Motor2_Pin1, HIGH);
digitalWrite(Motor2_Pin2, LOW);
delay (30);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, LOW);
break;
case 'H': //Moving the Car backward Left90
digitalWrite(Motor1_Pin1, HIGH);
digitalWrite(Motor1_Pin2, LOW);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, HIGH);
delay (30);
digitalWrite(Motor2_Pin1, LOW);
digitalWrite(Motor2_Pin2, LOW);
break;
case 'W': //Front light ON
digitalWrite(front_light1, HIGH);
break;
case 'w': //Front light OFF
digitalWrite(front_light1, LOW);
break;
case 'U': //Back light ON
digitalWrite(back_light1, HIGH);
break;
case 'u': //Back light OFF
digitalWrite(back_light1, LOW);
break;
case 'V': //Horn On
{
int i = 200; // The starting pitch
while(i < 800) {
i++;
tone(horn, i); // Emit the noise
delay(5);
}
delay(100); // A short break in between each whoop
}
break;
case 'v': //Horn OFF
noTone(horn);
break;
case 'x': //Turn ON Everything
break;
case 'X': //Turn OFF Everything
break;
//Controlling the Speed of Car
default: //Get velocity
if(command=='q'){
velocity = 255; //Full velocity
analogWrite(Enable1, velocity);
}
else{
//Chars '0' - '9' have an integer equivalence of 48 - 57, accordingly.
if((command >= 48) && (command <= 57)){
//Subtracting 48 changes the range from 48-57 to 0-9.
//Multiplying by 25 changes the range from 0-9 to 0-225.
velocity = (command - 48)*25;
analogWrite(Enable1, velocity);
}
}
}
}
}
Comments