Hi guys,
i am really new to arduino, didnt have any programming/computing background at all,
really fascinated with all the automation you can do with it, so i just bought a uno last week and decided to give it a go,
my project was a simple servo door lock with two buttons ( lock n unlock ), so whenever i push the button, it will automatically know its position, to either lock or unlock, however i got stuck when i try to combine couple of codes together, i believe it could have been something wrong with the old and new pos? .
Any Help would be very very appreciated.
Heres how the codes go,
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin = 13; // the number of the LED pin
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int lastButtonState1 = 0;
int lastButtonState2 = 0;
int directionState = 0;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
{
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
}
void loop()
{ buttonState1 = digitalRead(buttonPin1); // check if the pushbutton is pressed.
buttonState2 = digitalRead(buttonPin2); // check if the pushbutton is pressed.
if (buttonState1 != lastButtonState1) { // if it is, the buttonState is HIGH: if (directionState == 0)
if (directionState == 0)
digitalWrite(ledPin, HIGH); // turn LED on:
directionState = 1;
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
lastButtonState1 = buttonState1;
delay(15); // waits 15ms for the servo to reach the position
}
}
else if (buttonState2 != lastButtonState2) { // if it is, the buttonState is HIGH: if (directionState == 0)
if (directionState == 1)
digitalWrite(ledPin, LOW); // turn LED on:
directionState = 0;
for(pos = 180; pos >= 1; pos -= 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
lastButtonState2 = buttonState2;
delay(15); // waits 15ms for the servo to reach the position
}
}
}