I programmed my servo to rotate 90 degrees when an ultra sonic sensor pics up a target. I would like to let the servo motor stay there and if the sensor senses something else, let it move 90 degrees in the opposite direction. Thanks in advance for the help!!
#include <Servo.h>
Servo servo1;
int trigPin = 9;
int echoPin = 8;
long distance;
long duration;
void setup()
{
servo1.attach(7);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);// put your setup code here, to run once:
}
void loop() {
ultra();
servo1.write(0);
if(distance <= 10){
servo1.write(90);
}
}
void ultra(){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
}
You can simply re-work the logic in tutorial/BuiltInExamples/StateChangeDetection but recording a sensor hit instead of a button push, and toggling the position of the servo instead of changing the on/off state of an LED.
Budvar10:
I couldn't resist such a tempting offer, I had to peek.
I think that was the plan.
By the way, @mea-ii, in case you were waiting for a complete solution, people don't usually write or modify code for you here. You should make an effort to do the coding yourself.
I don't know how to reply to each one of you but thanks for the help, i will check that link. And no i am not waiting for someone to write my code i, just uploaded it so people could see what i was talking about. Thank you everyone!
aarg:
Good point, unsigned so it doesn't turn to bad luck when it rolls over.
aarg:
You can simply re-work the logic in tutorial/BuiltInExamples/StateChangeDetection but recording a sensor hit instead of a button push, and toggling the position of the servo instead of changing the on/off state of an LED.
It's the same problem and solution.
Okay i think i found out how out reply, Thanks ill check that out!