I am attaching a toggle switch to an servo motor. I want the servo to rotate from 0 to 90 and back to 0 only once when the toggle is switched on. Right now the servo rotates continuously when switched on. I have tried working with a boolean statement but have not had much success. Any guidance would be appreciated, thank you.
is buttonstate working properly?
is the hardware dumb? It looks like like 32 could make it jitter forever.
perhaps that section should be read the position and if its not at the start, move there, else do nothing?
it also looks like that section is moot: you moved back to start position anyway, no need to keep doing that. Remove that if statement and block (32+) ?
other stuff... does it really accept degrees? Most code works in radians, but it could, I guess.
Not a bad suggestion. It seems to be working just fine despite that section. When it is off it is still. When it is on it is moving constantly, I need it to execute the rotation only one time. That is really what I am aiming at.
I get it.
it could be any of 4 things or more...
- the button isnt working as you think (or wrong button bug etc), so its always high (or low even). can you debug or output this value in any way? If you can't see the value, hard code it instead of getting it from the hardware, so that it sits at low for like 1000 iterations, goes high for 10, then low forever. Or make the button do something else like fire up a light.
- something odd with the motor. what does it do if you take out both write statements? It should sit there 'off'. Then you can play with just that, so if they hit the button, do nothing, whatever experiments to debug with.
- exteme / band issue. Maybe you tell it to go to the start position but its not quite getting there, so it keeps trying. What if you say myServo.write(startPosition+5); for low, and in the return loop for high as well?
- units ... try going 0-3 and back, unless you confirmed that its in degrees?
- hardware problems. Maybe you wired the motor to the power supply and it just spins, ignoring pins and inputs and anything else... maybe you have a bad board or connection or whatever... it happens.
- something bugged up in code you didn't post.
basically, it looks right but something clearly isnt.
The Arduino (or whatever) microcontroller responds in the loop() which 'circulates' continually, to the connected outputs from the peripherals which in this case is the button, and in turn, depending on the state of the button, sends the relevant output to the servo, and when that is complete 'resets' the button state.
I have made those adjustments but still not achieving one rotation. The loop seems to be resetting the i value to zero each iteration of the for loop as long as the toggle is HIGH. Maybe the toggle switch is not ideal, and I need a button instead. Or there is a work around?
The loop seems to be resetting the i value to zero each iteration of the for loop as long as the toggle is HIGH. Maybe the toggle switch is not ideal, and I need a button instead. Or there is a work around?
Try something like this. Clearly I didn't test or even compile the program.