Help with servo and infrared

Hi

It’ two days that i’m tryng to wite a sketch to obtain this result:

I have a continuos rotation servo
i have a infrared sensor
and a push button

i would like to obtain this effect
i push button:

the servo start to run infinitely
until the infrared give a high signal

if infrared is high then stop the servo to rotate.

il tooks easy but in my sketch the servo run just for on eloop and the stop

thanks inadvance

sparapalle_tipo_1.ino (3.86 KB)

btw the infrares is a tcrt 5000 on digital input like the attached file http://www.ebay.it/itm/Infrared-reflective-Photoelectric-Switch-IR-Barrier-Line-Track-sensor-TCRT5000L-/301474786928?pt=UK_BOI_Industrial_Automation_Control_ET&var=&hash=item46314c2e70

The logic should be fairly simple - like this pseudo code

void loop() {
   IRstate = read IR sensor;
   btnState = digitalRead(btnPin);
   if (btnState == LOW && IRstate == OFF) {
      myServo.writeMicroseconds(1250); // choose a number for a suitable speed
   }
   if (IRstate == ON) {
     myServo.writeMicroseconds(1500); // or whatever value causes the servo to stop
   }
}

Using servo.writeMicroseconds() gives you finer control.

I assume the button pin uses INPUT_PULLUP so that LOW = button pushed I don't know whether the IR device gives HIGH or LOW for on so I have just used OFF and ON

...R

thanks Robert for your help i' ll let you know my progress.

ciao

thanks again Robert , it runs exactly as i expected and it so simple.... :P

jodeld18: thanks again Robert , it runs exactly as i expected and it so simple.... :P

Glad it works. Please note that it is ROBIN

...R

opps Sorry Robin