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().
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.
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?)
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.
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?
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.
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 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.