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
}
}
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
}
}
The documentation is online. Read about Servo.write() and constrain().
Did you click on that link by TomGeorge ? To read about the contrain() function ?
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 ?
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
}
}
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 ?
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.