executing if-else for final output

i am working on a fingerprint doorlock.

im using a pushbutton for in-house lock and open

im using counter to lock and open, if the finger valid or the button pressed it will increment the counter.

if even it locked, if odd its opened

  • the problem is, im editing the library code from adafruit. im adding a state change detection for the push button to increment the counter.

the state change detection must be in the void loop right?

for now im able to do the counter correctly. But when i put the if-else for the even and odd (output) in the void loop, the relay loops too.

where should i put the if-else for the output in the program

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

#define lock 11
#define openn 10
#define alarm 9
#define sw 7

int getFingerprintIDez();
boolean pinState;
bool executed = false;
int cnt = 0; // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
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");

  pinMode(lock, OUTPUT);
  digitalWrite(lock, LOW);
  pinMode(openn, OUTPUT);
  digitalWrite(openn, LOW);
  pinMode(alarm, OUTPUT);
  digitalWrite(alarm, HIGH);
  pinState = digitalRead(lock);
  pinMode(12, OUTPUT);


  // 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();
  digitalWrite(12, HIGH);
  //don't ned to run this at full speed.
  {
    buttonState = digitalRead(sw);                           //////////STATE CHANGE FOR THE PUSH BUTTON TO INCREMENT THE COUNTER

    // compare the buttonState to its previous state
    if (buttonState != lastButtonState) {
      // if the state has changed, increment the counter
      if (buttonState == HIGH) {
        // if the current state is HIGH then the button
        // wend from off to on:
        cnt++;
        Serial.println("Tombol dalam ditekan");
        Serial.print("Counter:  ");
        Serial.println(cnt);
      }
      // Delay a little bit to avoid bouncing
      delay(100);
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;

  }
}

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;

  cnt++;                                             /// TO INCREMENT THE COUNTER IF FINGER IS MATCH

  // found a match!

  digitalWrite(12, LOW);
  delay(600);
  digitalWrite(12, HIGH);

  if (cnt % 2 == 0) {
    digitalWrite(lock, HIGH);
    delay(300);
    digitalWrite(lock, LOW);
  }
 else {
    digitalWrite(openn, HIGH);
    delay(300);
    digitalWrite(openn, LOW);
  }


  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  Serial.print("Counter: "); Serial.println(cnt);


  return finger.fingerID;
}

To fix your layout, use ctrl-T in the IDE, and it's done for you. Makes your code a lot more readable.

Delta_G:

if (cnt %2 != 0)

An awfully long winded way to say else. What other value do you think it could have if it failed for being equal to zero?

You seem to have a function written at the end called outp. Why don't you just call that from loop?

Why the whole thing with even and odd. Why not just a boolean called "locked" that you could flip from true to false?

Look at the sections you copied. Notice how the blocks and braces all are lined up and formatted nice and yours look like you just put the cursor wherever and started typing. Fix that. You'll thank me later. It makes things a lot easier to debug.

yes i know i can use else. How can i use boolean? how can i know which fingerscan or button press gonna be the lock or unlock?

if i call the outp function in the loop, the the action loops too. if its even or odd the relay flashes nonstop, it should be once

im kinda new into this

wvmarle:
To fix your layout, use ctrl-T in the IDE, and it's done for you. Makes your code a lot more readable.

fixed that

What you want to do is:

boolean LockStateOpen = false;
boolean PreviousButonState = LOW;
boolean PreviousFingerprintState = false;


void loop()
{
  boolean buttonState = digitalRead(ButtonPin);
  if (buttonState != PreviousButtonState)
  {
    PreviousButtonState = buttonState;
    if (buttonState == HIGH)
    {
      // Button has just been pressed
      FlipLockState();
    }
  }


  boolean fingerprintState = getFingerprintID() == FINGERPRINT_OK;
  if (fingerprintState != PreviousFingerprintState)
  {
    PreviousFingerprintState = fingerprintState;
    if (fingerprintState == true)
    {
      // Valid fingerprint has just been recognized
      FlipLockState();
    }
  }
}


void FlipLockState()
{
  LockStateOpen = !LockStateOpen;
 
  if (!LockStateOpen) 
  {  // Lock
    digitalWrite(lock, HIGH);
    delay(300);
    digitalWrite(lock, LOW);
  }
  else 
  {  // Unlock
    digitalWrite(openn, HIGH);
    delay(300);
    digitalWrite(openn, LOW);
  }
}