I have a button on pin 3
I have a servo on pin 9
(unused sensor at pin A0)
When button is pressed the servo moves up (bridge open)
When button is depresses servo moves down (bridge close)
However for anny wierd reason, when the bridge is open the arduino sometimes reads the button wrong, The button is wired so it gives 5V to the Arduino, but the Arduino reads the pin sometimes as "LOW"
Its like it cant read the button annymore after the servo moved???
What am I doing wrong? is is this just a bug??
And how to fix it? anyone anny ideas?
Specs
Arduino uno Rev3
Software V1.0.5-r2
Arduino code below
#include <Servo.h>
Servo myservo;
int Button = 0; //The current button position (Up and Down)
int CurrentServoPos = 0; //The current servo position
int OpenClose = 0; //The state of the bridge
int OldOpenClose = 0; //is to apply it once when changed
void setup() {
pinMode(3, INPUT);
myservo.attach(10);
Serial.begin(9600);
}
void loop() {
Button = digitalRead(3);
if (Button > 0) {
OpenClose = 0;
Serial.println("Button OFF ");
}
else if (Button < 1) {
OpenClose = 1;
Serial.println("Burron ON ");
}
//Write open and close to the serv0
if (OpenClose == 1 && OldOpenClose == 0) {
OldOpenClose = 1;
for(CurrentServoPos = 90; CurrentServoPos >= 1; CurrentServoPos -= 1) {
myservo.write(CurrentServoPos);
delay(50);
}
}
else if (OpenClose == 0 && OldOpenClose == 1){
OldOpenClose = 0;
for(CurrentServoPos = 0; CurrentServoPos < 90; CurrentServoPos += 1) {
myservo.write(CurrentServoPos);
delay(50);
}
}
delay(1); // delay in between reads for stability
}
You will not be able to read the button state for 4.5 seconds whilst the servo moves.
Your program does exactly what it should. Press the button and the servo moves one way, release it and it moves the other. If you hold the button down the servo stays at one end of its travel and if you release it the servo stays at the other end of its travel.
You will not be able to read the button state for 4.5 seconds whilst the servo moves.
Your program does exactly what it should. Press the button and the servo moves one way, release it and it moves the other. If you hold the button down the servo stays at one end of its travel and if you release it the servo stays at the other end of its travel.
What do you want it to do ?
the problem is, I press the button. servo moves.
(still pressing it) but in a random time the Arduino things I am not pressing it. and let the servo moves back (while im still pressing it)
I can't make any sense of that statement or of the picture.
The simplest way to wire a switch is to connect one leg to ground and the other leg to a digital pin. Then, set the mode to INPUT_PULLUP. No external resistors required, which your picture doesn't have anyway.
Wired this way, the pin never floats, and is HIGH when the switch is not pressed and LOW when it is pressed.
I suspect that you are experiencing a floating pin, where the pin is NOT at a defined state when it is not pressed.