Servo Not Responding on toggle switch

An earlier reply from AWOL here, showed concern at the fact that everything in your code expects servos to react and then arrive at their commanded positions instantaneously. It's very likely (although I haven't hooked up a rig with a servo and some switches to test) that you are commanding the servo to to "a" and immediately telling it to go to "b" because of the speediness of loop().

Put some delay()s in there, to give the servo a chance to get where it's supposed to go, before you change its mind for it next time through loop().

@z00mj, please do not cross-post. Threads merged.

Sorry Coding Badly, definitely had no idea that they can merged. I'm just new here and want to learn things.

I already put delay neiklot but nothing really happened and I guess the reason why my servo doesn't respond to the toggle switch is because the potentiometer holds it. But obviously, I don't have any solutions too.

Put

 delay(1000);

at the end of loop(), perhaps it helps.

Let's get the cart back behind the horse where it belongs: what are you actually trying to do? That is, what's your actual objective, what's the application?

Walk us through a day in the life (or a minute in the life) of this contraption you have: what's its start up state, what do you do, what is supposed to happen, what do you do next, what's supposed to happen when you do that.

(Btw are they switches that stay on or off, or buttons that go open when released?.... and now that I ask that, makes me wonder, if they are buttons, are you expecting a "one time" movement from where the servo last was, each time you press?)

neiklot

What I'm trying to do is to set the servo in a desired angle that I want(with the use of potentiometer) and then control the direction of the servo by the use of toggle switch(SPDT) (Left, Center, Right).

This is just the start of my project. This hardware that I'm building tests the capabilities of a servo, changes the desired position(from potentio), then control it by the toggle switch, and I want to control the PWM and the rotation of the servo. The pot and toggle switch are just the start, after that I'll move on to the next part.

and btw, thank you DrDiettrich, the delay at the end of the loop() works. But it still twitch a little bit after I move the toggle switch.

b.jpg

z00mj:
and btw, thank you DrDiettrich, the delay at the end of the loop() works.

Ok well that takes us back 2 days or so when Awol suggested you add some delays to allow the servo time to move...

Sorry for that. I'm thinking hard for two days how I can make it worked but I'm not successful doing it. Can I use millis() instead of delays for this?

I think that it's time to learn a bit more. Choose what you want to learn now:

  • how to use variables
  • how to debounce a switch
  • how to use millis() instead of delay()

z00mj:
Can I use millis() instead of delays for this?

Probably, but tbh I'm still not 100% clear what you're trying to do.

But there's no harm in using delay() in the right circumstances, and this may be one of those right circumstances. It's a fact that a servo takes a certain time to get from p to q, so there's no point countermanding the write() before it gets there.

A delay() is ok for that provided the blocking nature of delay() doesn't screw up the rest of the program; and only you know what's coming next:

This is just the start of my project.

If you're planning on (say) having this test thing run 5 servos at once, then I would be inclined to think that servo movements ought to be sliced up so each servo gets the nod to move a degree at a time under millis() control.

I don't know much about debounce and millis. And I want to learn more about programming.

neiklot, this is a project given by my professor(we don't have the same topics given), he wants me to make a servo tester that angle can be adjusted by the pot, turn it to other directions by the use of spdt, change its pulse width, change the convention of rotation(clockwise/counter clockwise).

z00mj:
turn it to other directions by the use of spdt
...
change the convention of rotation(clockwise/counter clockwise).

Aren't those the same thing?

z00mj:
change its pulse width

Well the library does that, or do you mean you have to do that from first principles?

z00mj:
this is a project given by my professor

Ok then we need to be careful not to give you all the answers or we will dilute the value of your degree :wink: but I would rethink that abs() stuff if I were you. If the objective is just to stop the servo trying to go past the end stops, just check for the position getting too low (if going down) or too high (if going up). Just write the minimum or maximum value using an if, so the servo effectively sits at whichever end it's at until you move it away with the next pot movement or toggle.

I mean control the servos direction by spdt, and change its convention by some means. Servo moves from 0-180°, I will change it from 180-0°

Which "convention"?

A servo does not move by itself, you make it move by your code. Do you know how to move the servo from 0 to 180°?

If so, then you should know already how to make it move from 180° to 0.
If not, you have not yet understood the function and use of a servo.