0% found this document useful (0 votes)
14 views2 pages

Arduino Robot Control Code

The document contains an Arduino code that controls a robot using serial commands. It sets up pins for motor control and reads commands like 'forward', 'backward', 'left', 'right', and 'stop' to move the robot accordingly. The code includes a loop that continuously checks for available serial data and executes the corresponding motor actions based on the received command.

Uploaded by

king.raiders0745
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)
14 views2 pages

Arduino Robot Control Code

The document contains an Arduino code that controls a robot using serial commands. It sets up pins for motor control and reads commands like 'forward', 'backward', 'left', 'right', and 'stop' to move the robot accordingly. The code includes a loop that continuously checks for available serial data and executes the corresponding motor actions based on the received command.

Uploaded by

king.raiders0745
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

String readdata;

void setup() {
[Link](9600);
pinMode(3, OUTPUT); // connect to input 1 of l293d
pinMode(4, OUTPUT); // connect to input 4 of l293d
pinMode(5, OUTPUT); // connect to input 3 of l293d
pinMode(6, OUTPUT); // connect to input 2 of l293d

}
//-----------------------------------------------------------------------//
void loop() {
while ([Link]()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = [Link](); //Conduct a serial read
readdata += c; //build the string- "forward", "reverse", "left" and "right"
}
if ([Link]() > 0) {
[Link](readdata); // print data to serial monitor
// if data received as forward move robot forward
if(readdata == "forward")
{
digitalWrite(3, HIGH);
digitalWrite (4, HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
delay(100);
}
// if data received as reverse move robot reverse

else if(readdata == "backward")


{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6,HIGH);
delay(100);
}
// if data received as right turn robot to right direction.
else if (readdata == "right")
{
digitalWrite (3,HIGH);
digitalWrite (4,LOW);
digitalWrite (5,LOW);
digitalWrite (6,LOW);
delay (100);

}
// if data received as left turn robot to left direction
else if ( readdata == "left")
{
digitalWrite (3, LOW);
digitalWrite (4, HIGH);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
delay (100);
}
// if data received as stop, halt the robot

else if (readdata == "stop")


{
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
delay (100);
}

readdata="";}} //Reset the variable

You might also like