Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Windows - Arduino S...
 
Share:
Notifications
Clear all

[Solved] Windows - Arduino Solenoid Lock using FingerPrint with CoinCounter

65 Posts
2 Users
1 Reactions
15.8 K Views
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

Looking at the code sir I was using the Boolean code to indicate the TRUE and FALSE function where can I insert this line of code sir?

This code was the from void loop of the COIN program sir

 

void loop() {
  while(!coinInserted){
  coinSlotSignal = digitalRead(coinSlot);
  if(coinSlotSignal < 1) {
    coinCount += 1;
    coinInserted = true;
EEPROM.write(0,coinCount);
Serial.println(EEPROM.read(0)); 


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

}
  }
}
  
while(coinInserted) {
  coinSlotSignal = digitalRead(coinSlot);
  if(coinSlotSignal >0) {
    coinInserted = false;
  
  }
}
if(coinCount >= requiredCoins) {
  delay(1500);
}
}

Maybe it needs the

CoinInserted = false ;

 

inside the 2 while loops code

but when I try to sink the code its getting a lot of error


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans I already fixed it sir No need to add } in the else statement

it must be like this

 

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
      // this line Idint add any bracket
    // only check for coins to be inserted if we're not opening the door
    coinSlotSignal = digitalRead(coinSlot);

  if (coinSlotSignal < 1) {
    coinCount++;
    EEPROM.write(0, coinCount);
    Serial.println(EEPROM.read(0));
    lcd.setCursor(0, 0);
    lcd.print("TOTAL:");
    lcd.setCursor(0, 1);
    lcd.print(coinCount);
    delay(1000);
    // add a little delay, since the coinslot may show a signal longer
  }
  else {
    lcd.clear();
  }
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Seems this is only part of the code ...?

After re-formatting (always make sure accolades and indentations are done nicely - it improves the overview and helps finding errors):

void loop() {
  while (!coinInserted) {
    coinSlotSignal = digitalRead(coinSlot);
    if (coinSlotSignal < 1) {
      coinCount += 1;
      coinInserted = true;
      EEPROM.write(0, coinCount);
      Serial.println(EEPROM.read(0));

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

    }
  }
}

while (coinInserted) {
  coinSlotSignal = digitalRead(coinSlot);
  if (coinSlotSignal > 0) {
    coinInserted = false;

  }
}
if (coinCount >= requiredCoins) {
  delay(1500);
}
}

 

The while loop is outside the void loop ... so that is a problem ...
Additionally there is an accolade too many at the end ... another problem.

The code below is probably more like it - but you still will have to go through your code and find the mistakes - just in case.

void loop() {
  while (!coinInserted) {
    coinSlotSignal = digitalRead(coinSlot);
    if (coinSlotSignal < 1) {
      coinCount += 1;
      coinInserted = true;
      EEPROM.write(0, coinCount);
      Serial.println(EEPROM.read(0));

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

    }

  }

  while (coinInserted) {
    coinSlotSignal = digitalRead(coinSlot);
    if (coinSlotSignal > 0) {
      coinInserted = false;

    }
  }
  if (coinCount >= requiredCoins) {
    delay(1500);
  }
}

 

In your other post the code goes quite wrong as well:

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

    // only check for coins to be inserted if we're not opening the door
   coinSlotSignal = digitalRead(coinSlot);
   
   if(coinSlotSignal < 1) {
      coinCount++;
      EEPROM.write(0,coinCount);
      Serial.println(EEPROM.read(0)); 
      
      lcd.setCursor(0,0);
      lcd.print("TOTAL:");
      lcd.setCursor(0,1);
      lcd.print(coinCount);  
      delay(1000);
      // add a little delay, since the coinslot may show a signal longer   
  }
  else {
    lcd.clear();
  }
}

 

With a few corrections:

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 
    {
      // only check for coins to be inserted if we're not opening the door
      coinSlotSignal = digitalRead(coinSlot);

      if (coinSlotSignal < 1) {
        coinCount++;
        EEPROM.write(0, coinCount);
        Serial.println(EEPROM.read(0));

        lcd.setCursor(0, 0);
        lcd.print("TOTAL:");
        lcd.setCursor(0, 1);
        lcd.print(coinCount);
        delay(1000);
        // add a little delay, since the coinslot may show a signal longer   
      } else {
        lcd.clear();
      }
    }
}

 

You will have to look more careful where accolades are placed or not.
It will save a lot of time when formatted properly - if you need a tool for that, try this online service codebeautify.org.


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans its very nice of you sir , Im having a good experience and knowledge by your help, I'm improving right here


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans Sir I tired to post in the Arduino forum but their not so responsive one commented my post like this 

But I cant I understand what is he saying sir hope you can understand it for me sir

Understand the Arduino code structure. You have declarations and global definitions. After that you have all of your functions. Two functions that you must have one of are setup(), which is executed once, and loop() which runs forever.

You can put all of your code in one large and messy blob inside of the loop function. I prefer to put my code into separate functions and call those functions from inside of loop(). This makes troubleshooting way easier. And in a year when you revisit the code, it will be less confusing.

Any code not in a function will never be executed. This is what you have now.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Basically what he is saying is that should make yourself more familiar with programming for the Arduino and understand how code works.

Some of the issues we run into, for example, is that you see error messages because you're not using the accolades correctly.
This can be because of typo's of course, or because you may be copying and pasting code.
With code not being nicely formatted, it will be hard to see what the problem is.

Obviously, this can lead to frustration for those you ask for help.
For them it can feel like you didn't do enough effort.
Personally, I prefer folks being a little nicer o each other, but he does make a valid point.

p.s. I have written an intro course for Arduino programming, if you're interested (it's free of course).


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans wow , I tried to look of your references sir it was so informative sir. Ill take time to read it sir and Find out the problem of the code , hope I can finish it . I'm on the deadline of my project for school . If I cant finish the project I cant graduate


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 
Posted by: @kalilinux25

my project for school

Let me know where I can help ... but you will have to go get a better understanding of how code works.
It shouldn't be much work though. 

And if you still run into issues/errors, I do not mind trying to take a look and see if I can help.


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans what is he saying sir cant really understand 

 

This does not compile. Your loop() function ends early because of the braces. This leaves the code after it orphaned and unreachable.


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

What he mean is that each Arduino program has a structure like so:

...

void setup() {
  ...
}

void loop() {
  ...
}

...

 

Each "block" is enclosed by accolades (or "braces").

However, in your previous code you did something like this:

...

void setup() {
  ...
}

void loop() {
  ...
}
}

...

 

See the extra accolade? This ends the void loop() code too early - most likely a typo or copy and paste issue.
You can often see this right away when you clean up the code and use proper indentation.
See also my code after that - which I cleaned up a little.

With orphaned code he means that some code falls outside of the accolades, which is by definition a problem and wil cause issues when trying to compile it. Even when the compiler doesn't give you any errors, the code very likely will not do what you were expecting it to do.

For example, this would be correct:

...

void setup() {
  ...
}

void loop() {
  ...
  if(...) {
    // do something
  }

  // do something else
}

...

 

But in your code you had something like this:

...

void setup() {
  ...
}

void loop() {
  ...
  if(...) {
    // do something
  }
} // <---- makes the following code orphaned!
  // do something else
}

...

 

So in this last example "do something else" falls outside of the "block" (or: scope) and the compiler will complain about it.
If you go through the void loop you see 2 opening accolades ( { ), but 3 closing accolade ( } ).
These should always be matching pairs to close a block.

I hope that makes sense.


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans someone suggested to me that I must use interrupt instead of Boolean?

 

I tried to change the code of first line of

Boolean

 to 

int

 

I just change it the Boolean to int.

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

 

 

int coinCount = 0;
int coinSlotSignal;
int requiredCoins = 1;
int coinInserted = false;

 

 

then tried to upload it but nothing has change


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans I also read about attached interrupt codes but it was just uning pin 2,3 in digital pin in arduino uno..


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans I also found that I can use attached interrupt for the pin 9 of the coin slots but the problem what line of code to delete and where can I insert the attached interrupt to the coin code?

 

example

 

const byte ledPin = 13;
const byte interruptPin = 2;
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
  digitalWrite(ledPin, state);
}

void blink() {
  state = !state;

 

my code

#include <Adafruit_Fingerprint.h>    //Libraries needed
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#define coinSlot 9 // coinslot 5v to 0v if coin passes on the sensor
#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

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
  {
    coinSlotSignal = digitalRead(coinSlot);

    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
}

 

But I got so many error also.

 









C:\Users\asus\Documents\MARVE THESIS\2_whilelloops\2_whilelloops\2_whilelloops.ino: In function 'void setup()':
2_whilelloops:42:45: error: 'coinInterrupt' was not declared in this scope
   attachInterrupt(digitalPinToInterrupt(9), coinInterrupt, FALLING);
                                             ^~~~~~~~~~~~~
C:\Users\asus\Documents\MARVE THESIS\2_whilelloops\2_whilelloops\2_whilelloops.ino:42:45: note: suggested alternative: 'noInterrupts'
   attachInterrupt(digitalPinToInterrupt(9), coinInterrupt, FALLING);
                                             ^~~~~~~~~~~~~
                                             noInterrupts
C:\Users\asus\Documents\MARVE THESIS\2_whilelloops\2_whilelloops\2_whilelloops.ino: In function 'void loop()':
2_whilelloops:63:5: error: 'coinSlotSignal' was not declared in this scope
     coinSlotSignal = digitalRead(coinSlot);
     ^~~~~~~~~~~~~~
C:\Users\asus\Documents\MARVE THESIS\2_whilelloops\2_whilelloops\2_whilelloops.ino:63:5: note: suggested alternative: 'coinSlot'
     coinSlotSignal = digitalRead(coinSlot);
     ^~~~~~~~~~~~~~
     coinSlot
2_whilelloops:10:18: error: expected unqualified-id before numeric constant
 #define coinSlot 9 // coinslot 5v to 0v if coin passes on the sensor
                  ^
C:\Users\asus\Documents\MARVE THESIS\2_whilelloops\2_whilelloops\2_whilelloops.ino:78:10: note: in expansion of macro 'coinSlot'
     void coinSlot() {
          ^~~~~~~~
Multiple libraries were found for "Adafruit_Fingerprint.h"
 Used: C:\Users\asus\Documents\Arduino\libraries\Adafruit_Fingerprint_Sensor_Library
 Not used: C:\Users\asus\Documents\Arduino\libraries\fing
exit status 1
'coinInterrupt' was not declared in this scope

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
 

Well, an interrupt is an option of course.
What the interrupt does on coin insert, is jump to the defined function, execute it, and return to where it left off.
So no extra lines would be needed. Everything related to the coin slot should be moved in that function (coinInterrupt) though (eg. LCD or serial output, increase counted coins, etc).

As for the errors you're getting (I did not expect your code):
Always start with the first error, as fixing this first error could potentially clear all following errors as well.

So you first error says: "'coinInterrupt' was not declared in this scope" which means that the function "coinInterrupt" is not defined where it should be.
Typically this error occurs when accolades are not balanced correctly, or (like in your case) it simply was never defined.

You need to add something like this to at least define some sort of function:

void coinInterrupt {
  // do something here
}

 

As you can see; copying and pasting code from others, without really know the fine details, may come with errors that may or may not be hard to understand.


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
Topic starter  

@hans I also tried to add a library also sir ang change it to 

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

 

Full code has this 

 

#include <PinChangeInterrupt.h>
#include <Adafruit_Fingerprint.h>    //Libraries needed
#include <SoftwareSerial.h>

#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
#define coinSlot 9 // coinslot 5v to 0v if coin passes on the sensor
#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

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);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(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
  {
    coinSlotSignal = digitalRead(coinSlot);

    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 coinInterrupt() {
      coinCount++ ;
      insert = true;
}
//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
}

 

 

then Now the line of code error is at the 

coinSlotSignal

 

Arduino: 1.8.14 (Windows 10), Board: "Arduino Uno"





















C:\Users\asus\Documents\MARVE THESIS\hans_forum\hans_forum.ino: In function 'void loop()':

hans_forum:61:5: error: 'coinSlotSignal' was not declared in this scope

     coinSlotSignal = digitalRead(coinSlot);

     ^~~~~~~~~~~~~~

C:\Users\asus\Documents\MARVE THESIS\hans_forum\hans_forum.ino:61:5: note: suggested alternative: 'coinSlot'

     coinSlotSignal = digitalRead(coinSlot);

     ^~~~~~~~~~~~~~

     coinSlot

C:\Users\asus\Documents\MARVE THESIS\hans_forum\hans_forum.ino: In function 'void coinInterrupt()':

hans_forum:79:7: error: 'insert' was not declared in this scope

       insert = true;

       ^~~~~~

C:\Users\asus\Documents\MARVE THESIS\hans_forum\hans_forum.ino:79:7: note: suggested alternative: 'finger'

       insert = true;

       ^~~~~~

       finger

Multiple libraries were found for "Adafruit_Fingerprint.h"

 Used: C:\Users\asus\Documents\Arduino\libraries\Adafruit_Fingerprint_Sensor_Library

 Not used: C:\Users\asus\Documents\Arduino\libraries\fing

Multiple libraries were found for "PinChangeInterrupt.h"

 Used: C:\Users\asus\Documents\Arduino\libraries\PinChangeInterrupt

 Not used: C:\Users\asus\Documents\Arduino\libraries\PinChangeInterrupt-master

exit status 1

'coinSlotSignal' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

   
ReplyQuote
Page 2 / 5
Share: