Starting a new Servo project

Thanks Paul,

you will know the switch as been pressed and you can "detach"

What functionality allows me to take the "low" signal from the switch and translate that into initiating the "detach" ? Would an if() statement be the best way to do that or is there something more efficient?

If your if works use it.
Paul

Yes that is normally the way. A digitalRead gets the value of the switch and the if statement does something with that information. Switches should be wired between pin input and ground, an the pinMode should be set for input pull up. When a switch is pressed you will see a LOW otherwise you will see a HIGH.

...still waiting on my switch in the mail.

Does this code look like it is approaching correct?

#include <Servo.h>
Servo myservo;

void setup() {myservo.attach(9);
pinMode(10, INPUT_PULLUP);

}

void loop() {

int switchVal = digitalRead(10);
myservo.writeMicroseconds(1350);

if (switchVal == LOW) {

myservo.detach();

}

delay(3600000);

myservo.attach(9);

}

Thanks in advance.

I found a switch and wired this up and what if am finding is:

  1. Pushing the switch does NOT cause the servo to immediately stop.
  2. If I hold the button down, after a few seconds the servo does stop.
  3. When I release the button the servo starts again.

I will keep experimenting, but if anyone has any recommendations please let me know.

Thanks!

You need to move the servo a tiny bit, test the switch and if not pressed, repeat, until you have moved the servo a total distance that you need.
Paul

No surprise as you have a socking great big delay after only one read.

no surprise as I suspect that delay is not as big as as you posted. Once the big delay ends then your loop function has a chance to read the button again.

Why would you expect anything different, that is how you have written the code.

Use the "blink without delay" example sketch as your starting point. When 1 hour has elapsed, attach the servo and set it running and set up for the next 1 hour interval.

Also use the "state change" example to detect when the microswitch changes from pressed to not pressed (LOW to HIGH) and detach the servo when that state change is detected.

hmm...i am not following. Where would the 8hr delay come in?

The 8 hour delay begins AFTER you have found the limit switch!
Paul

Would a "while" loop be a way to approach this? The "while" loop would run the servo when switchVal == HIGH (button is not pressed) and when switchVal == LOW the code would leave the "while" loop and detach the servo and start an 8hr delay and then when the 8 hours is up there would be an attach and then the code would loop back around and restart the "while" loop.

???

something like this...this compiles, but the button push does not do anything.

#include <Servo.h>
Servo myservo;

void setup() {
myservo.attach(9);
pinMode(10, INPUT_PULLUP);

}

void loop() {
int switchVal = digitalRead(10);
while(switchVal == HIGH)

{myservo.writeMicroseconds(1350);}

myservo.detach();

delay(10000);
myservo.attach(9);

}

This is a classic error:

int switchVal = digitalRead(10);
while(switchVal == HIGH)

{myservo.writeMicroseconds(1350);}

It reads the pin once and if it's high, loops forever because there is no subsequent read of the pin in the loop.

...interesting. Can you suggest an edit to fix?

int switchVal = digitalRead(10);
while(switchVal == HIGH)
  {
  myservo.writeMicroseconds(1350);
  switchVal = digitalRead(10);
  }

you need break up loop sketch first.
loop() → just count time up(No run anything) run New void.
New void() the run Servo and micro switch(run and step). :slightly_smiling_face: