Using 2 pushbuttons to rotate a SG90 Micro Servo in different directions [solved]

I have a SG90 servo and I want to hook up 2 push buttons to rotate the servo, so if i press one of the push buttons, lets just say push button on the left (button1) the servo will rotate to the left, same with the other side, if i press the right pushbutton it will rotate to the right, I have written up a code which I THOUGHT would've worked, but now I regret thinking such, because the buttons do not respond to any of my presses, the code is listed below and so is my virtual wiring made on tinkerCAD (I have my setup in real life not just in TinkerCAD) Any ideas? I use a INPUT_PULLUP on both the pushbuttons for your information.

#include <Servo.h>

Servo servo;

const int button1 = 2;
const int button2 = 3;
const int servoPin = 5;

int servoPos = 0;

void setup() {
  servo.attach(5);

  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
}

void loop() {
  if (button1 == LOW) {
    servoPos = servoPos + 10;
  } else {
    
  }

  if (button2 == LOW) {
    servoPos = servoPos - 10;
  } else {
    
  }

  servo.write(servoPos);
}

  if (button1 == LOW)

This does not test the state of button1, it tests the [code]
if (button1 == LOW)

[/code]of button1, which is 2 and never changes

Did you perhaps mean

  if (digitalRead(button1) == LOW)

After changing the if statements to have the digitalRead, now I have another problem, it did work, the servos do respond to my commands now, but... They don't do what I want them to do, in my code it is said whenever a button is pressed it will add to int servoPos, but whenever I press a button, the servo sort of moves like a centimeter to the left, and then returns to its original position, in a matter of a second. But if I spam the button a bit, the button will turn exactly 180 degrees, any idea about this one? Thanks for your response by the way :slight_smile:

One of the unofficial rules of the forum is that when you make a change to a sketch you post the new version in a new post so that we can see what you have done

#include <Servo.h>

Servo servo;

const int button1 = 2;
const int button2 = 3;
const int servoPin = 5;

int servoPos = 0;

void setup() {
  servo.attach(5);

  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(button1) == LOW) {
    servoPos = servoPos + 10;
  } else {
    
  }

  if (digitalRead(button2) == LOW) {
    servoPos = servoPos - 10;
  } else {
    
  }

  servo.write(servoPos);
}

here you go, changed both the if statements, currently, nothing else other than those 2

Have you tried printing the value of servoPos each time you change it ? What range of values do you see ? What range of values does servo.write() take ?

Is this the request? Added new line just before the last function (servo.write) and with this, should I use the serial monitor or something else with it?

#include <Servo.h>

Servo servo;

const int button1 = 2;
const int button2 = 3;
const int servoPin = 5;

int servoPos = 0;

void setup() {
  servo.attach(5);

  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(button1) == LOW) {
    servoPos = servoPos + 10;
  } else {
    
  }

  if (digitalRead(button2) == LOW) {
    servoPos = servoPos - 10;
  } else {
    
  }

  cout << servoPos;
  servo.write(servoPos);
}

Close, but no cigar
The Arduino has no screen to print to. Use this

#include <Servo.h>

Servo servo;

const int button1 = A3;
const int button2 = A2;
const int servoPin = 5;

int servoPos = 0;

void setup()
{
  Serial.begin(115200);
  servo.attach(5);
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(button1) == LOW)
  {
    servoPos = servoPos + 10;
    Serial.println(servoPos);
  }
  if (digitalRead(button2) == LOW)
  {
    servoPos = servoPos - 10;
    Serial.println(servoPos);
  }
  servo.write(servoPos);
}

Once it is uploaded open the Serial monitor and set its baud rate to match that of the Serial.begin() in the code

I uploaded your code and the buttons do not work anymore, they do not move the servo even a little bit, i checked the wires and they sure are secured and connected. The servo is locked up meaning it is recieving electricity, and on the serial monitor, which i opened up, set baud to 115200 and restarted my arduino pressing the built in button, pressing the buttons or even doing nothing printed nothing on the serial monitor, I have my USB to the Arduino cable connected perfectly.

Did you change the button pins to match your project ?

I used A3 and A4 because that is how my test system is set up

Works just fine now! Did not even notice you have changed the ints, everything works fine now, I will configure the script a little bit myself to make the controls more smooth, but thanks!

I am glad it works

You really should add some checks to ensure that the values written to the servo remain in the range that it uses

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.