If you're not familiar with the useless machine, youtube it, it's quite hilarious.
I've built my own useless machine but I'm having a bit of a problem delaying the servo, I want the program to wait about 4 seconds after the switch is turned on before it's turned off.
The code I have will wait 4 seconds, but it only works once (modified to do so) because what was happening was the servo was waiting 4 seconds, moving, hitting the switch to off, waiting another 4 seconds, and then going back.
I honestly don't understand why it's doing this.
#include <Servo.h>
Servo useless;
int switchpin = 2;
int switchpos = 0;
int moveto = 40;
int delaymove = 1;
void setup()
{
pinMode(switchpin, INPUT);
useless.attach(3);
}
void loop()
{
switchpos = digitalRead(switchpin);
if (switchpos == HIGH)
{
moveto=1;
}
if (switchpos == LOW)
{
moveto=0;
}
if (moveto == 0 )
{
useless.write(40);
}
if (moveto == 1)
{
if (delaymove == 1)
{
delay(4000);
delaymove = 0;
}
useless.write(250);
}
}
I'm confused - clearly if you want it to repeat you'll have to reset delaymove back to 1 at some point (the code only clears it).
Normally these machines switch themselves off so why would you want to repeat anyway? If you annotated the servo values to explain what action that angle corresponded to it would be clearer.
Now if you want to respond to the input by delaying, moving the servo, delaying and moving it back, that's simply something like:
void loop ()
{
if (digitalRead(switchpin) == HIGH) // or whatever value you meant
{
delay(4000);
useless.write(250);
delay (4000) ;
useless.write(40);
}
}
If you want to be able to recognise other events during that process you should look at the technique used in blinkWithoutDelay example.
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
}
}
void loop ()
{
if (digitalRead(switchpin) == HIGH) // or whatever value you meant
{
delay(4000);
useless.write(250);
while (digitalRead(switchpin) == LOW) // I rather guessing what your switch input means...
{}
useless.write(40);
}
}