My code for is not working?

The servo motor is not being activated when I press the 2nd and 3rd button what is the problem?

#include<Servo.h> //include the servo library
int BUTTON1 = 5; //pin of the first button
int BUTTON2 = 4; //pin of the second button
int BUTTON3 = 3; //pin of the third button
int BUTTON4 = 2; //pin of the fourth button
int servoPin = 9; // Servo pin
Servo servo; //create a servo object
int pos = 0; //initial position of the servo
void setup() {
// put your setup code here, to run once:
//pin used by the servo
servo.attach(9);
pinMode(BUTTON1, INPUT_PULLUP); //define first button as input pullup
pinMode(BUTTON2, INPUT_PULLUP); //define second button as input pullup
pinMode(BUTTON3, INPUT_PULLUP); //define third button as input pullup
pinMode(BUTTON4, INPUT_PULLUP); //define fourth button as input pullup

}

void loop() {

if ((digitalRead(BUTTON2) == LOW) && (digitalRead(BUTTON3) == LOW)) { //if Value read of the button ==LOW:
pos++; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
if ((digitalRead(BUTTON1) == LOW) && (digitalRead(BUTTON4) == LOW)) { //if Value read of the button ==LOW:
pos--; //decreases the value of the "pos" variable each time the push button of the right is pressed
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
}

Hi, @jonathan678
Welcome to the forum.

Please post your code in code tags in a new post.

What are each of the buttons supposed to do?
What model Arduino are you using?
How are you powering your servos?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:
PS, Before posting press [ CTRL ] [ T ] in the IDE to format your code to make it easier to read.

Hi,
You need to put constraints on your "pos" value so it goes no lower than 0 and no higher than 180.

sensVal = constrain(sensVal, 10, 150);  // limits range of sensor values to between 10 and 150

Tom.. :smiley: :+1: :coffee: :australia:

I'm using an arduino uno with the servo motor plugged into a breadboard and the uno.
Are you sure that I have put constraints on the "pos" value can you elaborate more on that.

#include<Servo.h> //include the servo library
int BUTTON1 = 5; //pin of the first button
int BUTTON2 = 4; //pin of the second button
int BUTTON3 = 3; //pin of the third button
int BUTTON4 = 2; //pin of the fourth button
int servoPin = 9; // Servo pin
Servo servo; //create a servo object
int pos = 0; //initial position of the servo
void setup() {
// put your setup code here, to run once:
//pin used by the servo
servo.attach(9);
pinMode(BUTTON1, INPUT_PULLUP); //define first button as input pullup
pinMode(BUTTON2, INPUT_PULLUP); //define second button as input pullup
pinMode(BUTTON3, INPUT_PULLUP); //define third button as input pullup
pinMode(BUTTON4, INPUT_PULLUP); //define fourth button as input pullup

}

void loop() {

if ((digitalRead(BUTTON2) == LOW) && (digitalRead(BUTTON3) == LOW)) { //if Value read of the button ==LOW:
pos++; //increases the value of the "pos" variable each time the push button of the left is pressed
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
if ((digitalRead(BUTTON1) == LOW) && (digitalRead(BUTTON4) == LOW)) { //if Value read of the button ==LOW:
pos--; //decreases the value of the "pos" variable each time the push button of the right is pressed
delay(5); //5 milliseconds of delay
servo.write(pos); //servo goes to variable pos
}
}







pos must be in the range of 0-180

Welcome to the forum.

The documentation is online. Read about Servo.write() and constrain().
Did you click on that link by TomGeorge ? To read about the contrain() function ?

The Servo.write() is here : Servo - write() - Arduino Reference
There you can read that the angle must be from 0-180.

Suppose I take your sketch, then do what the others wrote, then put it in Wokwi simulation, then you get this:

The real question is: Why does your servo motor not work ?
Even a small servo motor can require a peak current of 500mA. Breadboards have bad contacts and jumper wires can be broken.
Do you have a multimeter ?

Hi

Was an example.

pos= constrain(pos, 0, 180);

Place this after each of your

pos++

and

pos--

It will stop your pos going out of range.
Put some Serial.print statements in to help you debug your code with the IDE monitor.

Tom :smiley: :+1: :coffee: :australia:

Next time you post code, use the auto-format function in the IDE to indent and verify your code structure.

You’ll get more friends, and more help.
Good work with the code-tags

It still doesn't work the way I want it to even though I made changes in the code.

The view in tinkercad.

What way is it working.
Can you post your new code in a new post please?

Can you please post some images of your project?

Thanks,, Tom... :smiley: :+1: :coffee: :australia:

The project is exactly like the tinkercad that I posted. You check out the tinkercad version which also has the code.

#include<Servo.h> //include the servo library
int BUTTON1 = 5;   //pin of the first button
int BUTTON2 = 4;  //pin of the second button
int BUTTON3 = 3;  //pin of the third button
int BUTTON4 = 2;  //pin of the fourth button
int servoPin = 9;  // Servo pin
Servo servo; //create a servo object
int pos= constrain(pos, 0, 180);  //initial position of the servo
void setup() {
  // put your setup code here, to run once:
  //pin used by the servo
  servo.attach(9);
  pinMode(BUTTON1, INPUT_PULLUP);  //define first button as input pullup
  pinMode(BUTTON2, INPUT_PULLUP);  //define second button as input pullup
  pinMode(BUTTON3, INPUT_PULLUP);  //define third button as input pullup
  pinMode(BUTTON4, INPUT_PULLUP);  //define fourth button as input pullup
  Serial.print(pos);
}

void loop() {

if ((digitalRead(BUTTON2) == LOW) && (digitalRead(BUTTON3) == LOW)) { //if Value read of the button ==LOW:
    pos++; 
    pos= constrain(pos, 0, 180);  //increases the value of the "pos" variable each time the push button of the left is pressed
    delay(5); //5 milliseconds of delay
    servo.write(pos); //servo goes to variable pos
  }
  if ((digitalRead(BUTTON1) == LOW) && (digitalRead(BUTTON4) == LOW)) { //if Value read of the button ==LOW:
    pos--; 
    pos= constrain(pos, 0, 180);  //decreases the value of the "pos" variable each time the push button of the right is pressed
    delay(5); //5 milliseconds of delay
    servo.write(pos); //servo goes to variable pos
  }
}

Hi,
We need to see your project layout to check if your protoboard does not have these breaks in the side bus.
protobreak

Do you have a DMM?

Tom... :smiley: :+1: :coffee: :australia:

Thanks for the photo, I think that is okay.

Can you try to make two test sketches ?
One that moves the servo motor with delays and another that reads the buttons and shows them on the Serial Monitor.

You really need a multimeter (some call it a DMM, but in my country it is a multimeter).

The link for the sketch is editable above, where the simulation with the diagram and code is also available for you to try. But I don't think adding delays or not would make a notable change.

That is a "share" link. I think that everyone can now alter your Tinkercad project. Did you make a copy for yourself ?
There might be a "make public" link, but I don't know how.

I already have your project working in the Wokwi simulator. Have you tried that link ?

I can't.
I don't have an account.

Hi,
Do you have a DMM?
The reason is, I don't think you have a good connection between the button legs and the protoboard sockets.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

1 Like

You seem to be using the internal pull-up resistors. You are ALSO using external resistors. Are those pull-up resistors or pull-down resistors? It looks like they are pull-down resistors which are fighting with the internal pull-up resistors.

You should eliminate the external resistors and wire the buttons between the Arduino pins and Ground.

It still doesn't work. Here's a top view.