Hello, I'm building an open source project called "robocup" . It's an automated water bottle that has a flexible straw that is Servo driven. You press a button and the straw rotates 180 deg, you press the button again and it returns 180 in the opposite direction. So I have all the hard parts done, I need help just getting it to respond to the push button. The designer had the .ino file for the Arduino Nano. I believe I correctly downloaded the "button.ino" file to the Nano but all it does is rotate 180 deg then back continuously, this is what I want, i just want the Servo to go after the button is pressed not continuously.
Draw a schematic of your named devices, showing pin numbers, and connecting pin numbers to pin numbers.
If the author has a schematic, mouse-over the schematic, right-click, "copy image," (return to forum), in the message box right-click "paste," click "reply"
there is an option to have a motion sensor instead of the pushbutton, that why there are three wires. Its my understanding that you use the blue and green wires for the switch
Is there anything in your project or code that keeps the input pin at a known voltage at all times or is it floating at an unknown voltage, maybe HIGH, maybe LOW when the switch is not closed ?
#include <Servo.h>
Servo servo;
const char servoPin = 3; //the pin to connect the servo (fill in according to reality)
const char buttonPin = 0; //the pin to connect the button (fill in according to reality)
const char servoDrinkinAngle = 180; //angle that the servo has to turn to drink (fill in)
const char servoStoredAngle = 0; //angle that the servo has to turn to store the whole thing (fill in)
void setup(){
servo.attach(servoPin);
pinMode(buttonPin, INPUT);
}
void loop(){
delay(digitalRead(buttonPin)); //proceed to the rest of the program if the button is pressed
servo.write(servoDrinkingAngle); //set the servo to the drinking position
delay(!digitalRead(buttonPin); //wait until the button is released
delay(50); //to cancel button bounce
delay(digitalRead(buttonPin); //proceed to the rest of the program if the button is pressed
servo.write(servoStoredAngle); //set the servo to the stored position
delay(!digitalRead(buttonPin); //delay until the button is released
delay(50); //to cancel button bounce
}
It is definitely not the best or most elegant way to do it, but it should work (haven't tested it) and is really simple. If you want to make sure that the servo doesn't smack the person in the wheelchair, add some kind of for-loop or something similar.
I want to be clear, this is one of the most simple solutions, not the best or most effective one.