#include <Servo.h>
const int servoPin = 3;
Servo servo;
int angle =0;
int angle1 =180;
int val = 0;
int trigPin = 11; // Trigger
int echoPin = 12; // Echo
long duration, cm;
int ledPin = 2;
int trigPin1 = 8; // Trigger
int echoPin1 = 9; // Echo
long duration1, cm1;
void setup() {
//Serial Port begin
Serial.begin (9600);
// Servo motor
servo.attach(servoPin);
// LED
pinMode(ledPin, OUTPUT);
// Sensor 1
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Sensor 2
//Define inputs and outputs
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
}
void loop() {
// ============= sensor 1 ==========================
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(10);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// Convert the time into a distance
cm = (duration/2) / 29.1;
Serial.print("servi: ");
Serial.print(cm);// Divide by 29.1 or multiply by 0.0343
Serial.print("angle: ");
Serial.println(angle);
if(cm < 30 ){
for( angle ; angle <=180; angle++){
servo.write(angle);
delay(20);
}
delay(2000);
}
angle = 0;
Serial.print("angle1: ");
Serial.println(angle);;
//
if(cm > 30){
for( angle1; angle1 >=0; angle1--){
servo.write(angle1);
delay(20);
}
delay(2000);
}
angle1=180;
Serial.print("angle2: ");
Serial.println(angle);
// }
/* if(cm< 20 && cm >0 && angle ==0){
servo.write(180);
angle = 180;
delay(15); } */
// }
//delay(250);
// ======================= sensor2 ================================
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin1, INPUT);
duration = pulseIn(echoPin1, HIGH);
// Convert the time into a distance
cm1 = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
Serial.print(" sensor_2: ");
Serial.print(cm1);
Serial.println();
if(cm1 < 10 && cm1 >= 0){
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
}
if(cm1 > 10){
digitalWrite(ledPin, LOW);
servo.write(0);
}
delay(250);
}
in this experiment i used servo motor nd sonar sensor... the code will be doing when sonar distance is less than 30 cm then servo motor rotate 0 to 180 degree.. and when sonar sensor distance is greater than 30 cm degree then servo motor rotate in reverse order 180 to 0 degree.............. but problem comes in servo motor when i run the code and the distance is less then 30 cm then motor rotate continuously 0 to 180 and 180 to 0.... its same as when distance is greater than 30 cm.... when i run for() loop without if condition, servo motor worked properly but when i used for() loop with sonar sensor distance ... the servo motor did not worked properly