I am trying to write a code that will set a servo at a position and then hold said position until such a time as the signal on pin 2 stops, at which point it will spin to a new position. When I run the code below with no signal on pin 2 the servo does not move to the second position,HOWEVER, if I change the if statement to activate on a HIGH signal with nothing plugged into pin 2 the servo does move to its second position. I am using an Arduino Pro Mini (5v, 16mHz). Thanks in advance for any help.
Thank you for the help, I have made the fix (feeling dumb as I do so), but now the servo does not move to the second position whether the if loop checks for LOW or HIGH. Would a boolean work better?
I like to translate my code into English when I am stuck. It helps me get a clearer picture of what's going on.
Yours says:
"If the signal from pin 2 is low move the servo to position 0."
You have the first time the servo is supposed to get into position in the setup which means it will only happen once when the program starts. I would try a sentence like this:
"If the signal from pin 2 is low move the servo to position 0. If the signal from pin 2 is high move it to position 180."
You can do this with two if statements OR you can do it with an if else.
I have added the digitalRead command there and now I am back to square one where the servo only moves to the second position wen the if loop checks for a HIGH signal even tho there is nothing plugged into the pin.
I am attempting to operate a valve via this program so when the program comes on I want the program to place the valve in the closed position and then check for the signal on pin 2 to stop at which point it will open the valve; so I have wanted to avoid any write commands other than the write command to set it and the write command to open it.
spycatcher2k:
Nothing plugged into the pin? Do you not have a pull up/down resistor installed? if not you pin is floating, and will give a random reading.
I connected the input pin in to the GND and now it works perfectly. Thank you guys so much for your help I really appreciate it.