Hello everyone !
I'm new to the Arduino World, and
I have an ongoing project as follows:
Two servo motors connected to an arduino UNO, as well as a self locked push button, the goal of my project is to press the button once, the two servos will rotate from 0 to 100 degrees and will stop. If I press again, I want the servos to rotate in the opposite direction and stop.
A project as described is possible? Do you have any advice for programming the button with servos?
I also wanted to know, I want to power my arduino with a battery.
At first I was advised a 9V for the circuit I described to you, then I read that it could damage the arduino, I am a little confused since... A 9V battery doesn’t seem like a good deal, what would you recommend?
Sorry if these questions seem stupid, I prefer to seek advice before I lay myself completely in my projects..
Don't take any more advice from that person. 9V batteries (assuming you mean PP3 size, it's hard to find any other type of 9V battery today) are good for smoke alarms, not Arduino projects, especially anything involving a motor.
I would recommend 4xAA batteries in a holder to give 5~6V (depending if you use primary batteries or rechargeable like NiMH).
Does that mean that the button remains pressed with the contacts closed until it is pressed again ?
If so, then all you want is for the servo to move to 100 degrees whilst the button is pressed and to 0 degrees when it is pressed again to release it
If the 9V battery is a PP3 then forget about using it to power your project. It is safe to use 9V to power the Arduino but that battery cannot supply enough current for long enough to be useful
You should also seriously consider not powering the servo from the Arduino 5V and GND pins as the current it requires could damage the Arduino
You have made the code much more complicated than it needs to be for the type of button that you are using, ie it locks on when pressed
Try this
Read the state of the input pin
if the button is currently pressed write 100 to the servo
else
write 0 to the servo
The for loops do not move the servo slowly because of how they are written so you might just as well move the servo with a single write command
How is the input wired ?
I suggest that you use INPUT_PULLUP in pinMode() and wire the button to take the pin LOW when pressed. Then if the pin reads LOW it is pressed