So for a school project I want to build an auto tying shoe and I want 2 modes, mode 1 is cassual in which one servo goes to 130, and the other 150 so its meduim tight, then mode 2 is running mode which is one servo at 160 one at 180. Both modes have a button one "buttont" and one "button". Right now when I press one button it makes the servos randomly go back and forth about 10 degrees, the other button does nothing. Why is this?
Thanks
Clayton
i_luv_arduino:
Why is this?
Because there is something wrong with the code.
If you want help you need to post the code so we can see what you can see.
...R
sorry i forgot to add it
[code]
#include <Servo.h>
Servo myservo;
Servo myservot;
int pos = 0;
int post = 0;
const int button = 2;
const int buttont = 3;
int state = 0;
int statet = 0;
void setup() {
myservo.attach(9);
myservot.attach(10);
pinMode(button, INPUT);
pinMode(buttont, INPUT);
}
void loop() {
state = digitalRead(button);
statet = digitalRead(buttont);
if (state == HIGH) {
myservo.write(130);
myservot.write(150);
delay(100);
}
else {
myservo.write(0);
myservot.write(0);
}
state = digitalRead(button);
statet = digitalRead(buttont);
if (statet == HIGH) {
myservo.write(160);
myservot.write(180);
delay(100);
}
else {
myservo.write(0);
myservot.write(0);
}
}
[/code]
Random motion of servos is almost always because of inadequate power supplies. You need a separate regulated 5 to 6 volt power supply, capable of supplying 1 ampere per servo. Connect the Arduino and servo power supply grounds together.
Right now when I press one button it makes the servos randomly go back and forth about 10 degrees, the other button does nothing. Why is this?
Try making the delay 1000 instead of 100 and see if it works better. A servo can't move much in .1 second.
Thanks this helped alot. I just have 2 little questions:
1: Why do they make a fast clicking sound. Is it broken? It still works fine. Its only one though...
2: Why do they randomly stop sometimes, wait, then go on. And when they pause pin 13 goes high...?
Now there not working anymore...
Looking at the code in Reply #2, you seem to have the same code twice within loop() with only a small variation. Don't do that. Take out the duplicate and use variable to keep track of the servo position. When you press the button the value of the variable should change.
Something like this (not complete, not tested)
void loop() {
state = digitalRead(button);
statet = digitalRead(buttont);
if (state == HIGH) {
servoPos = 130;
servoPosT = 150;
}
else if (stateT == HIGH) {
servoPos = 160;
servoPosT = 180;
}
else {
servoPos = 0;
servoPosT = 0;
}
myservo.write(servoPos);
myservot.write(servoPosT);
delay(200);
}
Organizing the code like this should make it easier to see the logic as well as avoiding the risk of copying errors
...R
i_luv_arduino:
Now there not working anymore...
Try a simpler program such as the servo Sweep from the IDE examples using the same pin and power supply as your program. Does it work ?
Thanks for the code robin, but now its not even downloading. Its an ardweeny with ftdi, it was working earlier...
Im just going to keep it simple with one button, thanks for all the help.
Lets say I wanted to do this, you step in the shoe, push a button on one side, it messures your weight and adjusts the servos accordingly, then when you hit the button again it makes the servos go back to 0. I have the weight code figured out, that code is here
#include <Servo.h>
Servo myservo;
const int button = 2;
int pos;
int pin = 0; // pin the sensor and 10k pulldown resistor
int force; // the analog reading from the FSR resistor divider
int state = 0;
void setup()
{
Serial.begin(9600);
myservo.attach(9);
pinMode(button, INPUT); // pin the servo is connected to
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (state == HIGH) {
force = analogRead(pin); // Reads the FSR
Serial.print("Analog reading = ");
Serial.println(force); // This will print the raw force value
pos = map(force, 0, 1023, 0, 179); // Scales the force reading to degrees for servo control
Serial.print("Adjusted reading = ");
Serial.println(pos); // This will print the adjusted servo reading (an angle)
myservo.write(pos); // Write the new angle to the servo
delay(200); // Delay 150 milliseconds before taking another reading
if (state == LOW) {
myservo.write(0);
}
}
}
by the way im just working with one servo for now.
Thanks in advance.
From Reply #11
I have the weight code figured out, that code is here
Is there a question?
...R
Robin I just wanted to know how to do that... because if I said if (button == high) it will just do the code when the button is HELD not just pressed. Also if you hit the button again it moves servo to 0. How would I do that? Im thinking if (button == HIGH) then in that if (button == LOW) my code then if (button == HIGH) to exit that loop. Do you think that will work? Ive tried to test it but its kind of glitching.
if I said if (button == high) it will just do the code when the button is HELD not just pressed.
Look at the StateChangeDetection example in the IDE to see how to do something when a button BECOMES pressed rather than when it IS pressed.
i_luv_arduino:
Robin I just wanted to know how to do that... because if I said if (button == high) it will just do the code when the button is HELD not just pressed. Also if you hit the button again it moves servo to 0. How would I do that? Im thinking if (button == HIGH) then in that if (button == LOW) my code then if (button == HIGH) to exit that loop. Do you think that will work? Ive tried to test it but its kind of glitching.
What @UKHeliBob has said is important, probably essential.
I suspect you are describing your requirement in a way that may make sense to a human but you are forgetting that a computer needs much more finely detailed instructions.
Have you studied the code I suggested in Reply #7. Keeping track of the servo position is keeping track of the state of the system.
...R
Thanks everyone!
Thanks everyone!