Seemingly simple servo operated box lid


i placing 180 degree servo inside wooden box, , lid of box hinged. servo has 2 pushbuttons, 1 open, , 1 close.

the close function, on pin 4, works perfectly. cut off open function , close box, if pressed while opening, want.

the open function, on pin 2, delayed. after closing box, , trying open again, servo wait around 5 seconds execute command.

code: [select]
#include <servo.h>

servo myservo;  // create servo object control servo
// twelve servo objects can created on boards
int pos = 0;    // variable store servo position
int openbutton = 2; //names pin 2 openbutton pin
int closebutton = 4; //names pin 4 closebutton pin
int openbuttonstate = low;
int closebuttonstate = low;


long lastdebouncetime = 0;  // last time output pin toggled
long debouncedelay = 200;    // debounce time; increase if output flickers
 
void setup() {
  pinmode(openbutton, input); // sets pin 2 input
  pinmode(closebutton, input); //sets pin 4 input
  myservo.attach(9);      //gives servo power
}


void loop() {
  // put main code here, run repeatedly:
 
 openbuttonstate = digitalread(openbutton);
 closebuttonstate = digitalread(closebutton);
  if ( (millis() - lastdebouncetime) > debouncedelay) {
 
    if (openbuttonstate == high) {
      myservo.write(0); //use servo open box
      lastdebouncetime = millis(); //set current time
   
    }
   if  (closebuttonstate == high) {
      myservo.write(90); //use servo close box
      lastdebouncetime = millis(); //set current time
    }


}
}


i using 9v battery , 5v regulator power servo, while nano powered through usb(i know 9v batteries aren't enough sustained projects, it's testing purposes.)

the pushbuttons run off of nano's 5v output.

what causing delay? i'm going crazy trying find it...

have got pull-down resistors on input pins stop them floating?

or, easier option use pinmode(---button, input_pullup); activates internal pull-up resistor. must  wire switch pulls pin gnd when pressed , change code low = pressed.

...r


Arduino Forum > Using Arduino > Project Guidance > Seemingly simple servo operated box lid


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino DUE ADC to DAC Piezo 30 Khz-100Khz

Indesign and MathType fonts