Hi everyone!
I am working on a keepsake box that uses a mini servo to lock the door
(minimum security obviously but this is more like a prop for a proposal)
The sketch I have created works in a general sense, but I need a little
guidance.
Button 2 (which is closed when the door is closed and returns the servo to
the locked position) is depressed when the circuit is powered up (because
the door is closed at the beginning of the cycle) and as a result, keeps
the ARDUINO from responding from button 1 (which will be a high signal
from a source external to the ARDUINO) which opens the lock. I also want
to cycle the LOCKED / UNLOCKED leds using PWM but not sure where to
park the additional code. I will include my sketch, any help in at least finding
the answer would be awesome.
Thanks!
George
// DAC box servo program REV4
// By: George Suprenant
// With help from the ARDUINO community
// 2012
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 20; // variable to store the 1st servo position LOCKED
int pos1 = 120; // Variable to store the 2nd servo position UNLOCKED
int buttonState1 = 0; // The initial state of the DAC button is OFF
int buttonState2 = 0; // The initial state of the DOOR button is OFF
const int btn1 = 2; // The nubmer of the DAC pin
const int led1 = 3; // The number of the locked led pin ~
const int btn2 = 4; // The number of the Door switch pin
const int led2 = 5; // The number of the unlocked led pin ~
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
void setup()
{
pinMode(btn1, INPUT); // Set btn1 as an INPUT
pinMode(led1, OUTPUT); // Set led1 as an OUTPUT
pinMode(btn2, INPUT); // Set btn2 as an INPUT
pinMode(led2, OUTPUT); // Set led2 as an OUTPUT
myservo.attach(9); // Attaches the servo on pin 9 to the servo object
myservo.write(pos); // Physicaly sets the servo to the LOCKED position (had problems with servo twitching 30+ deg on startup)
digitalWrite(led1, HIGH); // Turns on the LOCKED led
}
void loop()
{
buttonState1 = digitalRead(btn1); // Look at the DAC pin
buttonState2 = digitalRead(btn2); // Look at the door switch
if (buttonState1 == HIGH) { // If DAC pin is HIGH then,
myservo.write(pos1); // Move servo to the UNLOCKED position
digitalWrite(led1, LOW); // Turn the LOCKED led OFF
digitalWrite(led2, HIGH); // Turn the UNLOCKED led on
delay (2000); // Wait for 2 sec to prevent door switch bounce
} else if (buttonState2 == HIGH) { // But if the DOOR CLOSED switch is HIGH
delay (2000); // Wait for 2 sec to prevent lock damage and make sure door is closed
myservo.write(pos); // Move servo to LOCKED position
delay (500); // Wait 1/2 sec before door locked led going HIGH
digitalWrite(led2, LOW); // Set door unlocked led LOW
digitalWrite(led1, HIGH); // Set door locked led HIGH
}
}