I'm having a problem setting angle on a servo. What I want to do is have 2 buttons control the servo moving it to a set angle left or right then reset to center when nothing is being pressed.
It seems simple but I have no idea how to control the servo's angle I looked and looked and cant seem to find any info or any code to help me understand what I need to do. Any ideas or code examples I would love to have.
That should not be hard if you take it in easy steps
Start with the simple button tutorial (examples/digital/button) , when you have that working with an LED you can modify it to call the servo function to move to an angle:
myservo.write(0); // tell the servo to move to the 0 degree position
and when the button is released, instead of turning the LED off, call the function to center the servo.:
myservo.write(90); // center the servo
When you have the working you can add a second button similar to the first but when this is pressed you write the value to move the servo to another angle.
Thanks guys "myservo.write(0); " was the code I was looking for.
I edited the button code and it works fine for 1 button but when I add button 2 it goes all crazy like its trying to move both ways at the same time. I'm sure its some thing simple I'm still pretty new to the arduino but spending 2 hrs poking at code with no joy I thinks its better to just ask.
What I want is to move 2 servos with 4 buttons like
Button one moves servo 1 right servo 2 left /
Button 2 moves servo 1 left servo 2 right \ /
button 3 moves servo 1 left servo 2 left \
button 4 moves servo 1 right servo 2 right / /
When no button pushed they moves back to center I also want to add a master command so when input 5 is triggered it will go back to center and wait till input 5 is stopped.
You can see my really crappy code I used for testing any tips you can give me why its not working I'm sure its some thing really stupid and simple...
#include <Servo.h>
Servo myservo;
const int up = 1; // the number of the pushbutton pin
const int down = 2;
// variables will change:
int pressup; // variable for reading the pushbutton status
int pressdown; // variable for reading the pushbutton status
void setup() {
myservo.attach(8);
pinMode(up, INPUT);
pinMode(down, INPUT);
}
void loop(){
// read the state of the pushbutton value:
pressup = digitalRead(up);
pressdown = digitalRead(down);
if (pressup == HIGH) {
// Servo move
myservo.write(35);
delay(10);
}
else {
// Servo off
myservo.write(0);
}
if (pressdown == HIGH) {
// Servo move
myservo.write(120);
delay(10);
}
else {
// Servo off
myservo.write(0);
}
}
If you press the up button, the code in yellow below executes each time through the loop:
void loop(){
[glow] // read the state of the pushbutton value:
pressup = digitalRead(up);
pressdown = digitalRead(down);
if (pressup == HIGH) {
// Servo move
myservo.write(35);[/glow] [glow]<=======
delay(10);
}[/glow]
else {
// Servo off
myservo.write(0);
}
[glow] if (pressdown == HIGH)[/glow] {
// Servo move
myservo.write(120);
delay(10);
}
[glow] else {
// Servo off
myservo.write(0);[/glow] [glow]<=======
}[/glow]
}
The <======= indicates where you cause the servo to move both ways each time through the loop. Similar logic happens when you press the down button.
You need to remove the myservo.write(0); code from both places where it is, and only do that part if both the up button and the down button are not pressed. See && - Arduino Reference if you are not sure about how to do that.
I still get the same problem of the servo twitching left and right. Even when no buttons are pressed. I did try pull down resistors as the Arduino button tutorial says with still the same problem of twitching untill i comment out the "if back = high" command
I really think Im missing some thing major in the code like the reading of the push buttons
#include <Servo.h>
Servo myservo1;
// Servo myservo2;
const int Up = 1; // the number of the button
const int Down = 2; // the number of the button
// const int Left = 3; // the number of the button
// const int Right = 4; // the number of the button
// const int Shutdown = 5; // Shutdown input pin
// variables will change:
int Foward = 0; // variable for reading the pushbutton status
int Back = 0; // variable for reading the pushbutton status
// int Tunrleft ; // variable for reading the pushbutton status
// int Turnright ; // variable for reading the pushbutton status
// int Detect ; // variable for reading the pushbutton status
void setup() {
myservo1.attach(8); // Left servo
// myservo12.attach(7); // Right servo
pinMode(Up, INPUT);
pinMode(Down, INPUT);
}
void loop(){
// read the state of the pushbutton value:
Foward = digitalRead(Up);
Back = digitalRead(Down);
if (Foward == HIGH) {
// Servo move
myservo1.write(45);
//myservo2.write(145);
}
if (Back == HIGH) {
// Servo move
myservo1.write(5);
//myservo2.write(180);
}
// Servo move to center
if (digitalRead(Foward) == LOW && digitalRead(Back) == LOW) { // read two switches
myservo1.write(0);
}
}
Also, you already read the values of your two buttons into the variables Forward and Back, at the top of the loop(). So there are two things wrong with the test to do the myservo1.write(0): you shouldn't be reading the buttons again, and you shouldn't be using Forward and Back as buttons. Just test Forward and Back directly, they still have the values you read into them at the top of the loop.
god it's really no fun being a noob
You are close, don't give up. A couple of days to get something like this sorted out is not bad for a first-timer.