door lock with Arduino and a servo

hi doing this code but want to change the servos going the other way and that the time before he locks again getting longer?

/*
 Doorsystem lock controller sketch
 Language: Arduino
 
 This sketch will open the door if it receives a signal from the
 processing sketch that handles the RFID part. There are also two
 buttons that allow the user to open the door (from the inside) and
 to disconnect the servo for a short periode of time (from the outside)
 so you can open the door with a key.
 
 created 23 March 2013
 by Tjalling Tolle
 
 uses the Servo library by Arduino
*/

#include <Servo.h> 

const int buttonPin = 2;     // the number of the pushbutton pin
const int powerPin = 4;     // the number of the pushbutton pin
int currentAngle = 0;
int requiredAngle = 0;
Servo myservo;  // create servo object to control a servo 
int buttonState = 0;         // variable for reading the pushbutton status
int timer = 0;

void setup() { 
  pinMode(buttonPin, INPUT);     
  pinMode(powerPin, OUTPUT);
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo.write(currentAngle);
} 

void loop() { 
  if(timer>0) {
    requiredAngle=189;
    timer--;
  }
  else {
    requiredAngle=0;
  }
  
  if(currentAngle==requiredAngle) {
    myservo.detach();
  }
  else {
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
    if(currentAngle<requiredAngle) {
      currentAngle++;
    }
    else {
      currentAngle--;
    }
    Serial.print(currentAngle);
    Serial.print("-");
    Serial.println(requiredAngle);
  }
  myservo.write(currentAngle);
  
  if (Serial.available() > 0) {
    char in = (char) Serial.read();
    if(in=='Y') {
      timer=29999;
    }
  }
  
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
     timer=29999;
  } 
}

but want to change the servos going the other way

Servo.write(180 - thePos);

(Assuming a 180 degree servo)

and that the time before he locks again getting longer?

Assign a different value to timer. You'll probably have to change the type of timer to long.

Why are you detaching and reattaching the servo? That has no impact on the amount of energy it uses.

and that the time before he locks again getting longer?

The way that it is done at the moment is madness because loop() will run so fast that the value of timer will get to zero in practically no time at all despite the waste of time pointed out by Paul.

The answer is, of course, use the BlinkWithoutDelay principle. Save a start time then check each time through loop() whether the desired interval has elapsed and if it has, act on it. If not, go round again until it has or until user input is used to terminate the wait prematurely.

okay i have a rfid access control. but no / nc and I want an Arduino with a servo attached to the lock. it will only go a half laps left and a delay and tbx. how do I do it most easily?

not me who wrote the code. and are a beginner :roll_eyes:

how do I do it most easily?

If the servo starts at 0, and rotates to 180(ish) to unlock:

    lockServo.write(180);
    delay(howeverLongYouWant);
    lockServo.write(0);

If the servo starts at 180, and rotates to 0 to unlock, swap the first and third lines.

how do I get it together with (rfid access control)?

how do I get it together with (rfid access control)?

Since I have no idea how you are getting data from your RFID, or what RFID reader you are using, I don't know.

I have a no / nc output from it. thought I should have 2 cables from arduidon to (rf id) gnd and any appropriate input. so when arduino get a singnal then servos go a half turn so that it unlocks the door then so should you catch in the door on close (1 min) then I want it to rotate tbx and lock.

PaulS:

how do I do it most easily?

If the servo starts at 0, and rotates to 180(ish) to unlock:

    lockServo.write(180);

delay(howeverLongYouWant);
    lockServo.write(0);




If the servo starts at 180, and rotates to 0 to unlock, swap the first and third lines.

Falls off chair in amazement !

PaulS used the delay() command in a program. :slight_smile: Whatever next, code on the same line as an opening brace ?
As requested, though, it is the easiest way to do it, if not the best.

how do I add that he just moves on singnal into arduinon?

#include <Servo.h>
Servo myservo;
int pos = 180;
void setup() {
 myservo.attach(9);
  myservo.write(0);
    delay(20000);
    myservo.write(180); // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}