0% found this document useful (0 votes)
56 views3 pages

Voice Control Robot by Bluetooth

This code controls 4 DC motors and a servo motor to move a robot. It also controls 2 LEDs and a buzzer. The robot can move forward, backward, left, and right based on serial commands. The LEDs, buzzer, and motors are initialized and their behavior is defined on various serial commands.

Uploaded by

KbVennela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

Voice Control Robot by Bluetooth

This code controls 4 DC motors and a servo motor to move a robot. It also controls 2 LEDs and a buzzer. The robot can move forward, backward, left, and right based on serial commands. The LEDs, buzzer, and motors are initialized and their behavior is defined on various serial commands.

Uploaded by

KbVennela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <AFMotor.

h>
#include <Servo.h>
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
AF_DCMotor motor3(3, MOTOR12_8KHZ);
AF_DCMotor motor4(4, MOTOR12_8KHZ);

String readString;
Servo myServo;
int LED1 = A0;
int LED2 = A1;
int buzzerPin = A2;
void setup() {
Serial.begin(9600);
motor1.setSpeed(60);
motor2.setSpeed(60);
motor1.setSpeed(60);
motor2.setSpeed(60);
myServo.attach(10);
myServo.write(90);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}

void loop() {
{
while(Serial.available()){
delay(50);
char c=Serial.read();
readString+=c;
Serial.println(c);
}
if(readString.length()>0){
Serial.println(readString);
if (readString =="forward" || readString =="FORWARD")
{
myServo.write(90);
motor1.setSpeed(60);
motor1.run(FORWARD);
motor2.setSpeed(60);
motor2.run(FORWARD);
motor3.setSpeed(60);
motor3.run(FORWARD);
motor4.setSpeed(60);
motor4.run(FORWARD);
}
if (readString =="backward" || readString =="BACKWARD"){
motor1.setSpeed(60);
motor1.run(BACKWARD);
motor2.setSpeed(60);
motor2.run(BACKWARD);
motor3.setSpeed(60);
motor3.run(BACKWARD);
motor4.setSpeed(60);
motor4.run(BACKWARD);
}
if (readString =="left" || readString =="LEFT"){
myServo.write(180);
delay(1000);
myServo.write(90);
delay(1000);
motor1.setSpeed(0);
motor1.run(RELEASE);
motor2.setSpeed(0);
motor2.run(RELEASE);
motor3.setSpeed(100);
motor3.run(FORWARD);
motor4.setSpeed(100);
motor4.run(FORWARD);
delay (500);
motor1.setSpeed(60);
motor1.run(FORWARD);
motor2.setSpeed(60);
motor2.run(FORWARD);
motor3.setSpeed(60);
motor3.run(FORWARD);
motor4.setSpeed(60);
motor4.run(FORWARD);

}
if (readString =="right" || readString =="RIGHT"){
myServo.write(0);
delay(1000);
myServo.write(90);
motor1.setSpeed(60);
motor1.run(FORWARD);
motor2.setSpeed(60);
motor2.run(FORWARD);
motor3.setSpeed(0);
motor3.run(RELEASE);
motor4.setSpeed(0);
motor4.run(RELEASE);
delay (500);
motor1.setSpeed(60);
motor1.run(FORWARD);
motor2.setSpeed(60);
motor2.run(FORWARD);
motor3.setSpeed(60);
motor3.run(FORWARD);
motor4.setSpeed(60);
motor4.run(FORWARD);
}
if (readString =="stop" || readString =="STOP"){

motor1.setSpeed(0);
motor1.run(RELEASE);
motor2.setSpeed(0);
motor2.run(RELEASE);
motor3.setSpeed(0);
motor3.run(RELEASE);
motor4.setSpeed(0);
motor4.run(RELEASE);
delay(100);
digitalWrite(A0,HIGH);
digitalWrite(A1,HIGH);
delay(1000);
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);

}
if (readString =="LED on" || readString ==" LED ON " || readString =="led on"
|| readString =="led ON"){
digitalWrite(A0,HIGH);
digitalWrite(A1,HIGH);

}
if (readString =="LED off " || readString =="LED OFF" || readString =="led off"
|| readString =="led of" || readString =="LED of" || readString =="led off" ||
readString =="LED OF" || readString =="led OFF"){
digitalWrite(A0,LOW);
digitalWrite(A1,LOW);

}
if (readString =="sound on" || readString =="SOUND ON" || readString =="sound
ON" || readString =="SOUND on"){
tone(buzzerPin, 100);

}
if (readString =="SOUND off " || readString =="SOUND OFF" || readString
=="sound off" || readString =="sound of" || readString =="SOUND of" || readString
=="sound off" || readString =="SOUND OF" || readString =="sound OFF")
{
noTone(buzzerPin);

}
readString="";
}
}

You might also like