I really have been trying to read all the command references and have been scanning other folks sketches but I am completely lost.
I feel I am trying to do something fairly simple, yet tweaks I make to Public Sketches fail to work.
//What I am trying to accomplish is this.
Push a button and have that cause a servo to move to a new position.
THEN
After a period of time has passed have the servo move back to the original position.
Ideally, I would like to have one button press be one period of delay, and another button press (different button) be the same servo position change action... but a different delay time.
OR use a single button and the resulting action be based on if a switch is on or off (essentially DigitalRead HIGH or LOW) and when the ActionButton reads that Switch State it then does one of the two actions above.
Once I get that all working I would like to either use a Pot (or other buttons) to be able to step up or down the timer in 100(ms) increments, so that when I do push the ActionButton the timing is either 100(ms) longer than the 5/10(sec) or 100(ms) shorter.
BUT I know I should not ask to be spoon fed here. ;)
I would be very happy if someone could lead me the in the correct path to at least accomplish the simple single push button example described above between the dashed lines.
I would consider it a nice bonus if assistance could also be given to accomplish the steps of more control, but maybe I could figure that out if I could just get the basics figured out.
I apologize if this question is too basic... I have gone thru all the examples I thought would apply...etc etc and am confused.
Wire the switch(es) correctly. Make the pins INPUT. Use the internal pullup resistors unless you like complicated wiring.
Read the state of the switch. Determine if the state changed (there is an example for that). If so, and the change was to pressed, note what time it is and move the servo.
Independently, see if it is time to move the servo back. That depends on the current time, the time the servo was moved, if the servo was moved at all, and how long the servo is supposed to stay in the moved-to position (which depends on which switch was pressed to trigger the move). The blink without delay example bears looking at.
There is nothing particularly challenging about the parts of the project. The whole project looks daunting, until you start breaking it down.
Push a button and have that cause a servo to move to a new position.
THEN
After a period of time has passed have the servo move back to the original position.
//zoomkat servo button test 7-30-2011
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(160);
delay(2000);
servo1.write(20);
}
}
I see the value in PaulS's advice to use a Long to count time passing, but my needs for this project are very basic of just wanting an action to take place on a button push.
I have added a 2nd button (had to do some Googling to get the correct order of things) and I now have the 5 sec 10 sec scenario working. I may not get fancy with this by trying to add IF statements of checking if other buttons have been pushed or POT is moved to alter the delay(). I may just add more buttons. I have plenty more open Digital ports available.
Thanks again! I hope to get more familiar with all this so that at some point I can assist folks as well.