1

Hi Im having a problem on my code How can I properly use the attached Interrupt on my coin Sensor so that it will not have any problem on detecting coins

#include <Adafruit_Fingerprint.h>    //Libraries needed
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#define I2C_ADDR 0x27          //LCD i2c stuff
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
const int coinSlot = 9;
int relayPin = 4;

String Names[] = { "RAVEN", "Surtr", "Tech",}; //Those are the names affected to the fingertemplates IDs
//The first on which is Names[0] : "" has the ID 1 in the fingerprint sensor

SoftwareSerial mySerial(2, 3);                  //Fingerprint sensor wiring RX 3, TX 2
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin); //LCD declaring

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);                    //Fingerprint sensor declaring

volatile int coinCount = 0;
int requiredCoins = 1;
boolean coinInserted = false;

void setup()
{
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH);
  pinMode(coinSlot, INPUT_PULLUP);
  lcd.write(EEPROM.read(5));
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(9), coinInterrupt, FALLING);
  finger.begin(57600);              //Sensor baude rate
  lcd.begin (16, 2);
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.setCursor(0, 0);
  lcd.print("SECURED VAULT");
  lcd.home();
  finger.getTemplateCount();        //Counts the number of templates stored in the sensor flash memory
  delay(2000);
}

void loop() {
  int FingerPrintResult = getFingerprintIDez();

  if (FingerPrintResult != -1) { //This function keeps looping and waiting for a fingerprint to be put on the sensor
    OpenDoor();
  }
  else
  {
    if (coinInserted) {
      coinInserted = false;
      coinCount++;

      EEPROM.write(0, coinCount);
      Serial.println(EEPROM.read(0));

      lcd.setCursor(0, 0);
      lcd.print("TOTAL:");
      lcd.setCursor(0, 1);
      lcd.print(coinCount);
    }

    void coinSlot() {
      coinCount++ ;
      insert = true;

      else {
        lcd.clear();
      }
    }
  }
}
//Only the modifications are commented
int getFingerprintIDez() {
  uint8_t p = finger.getImage();        //Image scanning
  if (p != FINGERPRINT_OK)  return -1;

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

  lcd.clear();                     //And here we write a message or take an action for the denied template
  p = finger.fingerFastSearch();     //Looking for matches in the internal memory
  if (p != FINGERPRINT_OK) {         //if the searching fails it means that the template isn't registered
    lcd.print("Access denied");
    delay(2000);
    return -1;
  }
  //If we found a match we proceed in the function

  lcd.print("Welcome");                  //Printing a message for the recognized template
  lcd.setCursor(2, 1);
  lcd.print(Names[finger.fingerID - 1]); //Then print the name we gave it and the -1 is to remove the shift as the ID starts from "1" but the array from "0"
  return finger.fingerID;
}
void OpenDoor() {
  digitalWrite(relayPin, LOW); // turn on solenoidlock
  delay(5000);
  digitalWrite(relayPin, HIGH); // turn off solenoidlock
}

Please how can I get the code working and what line of code should I focus to make the code work

9
  • Were you not able to take Majenko's suggestion on your other question to rearrange the pins such that you can use an attachInterrupt capable pin? Failing that, did you not try to use the library mentioned in my answer to that question? – timemage May 22 at 13:57
  • Yup, now I'm definitely voting to leave it open. – timemage May 22 at 16:40
  • why can't you move the SoftwareSerial to other pair of pins? – Juraj May 24 at 5:24
  • I tried it sir but my fingerprint wont work on other pins but only in the 2,3 pins, waht maybe your suggestion sir? – Raven Tenepre May 24 at 14:23
  • there is no reason for SoftwareSerial to not to work on other pins. what pins did you try? – Juraj May 24 at 16:21
0

Again, it makes sense to just use an attachInterrupt capable pin; but if can't for some reason:

Library install

Applying what I suggested in the answer on your other question to the code you've provided here, means installing the mentioned library:

Choosing the Library Manager from Tools/Libraries menu.

And searching for and installing the library from the manager: Searching for pinChangeInterrupt and installing.

Code changes

Including the header

Patterned after documentation and an example for the library, your code would need to include the header for the library:

#include "PinChangeInterrupt.h"

Replacing attachInterrupt

Your attachInterrupt call would need to change to attachPinChangeInterrupt; likewise with the function that maps the pin to the interrupt number:

attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(9), coinInterrupt, FALLING);

That is about all there is to it. On the UNO/Nano or broadly ATMega328P-based Arduinos, only pins 2 and 3 work with attachInterrupt. attachPinChangeInterrupt will work with most pins of most AVR-based Arduinos; any pins that have an PCINTn signal routed to them. These are not as desirable to use, for performance reasons that you don't need to worry about for push buttons or your coin detector, and for easy-of-use reasons that are taken care of for you by this library.

4
  • I got a error code sir it says – Raven Tenepre May 23 at 14:13
  • libraries\SoftwareSerial\SoftwareSerial.cpp.o (symbol from plugin): In function `SoftwareSerial::read()': (.text+0x0): multiple definition of `__vector_3' PinChangeInterrupt0.cpp.o (symbol from plugin):(.text+0x0): first defined here collect2.exe: error: ld returned 1 exit status – Raven Tenepre May 23 at 14:13
  • Ah, yeah that is a bit of a pain. SoftwareSerial does use pin change interrupts, the libraries would need to be modified to free up at least one of the PCINT vector in each, to accommodate each other. Alternately you'd need to drop SoftwareSerial, or as was said before, find a way to make use of the regulator interrupt pins. There's not much reason why SoftwareSerial couldn't be written in terms of PinChangeInterrupt and may well have been. It is worth looking. – timemage May 23 at 16:00
  • How can I solve this issue sir where to look up – Raven Tenepre May 23 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.