Obstacle Sensing Robot Using Arduino
Code:
#include <Servo.h>
int ePin=9;
int tPin=8;
int pzoPin=10;
float distance;
long duration;
int servoPin = 11;
Servo Servo1;
int LED=7;
void setup(){
pinMode(ePin, INPUT);
pinMode(tPin, OUTPUT);
Serial.begin(9600);
pinMode(pzoPin, OUTPUT);
Servo1.attach(servoPin);
pinMode(LED, OUTPUT);
}
void loop(){
Servo1.write(0);
digitalWrite(LED, HIGH);
delay(70);
digitalWrite(LED, LOW);
delay(2000);
digitalWrite(tPin, LOW);
delay(100);
digitalWrite(tPin, HIGH);
delay(100);
digitalWrite(tPin, LOW);
duration = pulseIn(ePin, HIGH);
distance=duration/29/2;
Serial.println("Distance: ");
Serial.println(distance);
delay(2);
if(distance<=52){
tone(pzoPin, 0, 0);
delay(100);
tone(pzoPin, 1000, 500);
delay(1000);
Servo1.write(0);
delay(1000);
Servo1.write(90);
delay(1000);
Servo1.write(0);
delay(1000);
}
}
-------------------------------------------------------------------------------------
Circuit Diagram
You Can See It Virtually Working @Tinkercad
#include <Servo.h>
int ePin=9;
int tPin=8;
int pzoPin=10;
float distance;
long duration;
int servoPin = 11;
Servo Servo1;
int LED=7;
void setup(){
pinMode(ePin, INPUT);
pinMode(tPin, OUTPUT);
Serial.begin(9600);
pinMode(pzoPin, OUTPUT);
Servo1.attach(servoPin);
pinMode(LED, OUTPUT);
}
void loop(){
Servo1.write(0);
digitalWrite(LED, HIGH);
delay(70);
digitalWrite(LED, LOW);
delay(2000);
digitalWrite(tPin, LOW);
delay(100);
digitalWrite(tPin, HIGH);
delay(100);
digitalWrite(tPin, LOW);
duration = pulseIn(ePin, HIGH);
distance=duration/29/2;
Serial.println("Distance: ");
Serial.println(distance);
delay(2);
if(distance<=52){
tone(pzoPin, 0, 0);
delay(100);
tone(pzoPin, 1000, 500);
delay(1000);
Servo1.write(0);
delay(1000);
Servo1.write(90);
delay(1000);
Servo1.write(0);
delay(1000);
}
}
-------------------------------------------------------------------------------------
Circuit Diagram
You Can See It Virtually Working @Tinkercad
Nice
ReplyDelete