I'm having trouble Controlling Servo With a button. Need Help

This is my first project (besides Blinky) and I need to control a servo with a push button. When the button is pushed, the servo should turn 45 degrees and when I leave the button, it should return to its original position.

servoPin = 9
buttonPin = 8

Please, I need help!

Also, please take note that the colour of the wires in the diagram have no meaning and are random to be differentiable.

AnotherFinal.ino (417 Bytes)

Image from Original Post so we don’t have to download it. See this Simple Image Guide

Diagram (Rain Cover).png


Sorry but that image is completely unintelligible with all the wires on top of each other. Don't bother with Fritzing. Just make a simple pencil drawing and post a photo of it.

If you use the internal pullup resistor with
pinMode (buttonPin, INPUT_PULLUP);
then you just need to connect your switch between the pin and GND. Note that it show LOW when pressed.

This is a simplified version of your program

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    pos = 45;
  else  {
    pos = 0;
  myservo.write (pos);


Thanks a ton! Really helped me out! Thanks for your time!