Need a hand on a "handless" Door lock

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
}
}
}

Hi and welcome.

Please read the forum manual (click !).
It will teach you how to properly ask questions so that they are easier to read, understand and answer by the ones trying to help you.
One of the things you'll learn, is to put code in [code] [/code] tags.

Reading your code is a bit hard to do.
The formatting seems off (but that could be due to the missing tags), your comments are questionable, and you have put code in a comment line.

This is what i'm talking about (snippet):

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;

The 2nd line says { butto... (and so on)
There is nothing wrong with that from the compilers view and it will compile.
I would have put the { on its own line and buttonState on the next.
The IDE understands that and will indent all next lines until it encounters the first }.
That's just a small thing but it will make it much easier for you to read and understand you own code.

This line:

  if (buttonState1 != lastButtonState1) {   // if it is, the buttonState is HIGH:    if (directionState == 0)

Are you sure that "if it is, the buttonState is HIGH" ?
I don't think that is the case.
Then you put the next line directly after it (and repeated it, probably because it didn't work).

What i'm missing is a reset of lastButtonState (either 1 or 2).
So once you have changed it for the first 180 times to 1, it will not change any more.
Oh and by the way, changing it form 0 to 1 once every occurrence would do.
That can be done by placing that line a bit up from where it is now.

The lock takes some 2.7 seconds to open or close if i'm not mistaking, isn't that a bit slow ?
Help yourself, and make sure your comments are correct.
If you copy some code (i't obvious you did), then not only change values and/or variable names, also change the comments on those lines.

These remarks of mine are meant to help you, not to put you off.
Use them to your advantage.

"so whenever i push the button, it will automatically know its position, to either lock or unlock,"

So, if you want to use only one button, the code will know what state the lock was switched to last (and is currently), then the code toggles that state, and sends the signal to the hardware.

Do you want to use one button to toggle the lock, or do you want to use two buttons ( one to lock, the other to unlock) ?

At the moment the code is made to use a lock and an unlock button (on respectively pin 2 and pin 3).
He also stated that he wants to use 2 buttons.
But i agree that the description allows for some confusion.