if you help me you'll get 10 years of good luck

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

How can you guarantee we get 10 years of good luck.

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.

I couldn't resist such a tempting offer, I had to peek.

Budvar10:
I couldn't resist such a tempting offer, I had to peek.

I think that was the plan. :slight_smile:

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 want a legally-binding definition of "good luck" before I commit

At least we weren't threatened with 10 years of bad luck if we don't help…

a7

larryd:
How can you guarantee we get 10 years of good luck.

Luck = unsigned long 10 years ;

Maybe he has wooden nickels with an Indian on one side and the words YEARS OF GOOD LUCK
Of which you will get 10 ?

dave-in-nj:
Luck = unsigned long 10 years ;

Good point, unsigned so it doesn't turn to bad luck when it rolls over.

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!

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