Code for voice controlled car T4TECHZ
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1); //TX, RX respetively
String readvoice;
int LED1 = A0; //define LED 1 pin
int LED2 = A1; //define LED 2 pin
int LMP=3;
int LMN=4;
int RMP=5;
int RMN=6;
long duration, distance, duration2, distance2;
void setup() {
BT.begin(9600);
Serial.begin(9600);
pinMode(LMP, OUTPUT);
pinMode(LMN, OUTPUT);
pinMode(RMP, OUTPUT);
pinMode(RMN, OUTPUT);
}
//-----------------------------------------------------------------------//
void loop() {
while (BT.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = BT.read(); //Conduct a serial read
readvoice += c; //build the string- "forward", "reverse", "left" and "right"
}
if(readvoice == "*forward#")
{
digitalWrite(LMP, HIGH);
digitalWrite (LMN, HIGH);
digitalWrite(RMP,LOW);
digitalWrite(RMN,LOW);
delay(100);
}
else if(readvoice == "*back#")
{
digitalWrite (LMP,LOW);
digitalWrite (LMN,LOW);
digitalWrite (RMP, HIGH);
digitalWrite (RMN, HIGH);
delay(100);
}
else if (readvoice == "*right#")
{
digitalWrite (LMP,HIGH);
digitalWrite (LMN,LOW);
digitalWrite (RMP,LOW);
digitalWrite (RMN,LOW);
delay (800);
digitalWrite(LMP, HIGH);
digitalWrite(LMN, HIGH);
digitalWrite(RMP,LOW);
digitalWrite(RMN,LOW);
delay(100);
}
else if ( readvoice == "*left#")
{
digitalWrite (LMP, LOW);
digitalWrite (LMN, HIGH);
digitalWrite (RMP, LOW);
digitalWrite (RMN, LOW);
delay (800);
digitalWrite(LMP, HIGH);
digitalWrite (LMN, HIGH);
digitalWrite(RMP,LOW);
digitalWrite(RMN,LOW);
delay(100);
}
else if (readvoice == "*stop#")
{
digitalWrite (LMP, LOW);
digitalWrite (LMN, LOW);
digitalWrite (RMP, LOW);
digitalWrite (RMN, LOW);
delay (100);
}
else if (readvoice == "*keep watch in all direction#")
{
digitalWrite (LMP, HIGH);
digitalWrite (LMN, LOW);
digitalWrite (RMP, LOW);
digitalWrite (LMN, LOW);
delay (100);
}
else if(readvoice == "*turn on light#")
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
}
else if(readvoice == "*turn off light#")
{
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
}
readvoice="";} //Reset the variablevoid setup()
// put your setup code here, to run once:
Comments
Post a Comment