Hi, first of all, thank you for the answers !!
We have a error posting the code.."Boton" isn´t, and the correct value is "button" (We put in spanish in our code but we translate for this post..)
And I add that none of the three buttons will be pressed simultaneously....
PaulS:
We tried to set up a second button (#2) and servo goes crazy
That seems very unlikely, unless your code or your wiring is completely bogus. Which is it?
if (digitalRead(button) == LOW)
if (digitalRead(button) == HIGH)
Are you planning to add code for the other possible return values for digitalRead(), like CARESSED and TICKLED and...
Every if statement should be followed by curly braces.
Why is there a delay() in one loop, and not in the other?
> -If we press the button on long time, goes to 90º instead of do it on a long and a short press (the servo should go to 90º/45º/25º yes or yes and return to its original position, 45º...)
Are you using some kind of random word generator?
No way with "caressed and tickled" retorn values because the servo must act on a short and a long press (For that I say "yes or yes" unconditionally..)
The wiring is simple and no errors on it (Push button with pull down resistor), and goes crazy because we don´t know how to configure it..
The code works fine with if sentences....
??
The first delay not exist in the original code (Look at the end of this post..)
Some words we check on a translator before we posting.....
Henry_Best:
for(pos = 45; pos < 90; pos += 90)
{
myservo.write(pos);
}
How many times do you expect this for loop to run?
The times that we needed (We want to emulate a shifter of a kart, and the buttons have to work every time that we pressed one of them to up, or down the gear..)
UKHeliBob:
-We tried to set up a second button (#2) and servo goes crazy
The way the program is written at the moment the servo will go to one position when the button is pressed and another when the button is not pressed. You have not posted your attempt at adding a second button, but if you left in the code to do both actions as above then no wonder the servo does not do what you want as it will be trying to do 2 things when the second button is pressed and the first is not.
Please post you program where you added a second button so we can see what is going on and give advice.
Some questions :
How is the servo powered ? If direct from the Arduino then it may be taking more current than the Arduino voltage regulator is designed to provide and could, therefore, damage it.
How are the buttons wired ? Are you using any pull up/down resistors to keep the input button at a known voltage at all times ?
int boton = 4;
Did you really copy this line from the IDE ?
Exact, and there we have the problem (The code for the second button is similar to the first button, and only we change positions, and the servo works crazy because go to many positions, like 45º, 25º and 0º...)
The servo is powered throught 5v arduino but I repeat that this is a prototype (The servo that we want to put is a 15kg of torque and is capable of go from 0º to 90º on 0.5 seconds...Don´t worry because we powered the final servo trought the power supply of the kart..)
The buttons it´s with 1k pull down resistors...
The "boton" is an error......Its "button", sorry....
Robin2:
As others have said there are some detail "shortcomings" in your code.
However the overall picture is pretty close to what is needed
Instead of reading the same button and acting on whether it is high or low each section should test its own button. For example
if (digitalRead(buttonApin) = LOW) { // assumes it reads LOW when button is pressed
// make servo do one thing
}
if (digitalRead(buttonBpin) = LOW) {
// make servo do another thing
}
...R
Yes, sorry for the error (boton)...
Yes, but the servo must be on 45º always (The servo have to go up or down from that position..)
PaulS:
For example
That snippet won't even compile. I can't believe you posted that. Must be tired.
You're right, it was late and I was tired and I apologize for that, but look this:
This is with the correct code and with the first button (From 45º to 90º)..The config works..
Paul, the code is in this post:
http://forum.arduino.cc/index.php?topic=108375.0
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int button = 2; // The button will be on Pin 7
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
digitalWrite (button, LOW);
}
void loop()
{
if (digitalRead(button) == LOW)
for(pos = 0; pos < 90; pos += 90) // goes from 0 degrees to 90 degrees
{ // in steps of degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
// waits 1s for the servo to reach the position
}
if (digitalRead(button) == HIGH)
for(pos = 90; pos>=90; pos-=90) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
}
But we change:
void loop(){
if (digitalRead(button) == LOW)
for(pos = 45; pos < 90; pos += 90)
{
myservo.write(pos);
}
if (digitalRead(button) == HIGH)
for(pos = 90; pos>=90; pos-=45)
{
myservo.write(pos);
delay(50);
}
}
And the code compiles without problems..
Ver Imagen: http://s2.subirimagenes.com/privadas/previo/thump_2254287dibujo.jpg
Robin2:
PaulS:
That snippet won't even compile. I can't believe you posted that. Must be tired.
Strange as it may seem I did not expect it to compile
...R
PaulS:
Strange as it may seem I did not expect it to compile
I hope that OP has the same expectations.
UKHeliBob:
I hope that OP has the same expectations.
Well, as his original code did not compile anyway he is no worse off.
xDDDDD
Soooooooorry guys 8) Yesterday I worked hard and I came home very tired....
Thank you for your support
Regards
(We are trying to make it through arduino because the shifter kit = 1270 USD.....Servo it´s on 120 USD...)