I understand it's not resetting delaymove back at any point, I need to figure out how to do it still.
What I want is when the switch is set to high, nothing happens for 4 seconds, after those 4 seconds, the servo moves up (I have it set to 250 so that it will move until it hits the switch, instead of having it possibly stop prematurely), hits the switch, and immediately moves back to position 40 (through trial and error this is found to be the all- the- way- back- in point.).
What is happening is when the switch is set to high (by the user), the servo IS waiting 4 seconds (desired), when it hits the switch and resets it to low, it waits another 4 seconds (not desired)
I commented the code out and simplified it some.
#include <Servo.h> //set up the library
Servo useless; // set the servo
int switchpin = 2; //initialize variables
int switchpos = 0;
int moveto = 40;
int delaymove = 1;
void setup()
{
pinMode(switchpin, INPUT);
useless.attach(3); //attach the servo
}
void loop()
{
switchpos = digitalRead(switchpin); //read the switch
if (switchpos == HIGH) //if the box has been turned on
{
delay(4000); //wait
useless.write(250); // move the servo farther than requred to
//prevent premature stopping
}
if (switchpos == LOW) //if the servo has turned the box back off
{
useless.write(40); //move the servo back inside the box
}
}