Hello everyone, I want to control servos with 2 buttons when i push one of them servo rotates right and when i push other button servo rotates left, heres my code: #include <LiquidCrystal.h> #include <Servo.h>
The second is that the servo object doesn't take negative values.
So instead of HIGH it should be LOW?
When i make simple program with servo and write negative values it worked perfectly servo rotated left....
The problem is that when i push buttons nothing happens...
else (digitalRead(buttonleft) == HIGH);What is that supposed to do ? It is not testing the state of buttonleft which is presumably what you want it to do.
myservo.write(5);
myservo.write(-5);
These 2 lines are presumably supposed to move the servo in increments of 5 either way. However, msyservo.write() takes an absolute position as its argument and not an increment from where the servo currently is. You need to read the buttons and if pressed update a position variable and use that as the argument to myservo.write() making sure that the value stays in bounds which -5 is not by the way.
else (digitalRead(buttonleft) == HIGH);What is that supposed to do ? It is not testing the state of buttonleft which is presumably what you want it to do.
How should I change it to test the state of button? Sorry I'm just 3 day with arduino so I don't know a lot of things ...
myservo.write(5);
myservo.write(-5);
These 2 lines are presumably supposed to move the servo in increments of 5 either way. However, msyservo.write() takes an absolute position as its argument and not an increment from where the servo currently is. You need to read the buttons and if pressed update a position variable and use that as the argument to myservo.write() making sure that the value stays in bounds which -5 is not by the way.
If I put it this way?
pos=pos+5;
myservo.write(pos);
Closer, but still no cigar.
You have no bounds checking on the value of pos. Its value should stay between 0 and 180.
Also, compare if (digitalRead(buttonleft) == HIGH);and if (digitalRead(buttonright) == HIGH)Spot the difference.
Have you got any pulldown resistors on the button pins to stop their voltage floating and providing spurious inputs ?
As written, holding down a button will rapidly cause the servo to move one way or the other (once the code is fixed, of course). Is that what you want or should the servo only move a little each time a button becomes pressed and not move any further until it is pressed again ?
Well the code isn't still working. Now I'm just trying to do that servo rotates from a push, but nothing happens... Later I will improve this code, but for now it would be nice to make it move from a push...
#include <Servo.h>
// constants won't change. They're used here to
// set pin numbers:
Servo myservo;
const int buttonPin1 = 2;
const int buttonPin2 = 3;
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
int pos = 0;
void setup() {
myservo.attach(13);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
pos=pos+5;
myservo.write(pos);
delay(500);
}
if (buttonState2 == HIGH) {
pos=pos-5;
myservo.write(pos);
delay(500);
}
}
It works when I plug in only one button and do not write second if statement in code, but when I add second button nothing happens.... Where could be the problem?
It works when I plug in only one button and do not write second if statement in code, but when I add second button nothing happens.... Where could be the problem?
I've never found buttons very useful, except to keep a shirt from flapping open. I prefer to use switches. When using switches, I prefer to use the internal pullup resistors, and connect one leg to ground and one leg to the digital pin.
I prefer to use the state change detection example, and do something when the switch BECOMES pressed, not IS pressed.
If you are going to use buttons, and not use the internal pullup resistors, you need to tell us how the "buttons" and external resistors are wired. And why.
I added one led and kept two buttons now the code is:
#include <Servo.h>
// constants won't change. They're used here to
// set pin numbers:
Servo myservo;
const int buttonPin1 = 2;
const int buttonPin2 = 3;
const int ledPin = 13;
// variables will change:
int buttonState1 = 0;
int buttonState2 = 0;
int pos = 0;
void setup() {
myservo.attach(9);
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
pos=pos+5;
myservo.write(pos);
digitalWrite(ledPin, HIGH);
delay(500);
}
if (buttonState2 == HIGH) {
// turn LED on:
pos=pos-5;
myservo.write(pos);
digitalWrite(ledPin, LOW);
delay(500);
}
}
LED works fine in this case- first button turns it on, and the second turns off led.... Soon i will upload photo where you can see how everything is connected...
A wiring diagram is much better than a photo. Draw a diagram and post a photo of it.
It looks like the servo is powered from the Arduino 5v pin. That often causes problems because the pin can't provide enough current. Much better to give the servo a separate power supply with a common ground connection to the Arduino.