I'm trying to control this motor with a fingerprint sensor and need help with the code!

So, here's the issue. I'm a bit new to coding and I'm not quite sure what I'm doing wrong. I already verified that all of the parts are working correctly, and I can get the motor to work both ways with separate pieces of code. In short, I'm trying to press the sensor once and have the motor turn clockwise for a set period of time and have the LED turn on when it is complete. When I press the sensor again, I want the motor to turn counterclockwise for the same set of time and the LED to turn off when it is complete. Right now,
I press the sensor and nothing happens besides the sensor lighting up green. Any tips would be greatly appreciated! Here's what I'm working with.


Here's what I have for code so far.

/***************************************************
  This is an example sketch for our optical Fingerprint sensor

  Designed specifically to work with the Adafruit BMP085 Breakout
  ----> http://www.adafruit.com/products/751

  These displays use TTL Serial to communicate, 2 pins are required to
  interface
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/


#include <Adafruit_Fingerprint.h>


#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)
// For UNO and others without hardware serial, we must use software serial...
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
// Set up the serial port to use softwareserial..
SoftwareSerial mySerial(2, 3);

#else
// On Leonardo/M0/etc, others with hardware serial, use hardware serial!
// #0 is green wire, #1 is white
#define mySerial Serial1

#endif


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
  Serial.begin(9600);
  while (!Serial);  // For Yun/Leo/Micro/Zero/...
  delay(100);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(12,OUTPUT);
  digitalWrite(12,LOW);
  

  Serial.println("\n\nAdafruit finger detect test");

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

  Serial.println(F("Reading sensor parameters"));
  finger.getParameters();
  Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);

  finger.getTemplateCount();

  if (finger.templateCount == 0) {
    Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
  }
  else {
    Serial.println("Waiting for valid finger...");
      Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  }
}

void loop()                     // run over and over again
{
  digitalRead(12);
  getFingerprintID();
  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.fingerSearch();
  if (p == FINGERPRINT_OK && 12 == LOW) {
    Serial.println("Found a print match!");
    digitalWrite(8,LOW);
    digitalWrite(9,HIGH);
    delay(8000);
    digitalWrite(8,LOW);
    digitalWrite(9,LOW);
    digitalWrite(12,HIGH);
  } else if (p == FINGERPRINT_OK && 12 == HIGH) {
    digitalWrite(8,HIGH);
    digitalWrite(9,LOW);
    delay(8000);
    digitalWrite(8,LOW);
    digitalWrite(9,LOW);
    digitalWrite(12,LOW);
  } 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);

  return finger.fingerID;
}

// 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;

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

Show a schematic to help understand your wiring.
Where is the power supply for the ?DC? motor?

Lesson 1:
Do not use magic numbers. Your pins have a function so give them a sensible name based on that function. I have no idea what pins 8, 9 and 12 are used for.

E.g.

const uint8_t myPin = 8;

In setup, you can use

pinMode(myPin, OUTPUT);

And similar in the rest of your code. myPin is not a sensible name so pick on that covers the functionality.

Lesson 2:

void setup()
{
  Serial.begin(9600);
  while (!Serial)
  Serial.print("LOW = ");
  Serial.println(LOW);
}

void loop()
{
}

Run the above sketch; what does it print? Will that ever be the same as 12? So if (p == FINGERPRINT_OK && 12 == LOW) will never evaluate to true.

Further

  1. You're trying to read a pin that is configured as output; is there a reason for that?
  2. You throw away the result; so you can just as well leave the digitalRead() statement out.

This might work better

  1. Create a variable (before setup()) to store the result of the digitalRead(); call it e.g. status.
  2. Change the first line in loop()
status = digitalRead(somePin);
  1. And in getFingerprintID(), change the line
    if (p == FINGERPRINT_OK && 12 == LOW)
    to
    else if (p == FINGERPRINT_OK && status == LOW)
    and the line
    else if (p == FINGERPRINT_OK && 12 == HIGH)
    to
    else if (p == FINGERPRINT_OK && status == HIGH)

Note again: I do not know what pin 12 is used for so can't give it a sensible name; hence the name somePin.

Thank you so much for the help! I changed the pin from 12 to 11. Now the motor goes counterclockwise fulfilling one set of the conditions.

if (p == FINGERPRINT_OK && status == LOW) {
    Serial.println("Found a print match!");
    digitalWrite(posMotorPin,LOW);
    digitalWrite(negMotorPin,HIGH);
    delay(8000);
    digitalWrite(posMotorPin,LOW);
    digitalWrite(negMotorPin,LOW);
    digitalWrite(ledPin,HIGH);

How do I get status to read HIGH?

} else if (p == FINGERPRINT_OK && status == HIGH) {
    digitalWrite(posMotorPin,HIGH);
    digitalWrite(negMotorPin,LOW);
    delay(8000);
    digitalWrite(posMotorPin,LOW);
    digitalWrite(negMotorPin,LOW);
    digitalWrite(ledPin,LOW);

I guess I was trying to use digitalRead() sort of like a momentary switch? I'm sure I'm missing something super fundamental. Unsure if that's even possible with pins. Like I said very new at this. I still can't seem to figure out how to get it to read the second set of conditions to turn the motor clockwise.

I read something somewhere about maybe having to use EEPROM?

Here's all of the code I have now.

const uint8_t posMotorPin = 8;
const uint8_t negMotorPin = 9;
const uint8_t ledPin = 11;
const uint8_t status;


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

  pinMode(posMotorPin,OUTPUT);
  pinMode(negMotorPin,OUTPUT);
  pinMode(ledPin,OUTPUT);

  Serial.println("\n\nAdafruit finger detect test");

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

  Serial.println(F("Reading sensor parameters"));
  finger.getParameters();
  Serial.print(F("Status: 0x")); Serial.println(finger.status_reg, HEX);
  Serial.print(F("Sys ID: 0x")); Serial.println(finger.system_id, HEX);
  Serial.print(F("Capacity: ")); Serial.println(finger.capacity);
  Serial.print(F("Security level: ")); Serial.println(finger.security_level);
  Serial.print(F("Device address: ")); Serial.println(finger.device_addr, HEX);
  Serial.print(F("Packet len: ")); Serial.println(finger.packet_len);
  Serial.print(F("Baud rate: ")); Serial.println(finger.baud_rate);

  finger.getTemplateCount();

  if (finger.templateCount == 0) {
    Serial.print("Sensor doesn't contain any fingerprint data. Please run the 'enroll' example.");
  }
  else {
    Serial.println("Waiting for valid finger...");
      Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
  }
}

void loop()                     // run over and over again
{
  status == digitalRead(ledPin);
  getFingerprintID();
  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.fingerSearch();
  if (p == FINGERPRINT_OK && status == LOW) {
    Serial.println("Found a print match!");
    digitalWrite(posMotorPin,LOW);
    digitalWrite(negMotorPin,HIGH);
    delay(8000);
    digitalWrite(posMotorPin,LOW);
    digitalWrite(negMotorPin,LOW);
    digitalWrite(ledPin,HIGH);
  } else if (p == FINGERPRINT_OK && status == HIGH) {
    digitalWrite(posMotorPin,HIGH);
    digitalWrite(negMotorPin,LOW);
    delay(8000);
    digitalWrite(posMotorPin,LOW);
    digitalWrite(negMotorPin,LOW);
    digitalWrite(ledPin,LOW);
  } 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;
  }```

If there is 5V on ledPin when you read it then status will be HIGH.

But why are you reading a pin that's configured as an output? That makes no sense. You set the state of an output. You read the state of an input. Why don't you set status to true or false when it needs to be set instead of trying to read it from an output pin.

'==' is a compare
'=' is an assignment; look at the snip where I used digitalRead().

Got a compilation error.

de.ino:89:30: error: assignment of read-only variable 'status'
status = digitalRead(ledPin);
^

exit status 1

Compilation error: assignment of read-only variable 'status'

remove the const from its declaration..

good luck.. ~q

Got it! Thank you everyone!

I like your idea. Now I don't need the LED'S, thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.