Hi,
I'm very new to all of this and I'm trying to figure out how to break out of a while loop using an ultrasonic sensor.
I'm using a servo motor to scan an area when distance>60 and wish to move it to pos130 once distance<60. If someone can help with this I'd greatly appreciate it!
#include <Servo.h>
#include <LiquidCrystal.h> //LCD
#include "SR04.h" //Ultrasonic
const char TRIG_PIN = 4;
const char ECHO_PIN = 3;
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // initialize the library with the numbers of the interface pins
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
pinMode(TRIG_PIN , OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(ECHO_PIN, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600);
myservo.attach(5); // attaches the servo on pin 5 to the servo object
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
}
void loop() {
a=sr04.Distance();
while (a>60){
analogRead(a);
lcd.setCursor(0,0);
lcd.print(" SYSTEM ARMED ");
//delay(20);
if (pos < 180) {
pos++; // = pos +=1;
myservo.write(pos);
delay(40);
}
else {
if (pos < 360) { // at 180 degrees start moving bask
pos++;
myservo.write(360-pos);
delay(40);
}
else{ // when you reach 360 reset to zero and start all over again.
pos = 0;
}
} //close else
} //close while
pos = 130;
myservo.write(pos);
delay(20);
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
//(note: line 1 is the second row, since counting begins with 0):
lcd.print("INTRUDER ALERT! "); // Prints string "Distance" on the LCD
Serial.print("Distance: ");
Serial.println(a);
delay(50);
}