Hello. I am trying to set up a circuit that will detect someone approach to <=x distance from hc-sr04, then retreat to (x+some_number), THEN trigger servo movement, (maybe move after waiting a few seconds). then start it all over waiting for someone to approach.
arduino uno
TIA!
here is what i have so far. can someone give me some direction?
#include <NewPing.h>
#include <Servo.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 200
// NewPing setup of pins and maximum distance
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo; //create servo object to control a servo
int pos = 0; //variable to store the servo position
void setup() {
myservo.attach(9); //attaches the servo on pin 9 to the servo object
}
void loop()
delay(50);
unsigned int distance = sonar.ping_cm();
// MORE CODE HERE???!!!
{
for (pos = 0; pos <= 180; pos += 1;) { //goes from 0 to 180 degrees in sweeps of 1 degree
myservo.write(pos); //tell servo to go to position in variable pos
delay(15); //waits 15ms for servo to reach the position
}
}
To understand what you are creating, start by printing the return distance on the IDE screen using Serial.print(). Then you can move towad the sensor and away and see the values on the screen.
Once you get that working and understand how quickly the values can change, or how slowly they change, then you can decide how to know if the object is moving toward the sensor or away, and the length of time you want to have in your test.
Finally, you can use all that information to code up the decision making necessary for you final version of the project.
You can't just make up the final version of such a program without knowing all the possible variations.
PAul
I've already done that before I came here for help. I have basically put it in to as close to correct code as I know. If someone could give me some direction to what I should use as far as "if then else" or what, that would be fantastic. That is where i' m stuck. Does that make sense?
You forgot the '{' that starts the body of the loop() function.
The steps you say you want to do are:
- detect someone approach to <=x distance
- detect someone retreat to (x+some_number)
- (maybe waiting a few seconds).
- THEN servo movement,
- then start it all over waiting for someone to approach.
You need the value for 'x'. I recommend calling it something more meaningful, like ApproachDistance:
const int ApproachDistance = 100; // 100cm
You could use an offset, like 'some_number' but it would be just as easy to set the retreat distance:
const int RetreatDistance = 150; // 150cm
Then you can use a 'while' loop to wait until the distances are reached:
while (sonar.ping_cm() > ApproachDistance) /* Do Nothing */ ;
Serial.println("Approach Detected");
while (sonar.ping_cm() < RetreatDistance) /* Do Nothing */ ;
Serial.println("Retreat Detected");
Then put in whatever delay you like, and then the servo sweep, which you already have.
johnwasser THIS THIS THIS. Thank you! I'll give this a try later today. Yeah, I just described the scenario with algebra so anyone trying to help would maybe better understand what I was trying to do.
Once you get the fundamentals working, then you may want to see if you can select only those responses that you want from all the other possible responses. Like turning around instead of retreating, etc.
Paul
johnwasser IT WORKS!
#include <NewPing.h>
#include <Servo.h>
#define TRIGGER_PIN 11
#define ECHO_PIN 12
#define MAX_DISTANCE 200
// NewPing setup of pins and maximum distance
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo; //create servo object to control a servo
int pos = 0; //variable to store the servo position
const int ApproachDistance = 60; // 60cm
const int RetreatDistance = 190; // 190cm
void setup() {
myservo.attach(9); //attaches the servo on pin 9 to the servo object
}
void loop() {
delay(50);
unsigned int distance = sonar.ping_cm();
while (sonar.ping_cm() > ApproachDistance) /* Do Nothing */ ;
Serial.println("Approach Detected");
while (sonar.ping_cm() < RetreatDistance) /* Do Nothing */ ;
Serial.println("Retreat Detected");
delay(9000); // waits for 9 seconds
for (pos = 0; pos <= 180; pos += 1); {//goes from 0 to 180 degrees in sweeps of 1 degree
myservo.write(pos); //tell servo to go to position in variable pos
delay(15); //waits 15ms for servo to reach the position
}
}