0% found this document useful (0 votes)
31 views

Arduino - Voice - Control - Robot Code

This Arduino sketch controls a voice-controlled robot. It uses an ultrasonic sensor to measure distance and stop the robot if an object is detected within 30 cm. The robot can move forward, backward, left, right, or stop based on voice commands received over Bluetooth. Pins are defined for controlling two DC motors and reading the ultrasonic sensor. Setup and loop functions initialize serial communication and listen for commands to control motor directions accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Arduino - Voice - Control - Robot Code

This Arduino sketch controls a voice-controlled robot. It uses an ultrasonic sensor to measure distance and stop the robot if an object is detected within 30 cm. The robot can move forward, backward, left, right, or stop based on voice commands received over Bluetooth. Pins are defined for controlling two DC motors and reading the ultrasonic sensor. Setup and loop functions initialize serial communication and listen for commands to control motor directions accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*Arduino Voice Control Robot

*Version 1.0

*Created By DIY Builder

*Before Uploading the sketch you must install the required libraries first.

*Unless you'll get compilation error message.

*Also remove the bluetooth and servo connector before uploading the sketch.

*You can contact me here https://www.instagram.com/diy.builder/

*/

String command;

//Right motor

int rightMotorPin1=D5;

int rightMotorPin2=D6;

//Left motor

int leftMotorPin1=D7;

int leftMotorPin2=D8;

#define trigger_pin D1
#define Echo_pin D2

long duration;

int distance;

unsigned long oldtime=0;

void setup() {

Serial.begin(9600);

pinMode(rightMotorPin1, OUTPUT);

pinMode(rightMotorPin2, OUTPUT);

pinMode(leftMotorPin1, OUTPUT);

pinMode(leftMotorPin2, OUTPUT);

pinMode(trigger_pin, OUTPUT); // configure the trigger_pin(D9) as an Output

pinMode(Echo_pin, INPUT); // configure the Echo_pin(D11) as an Inpu

void loop() {

delay(10);

while(Serial.available()) {

command = "";

command = Serial.readString();
Serial.print(command);

if(command == "*move forward#"){

forward();

Serial.println("Forward Entering");

}else if(command == "*move backward#"){

backward();

}else if(command == "*turn left#"){

left();

}else if(command == "*turn right#"){

right();

}else if(command == "*stop it#") {

Stop();

command = "";

if((millis()-oldtime)>1000)

digitalWrite(trigger_pin, LOW); //set trigger signal low for 2us

delayMicroseconds(2);

/*send 10 microsecond pulse to trigger pin of HC-SR04 */

digitalWrite(trigger_pin, HIGH); // make trigger pin active high


delayMicroseconds(10); // wait for 10 microseconds

digitalWrite(trigger_pin, LOW); // make trigger pin active low

/*Measure the Echo output signal duration or pulss width */

duration = pulseIn(Echo_pin, HIGH); // save time duration value in "duration variable

distance= duration*0.034/2; //Convert pulse duration into distance

if(distance<30)

digitalWrite( rightMotorPin1,LOW);

digitalWrite(rightMotorPin2,LOW);

digitalWrite(leftMotorPin1, LOW);

digitalWrite(leftMotorPin2 , LOW);}

oldtime=millis();

void forward()

digitalWrite(rightMotorPin1,HIGH);

digitalWrite(rightMotorPin2,LOW);

digitalWrite(leftMotorPin1,HIGH);
digitalWrite(leftMotorPin2,LOW);

void backward()

digitalWrite(rightMotorPin1,LOW);

digitalWrite(rightMotorPin2,HIGH);

digitalWrite(leftMotorPin1,LOW);

digitalWrite(leftMotorPin2,HIGH);

void right()

digitalWrite(rightMotorPin1,HIGH);

digitalWrite(rightMotorPin2,LOW);

digitalWrite(leftMotorPin1,LOW);

digitalWrite(leftMotorPin2,HIGH);

void left()
{

digitalWrite(rightMotorPin1,LOW);

digitalWrite(rightMotorPin2,HIGH);

digitalWrite(leftMotorPin1,HIGH);

digitalWrite(leftMotorPin2,LOW);

void Stop()

digitalWrite(rightMotorPin1,LOW);

digitalWrite(rightMotorPin2,LOW);

digitalWrite(leftMotorPin1,LOW);

digitalWrite(leftMotorPin2,LOW);

You might also like