I Have a simple push-button with 4 pins and a simple micro servo. I'm using an Arduino nano. I don't have any resistors and don't know if I will need them. What I'm trying to do is this:
When I start the process, the servo starts at 0 degrees
When I press down the button (hold) the servo moves from 0 to 180 degrees, not more
When I let go of the button, the servo moves from 180 degrees back to 0 degrees
I'd love for someone to help me with regards to the code, and the pins to attach my wires to on the arduino. Many thanks...
If you’re starting from scratch, and just need someone to work with, you can try asking to move this across to the Help Needed section… formerly Gigs & Collaboration.
Have you tried the Button example in the IDE ? It turns an LED on/off when a button is pressed/released and could easily be adapted to move a servo one way or the other instead
#include <Servo.h>
const byte ServoPin = 3;
const byte ButtonPin = 4;
Servo servo;
// * When I start the process,
void setup()
{
// the servo starts at 0 degrees
servo.write(0);
servo.attach(ServoPin);
pinMode(ButtonPin, INPUT_PULLUP);
// NOTE: The internal pull-up resistor will keep the
// pin reading HIGH when not connected. The pin
// will read LOW when connected to Ground through
// a closed switch.
}
void loop()
{
// * When I press down the button (hold)
if (digitalRead(ButtonPin) == LOW)
{
// the servo moves from 0 to 180 degrees, not more
servo.write(180);
}
else // * When I let go of the button,
{
// the servo moves from 180 degrees back to 0 degrees
servo.write(0);
}
}
Thank you so much @johnwasser!
Is there a Circuit design you could quickly make so I know what everything looks like and where all the wires go?
Much appreciated...
At the moment it’s just powered by my laptop. But in the future I want to power it with as small as a battery as possible. Possibly a a23 battery. I was also wondering if it’s possible to make a second button spin a mini motor when held down or do I do need a seperate arduino to do that as well. Is it possible to substitute 2 other pins with gnd and 5v? If so then I might be able to use motor on it (arduino) as well as the servo?