Ultrasonic and while loop

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);
}

Just incorporate/use the data from the ultrasonic sensor in the while head of the loop.

How? I cant figure out how to read the data from the sensor in the loop
I've tried this:

while (a>60){

analogRead(a);

while (a>60){

analogRead(a)

You have more than 60 analogue inputs ? :o

Nope that's a failed attempt to read the distance from the sensor inside the while loop

60 is distance in cm

So what is the analogRead supposed to be reading?

seank199:
How? I cant figure out how to read the data from the sensor in the loop
I've tried this:

while (a>60){

analogRead(a);

What's wrong with that? New values to a, like a <= 60, will make the exit of the loop.

TheMemberFormerlyKnownAsAWOL:

while (a>60){

analogRead(a)


You have more than 60 analogue inputs ? :o

That's a good one! Random key pressing when coding....

TheMemberFormerlyKnownAsAWOL:
So what is the analogRead supposed to be reading?

I've used analogRead incorrectly in an attempt to read the distance mate

Maybe use the construction You've already used:

  a = sr04.Distance();

Not understanding what analogRead means! :roll_eyes:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.