Arduino Uno + MG955 servo + Shadow switch push button

Hi guys

I’ve just acquired myself an Arduino Uno board, a MG955 servo, and a shadow switch push button.

I’ve managed to get the board and servo to work together, wiring them as following:

Black/brown to GND

Red to 5V

Yellow to 10

I’ve run some random code I’ve found and the motor seems to work just fine.

Now I am a total newbie at this so I really don’t know my next step. I’ve tried googling the solution, but I don’t seem to be able to find anything that guides you on how to use a shadow switch along with the Arduino and a servo.

I want it to be able to open and close something with the switch.

How do I go about it?

I’ve added pictures of the different things, so you guys can see what I’m on about, in case i make no sense.

Any and all help, much appreciated! Thank you!

Edit:
Something like this, but with the button in between :slight_smile:

You can execute the first (opening) loop when the button is pressed, and the second (closing) loop when the button is released.

wire the switch to a not-currently-used pin and to ground such that the pin is connected to ground when the button is pushed. best to not use pins 0 or 1, or obviously the one in used for the servo

put this statement between the for-loops in loop()
while(HIGH == digitalRead(pin_number);

put this statement in setup()
pinMode(pin_number, INPUT_PULLUP);

wg0z:
wire the switch to a not-currently-used pin and to ground such that the pin is connected to ground when the button is pushed. best to not use pins 0 or 1, or obviously the one in used for the servo

put this statement between the for-loops in loop()
while(HIGH == digitalRead(pin_number);

put this statement in setup()
pinMode(pin_number, INPUT_PULLUP);

wg0z:
wire the switch to a not-currently-used pin and to ground such that the pin is connected to ground when the button is pushed. best to not use pins 0 or 1, or obviously the one in used for the servo

put this statement between the for-loops in loop()
while(HIGH == digitalRead(pin_number);

put this statement in setup()
pinMode(pin_number, INPUT_PULLUP);

Thanks for the reply :slight_smile:

But I can’t seem to get it to work.

I’m using this code with your suggestion:


#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
pinMode(10, INPUT_PULLUP);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
while(HIGH == digitalRead(10);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}


Sketch gives me the following error:

C:\x\x\x\Arduino\sketch_dec30a\sketch_dec30a.ino: In function ‘void loop()’:

sketch_dec30a:23: error: expected ‘)’ before ‘;’ token

while(HIGH == digitalRead(10);

^

exit status 1
expected ‘)’ before ‘;’ token

What am I doing wrong? :o

you have unbalanced parentheses. add a ')'

LOL, that did the trick..

But it seems the code is not good.

Now when I press the button, it starts the loop bu going 180 deg. one way, then 180 deg. the other way - running this loop until I bush the button again.

Is there anyway I can, when I push the botton once, make it do the 180 deg. then stop, then do 180. again after I push the button again?

sure. just duplicate the while after the first for-loop.
I guess maybe your button is push for on, push again for off.
you need to generally keep it in the off/open circuit position.

I did it like this, seems to work:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
pinMode(10, INPUT_PULLUP);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
while(HIGH == digitalRead(10));
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
while(LOW == digitalRead(10));
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

Odd thing is though, the servo seems to be constantly running, vibrating a little bit, is that normal?

you're probably trying to drive the servo too far.
try keeping pos in the range 30..150.

wg0z:
you're probably trying to drive the servo too far.
try keeping pos in the range 30..150.

That seems to have done it!

Thank a lot for the help mate! :grin: