Servo motor delay trouble

Hi, so I'm making a useless box where the timing of two different servo motors is very important.

When running the code it works fine initially, but after flipping the switch a few times both servos are inconsistent about starting.

when the switch is flipped it will do one of four things.

  1. it will react immediately, working correctly, flipping the switch back and go back in place
  2. it will react immediately, but stops after it flips the switch for a few second then goes back
  3. it will have a delayed reaction before it starts to flip the switch then goes back in to place correctly
  4. Sometime both motors work separately too when they should not.

Only the first one option should be running consistently. I am not sure what is wrong with it but here is my code below. If anyone has any suggestions it would be greatly appreciated.

#include <Servo.h>

int tswitch = 2;
int servo1 = 7;
int servo2 = 6;

Servo Door;
Servo Arm;

void setup() 
{
  Door.attach(servo1);
  Arm.attach(servo2);

  pinMode(tswitch, INPUT_PULLUP);
  digitalWrite(tswitch, HIGH);

   Door.write(110);//110//startin positions
   Arm.write(90);///40
}

void loop() 
{
  if(digitalRead(tswitch) == LOW)/////Low == ON
  {
    simpleClose();
  }
}


void simpleClose()
{
  Door.write(90);
  Arm.write(150);
  delay(100);
  Door.write(110);
  Arm.write(90);
}
 

Post images of your project with wiring and power source.

Try adding a few serial prints to see what's going on with the thing.

See this discussion: includes how to, schematics and software.

thank you Idahowalker for your assistance. I believe it was a combination of not having the

"bool qwertyuiop = digitalRead(tswitch);"

and

a lack of delays between the .write portion

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.