Changing direction of servo motor lock attached to arduino

Hi, I have attached a lock bolt to a servo motor to act as a door lock. I unlock and lock the door lock using an adafruit fingerprint sensor connected to an arduino uno board.

The problem I have is that the code I have for the servo motor only moves the lock backwards (unlocking the door) which works perfectly. However, I do not know how to program the code to make the servo motor change direction the next time I scan my fingerprint (to lock the door).

Someone please help. Here is my code.

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Servo.h> //Add servo library

int getFingerprintIDez();

Servo servo1; //Define servo name / object

#define servoPin 9 //Define pin number to which servo motor is connected
#define durationTime 15000 //Define the time it remains in the open position of the door lock (miliseconds)
#define servoMin 360 //Open position
#define servoMax 90 // Closed position


SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);


void setup()  
{
  while (!Serial);  // For Yun/Leo/Micro/Zero/...
  
  Serial.begin(9600);
  Serial.println("Adafruit finger detect test");

  servo1.attach(servoPin); //Define pin number of the servo
  servo1.write(servoMax); //The position of the servo at the start of the program

  // set the data rate for the sensor serial port
  finger.begin(57600);
  
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1);
  }
  Serial.println("Waiting for valid finger...");
}

void loop()                     // run over and over again
{
  getFingerprintIDez();
  delay(50);            //don't ned to run this at full speed.
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK success!

  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  
  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }   
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence); 
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;

  servo1.write(servoMin); //If the fingerprint is correct open the door lock
  delay(durationTime); //Keep the lock open for the defined duration
  servo1.write(servoMax); //take the lock OFF again
  
  
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  return finger.fingerID; 
}

Hi, I have attached a lock bolt to a servo motor to act as a door lock. I unlock and lock the door lock using an adafruit fingerprint sensor connected to an arduino uno board.

The problem I have is that the code I have for the servo motor only moves the lock backwards (unlocking the door) which works perfectly. However, I do not know how to program the code to make the servo motor change direction the next time I scan my fingerprint (to lock the door).

Someone please help with the code to achieve this. Here is my code.

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Servo.h> //Add servo library

int getFingerprintIDez();

Servo servo1; //Define servo name / object

#define servoPin 9 //Define pin number to which servo motor is connected
#define durationTime 15000 //Define the time it remains in the open position of the door lock (miliseconds)
#define servoMin 360 //Open position
#define servoMax 90 // Closed position


SoftwareSerial mySerial(2, 3);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);


void setup() 
{
 while (!Serial);  // For Yun/Leo/Micro/Zero/...

 Serial.begin(9600);
 Serial.println("Adafruit finger detect test");

 servo1.attach(servoPin); //Define pin number of the servo
 servo1.write(servoMax); //The position of the servo at the start of the program

 // set the data rate for the sensor serial port
 finger.begin(57600);

 if (finger.verifyPassword()) {
   Serial.println("Found fingerprint sensor!");
 } else {
   Serial.println("Did not find fingerprint sensor :(");
   while (1);
 }
 Serial.println("Waiting for valid finger...");
}

void loop()                     // run over and over again
{
 getFingerprintIDez();
 delay(50);            //don't ned to run this at full speed.
}

uint8_t getFingerprintID() {
 uint8_t p = finger.getImage();
 switch (p) {
   case FINGERPRINT_OK:
     Serial.println("Image taken");
     break;
   case FINGERPRINT_NOFINGER:
     Serial.println("No finger detected");
     return p;
   case FINGERPRINT_PACKETRECIEVEERR:
     Serial.println("Communication error");
     return p;
   case FINGERPRINT_IMAGEFAIL:
     Serial.println("Imaging error");
     return p;
   default:
     Serial.println("Unknown error");
     return p;
 }

 // OK success!

 p = finger.image2Tz();
 switch (p) {
   case FINGERPRINT_OK:
     Serial.println("Image converted");
     break;
   case FINGERPRINT_IMAGEMESS:
     Serial.println("Image too messy");
     return p;
   case FINGERPRINT_PACKETRECIEVEERR:
     Serial.println("Communication error");
     return p;
   case FINGERPRINT_FEATUREFAIL:
     Serial.println("Could not find fingerprint features");
     return p;
   case FINGERPRINT_INVALIDIMAGE:
     Serial.println("Could not find fingerprint features");
     return p;
   default:
     Serial.println("Unknown error");
     return p;
 }

 // OK converted!
 p = finger.fingerFastSearch();
 if (p == FINGERPRINT_OK) {
   Serial.println("Found a print match!");
 } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
   Serial.println("Communication error");
   return p;
 } else if (p == FINGERPRINT_NOTFOUND) {
   Serial.println("Did not find a match");
   return p;
 } else {
   Serial.println("Unknown error");
   return p;
 }   

 // found a match!
 Serial.print("Found ID #"); Serial.print(finger.fingerID);
 Serial.print(" with confidence of "); Serial.println(finger.confidence);
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
 uint8_t p = finger.getImage();
 if (p != FINGERPRINT_OK)  return -1;

 p = finger.image2Tz();
 if (p != FINGERPRINT_OK)  return -1;

 p = finger.fingerFastSearch();
 if (p != FINGERPRINT_OK)  return -1;

 servo1.write(servoMin); //If the fingerprint is correct open the door lock
 delay(durationTime); //Keep the lock open for the defined duration
 servo1.write(servoMax); //take the lock OFF again


 // found a match!
 Serial.print("Found ID #"); Serial.print(finger.fingerID);
 Serial.print(" with confidence of "); Serial.println(finger.confidence);
 return finger.fingerID;
}

Learn how to attache code using code tags, </>. Read the topics How to use this forum and “How to attache code, pistures etc.”

You have code for moving the servo one way and don’t know how to make the servo move the other way? That sounds like You didn’t make this code Yourself. If so, maybe You can ask the guy who made this code?

To do coding, learn what it is about, trying easy things like blinking the onboard LED. That’s how I started in the Arduino sfear in spite of 40 years of programming in other environments.

I got this code online for a hardware project I am doing for my final project at school so I am unable to ask the guy who made the code. I am new to programming so I am not sure how to adjust this unfortunately :frowning:

Okey, You are in school. Don’t You have a teacher being reposible for Your course who You can ask?
This forum is free but not started to be an alternative school.

And, again, read “How to use this forum” etc. learning posting so You will not get punched by a number of helpers. This forum is not a pre-school chidren care.

thanks for the help bud

Sorry bud but there is no quick and easy way to hand over what you would like. We are not mind readers having code ready on the shelf to hand over to You.

Ya I understand. I just wanted to see if there was something simple to add to the code to fix it. Ive been researching about this for days but no luck I guess.

Researching did not give You the answer. The only way is that You need to learn about coding and understand the code You have got. Simple fixies…. There is no such. I give You a picture, not a punsch in Your face. "Go to the toyshop and buy the toy You long for. "

Try the forum search feature for key words like "button toggle".

Edit your post and place code tags around your code to get rid of the smiley

[code]your code here[/code]

Done. Thanks. Any chance you know how I can fix this?

If you're getting here:

   servo1.write(servoMin); //If the fingerprint is correct open the door lock
    delay(durationTime); //Keep the lock open for the defined duration
    servo1.write(servoMax); //take the lock OFF again

(i.e. the servo "unlocks" the door and waits 15-seconds) then are you sure the mechanism is such that the servo is able to move the lock back to the lock position?

For example, does the mechanism go over-center and mechanically prevent the servo moving in the other direction?

Can you write minimal code to exercise the mechanism to ensure it moves properly?

The mechanism works well both ways, but I cant figure out a way to get the motor to move in the opposite direction once I put my fingerprint a second time. Here is a video demonstrating what my goal is ( watch the very end).

Looking at the video, you have a continuous rotation servo.

See this reference for .write() Arduino - ServoWrite

Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement).

Consequently, these lines do not make sense

#define servoMin 360 //Open position
#define servoMax 90 // Closed position

servo1.write(servoMin); //If the fingerprint is correct open the door lock
delay(durationTime); //Keep the lock open for the defined duration
servo1.write(servoMax); //take the lock OFF again

You will need to run the servo for some period of time to open or close the lock.

Try the forum search feature for key words like "button toggle".

Yes, this is the way to go.

getFingerprintIDez();

This will return either an ID or a -1 if the fingerprint reading is not identified

You need to keep track of the lock state, either open or closed. If the function returns a positive number, check whether the lock is open or closed, and then write the appropriate move. If the lock is open and a valid fingerprint is read, close it. If the lock is closed and a valid fingerprint is read, open it.

I've merged your other cross-post @AhmedSabrah.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

O.k. you like videos. So show us a video of what your lock looks like, exactly what "servo" you use and what it currently does using that code you posted. Because to me the code doesn't look like it will do anything sensible at all.

One hint, if you are using a continuous rotation servo then write(180) should make it rotate one way (unlock perhaps) and write(0) should make it rotate the other way (lock perhaps). Write(90) or something close to 90 will make it stop moving.

Steve