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

Receiver Code

The document contains an Arduino sketch for controlling a robot using Bluetooth and ultrasonic distance measurement. It defines various functions for moving the robot forward, backward, left, right, and stopping based on commands received via Bluetooth. The code also includes setup and loop functions to initialize pins and read distance measurements to prevent collisions.

Uploaded by

sriramaanusha3
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)
21 views3 pages

Receiver Code

The document contains an Arduino sketch for controlling a robot using Bluetooth and ultrasonic distance measurement. It defines various functions for moving the robot forward, backward, left, right, and stopping based on commands received via Bluetooth. The code also includes setup and loop functions to initialize pins and read distance measurements to prevent collisions.

Uploaded by

sriramaanusha3
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

#include <SoftwareSerial.

h>

SoftwareSerial BTSerial(10, 11); // CONNECT BT RX PIN TO ARDUINO 12 PIN | CONNECT


BT TX PIN TO ARDUINO 11 PIN
#include<SPI.h>

int trigPin = 12; // Trig


int echoPin = 13; // Echo
long duration, cm, inches;
int direct;
int l1=3;
int r1=5;
int l2=6;
int r2=9;

int forword_corr;

int a = 0 ;
int s = 3;

int b = 0 ;
int c = 2;

void setup() {
[Link](9600);
[Link](9600);

pinMode(l1, OUTPUT);
pinMode(r1, OUTPUT);
pinMode(l2, OUTPUT);
pinMode(r2, OUTPUT);

digitalWrite(l1, LOW);
digitalWrite(r1, LOW);
digitalWrite(l2, LOW);
digitalWrite(r2, LOW);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {

digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
[Link](cm);
[Link]("cm");
[Link]();
if([Link]()>0){ // Witing for data incoming from the other XBee
module
direct=[Link]();

if(direct == 'B'){
[Link]("Backward");
backward();
}else if (direct=='F'){
[Link]("Forward");
forward();
}else if(direct == 'R'){
[Link]("Right");
left();
}else if(direct == 'L'){
[Link]("left");
right();

}else if(direct == 'r'){


[Link]("rotright");
rotr();

}else if(direct == 'l'){


[Link]("rotleft");
rotl();
}else if (direct == 'S'){
[Link]("Stop");
stopCar();
}

}
}

void forward()
{
if(cm<=15){
digitalWrite(l1, LOW);
digitalWrite(r2, LOW);

}
else{
digitalWrite(l1, HIGH);
digitalWrite(r1, LOW);
digitalWrite(l2, LOW);

digitalWrite(r2, LOW);
delay(2);
digitalWrite(r2, HIGH);
delay(2);
}
}
void backward()
{
digitalWrite(l1, LOW);
digitalWrite(l2, HIGH);
digitalWrite(r2, LOW);
if(b%c == 0){
digitalWrite(r1, HIGH);
}else{
digitalWrite(r1, LOW);
}

if(b > 1000 ) {


b=0;
}
}
void right()
{
digitalWrite(l1, HIGH);
digitalWrite(r1, LOW);
digitalWrite(l2, LOW);
analogWrite(r2, 100);

}
void left()
{
analogWrite(l1, 100);
digitalWrite(r1, LOW);
digitalWrite(l2, LOW);
digitalWrite(r2, HIGH);
}

void stopCar() {
digitalWrite(l1, LOW);

digitalWrite(r1, LOW);
digitalWrite(l2, LOW);
digitalWrite(r2, LOW);
}
void rotr() {

digitalWrite(l1, HIGH);
digitalWrite(r1, LOW);
digitalWrite(l2, HIGH);
digitalWrite(r2, LOW);
}
void rotl(){

digitalWrite(l1, LOW);
digitalWrite(r1, HIGH);
digitalWrite(l2, LOW);
digitalWrite(r2, HIGH);
}

You might also like