Arduino mega code not compiling

I have made a project using arduino uno and the code works perfectly. I realised that i would need more gpio pins so tried to use an arduino mega. but the code is not compiling in this case.
this is the code

#include <Adafruit_Fingerprint.h>
#include <Arduino.h>
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
int i=0;
int impulsCount=0;
int total_amount=0;
#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)

SoftwareSerial mySerial(10, 11);
LiquidCrystal_I2C lcd(0x27,16,2);

#else
#define mySerial Serial1

#endif


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  attachInterrupt(0,incomingImpuls, FALLING);
 EEPROM.get(0, total_amount);
  while (!Serial);  // For Yun/Leo/Micro/Zero/...
  delay(100);
  pinMode(13, 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 incomingImpuls()
{
  impulsCount=impulsCount+1;
  i=0;
  //lcd.clear();
//lcd.print("processing");
}
void loop()                     // run over and over again
{
  getFingerprintID();
  delay(50);  
  i=i+1;
  lcd.clear();
lcd.print("The total");
   lcd.setCursor(0,1);
  lcd.print("balance is ");
  lcd.print( EEPROM.get(0, total_amount)); 
  Serial.print("i=");
  Serial.print(i);
  Serial.print(" Impulses:");
  Serial.print(impulsCount);
  Serial.print(" Total:");
  Serial.println(total_amount);
 
  if (i>=30 and impulsCount==1){
    total_amount=total_amount+1;
    impulsCount=0;
    EEPROM.put(0, total_amount);
     lcd.clear();
lcd.print("1 rs inserted");
delay(500);
  }
   if (i>=30 and impulsCount==2){
    total_amount=total_amount+2;
    impulsCount=0;
    EEPROM.put(0, total_amount);
         lcd.clear();
lcd.print("2 rs inserted");
delay(500);
  }
   if (i>=30 and impulsCount==5){
    total_amount=total_amount+5;
    impulsCount=0;
    EEPROM.put(0, total_amount);
         lcd.clear();
lcd.print("5 rs inserted");
delay(500);
  }
   if (i>=30 and impulsCount==10){
    total_amount=total_amount+10;
    impulsCount=0;
    EEPROM.put(0, total_amount);
         lcd.clear();
lcd.print("10 rs inserted");
delay(500);
  }
}

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) {
    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");
    lcd.clear();
lcd.print("DENIED");
delay(1000);
    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);
  lcd.clear();
  digitalWrite(13, HIGH);
   lcd.print("Welcome");
  delay(1500);
  digitalWrite(13, LOW);
  

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

the error msg that comes up tells that lcd was not defined.

Perhaps because the definition of lcd is within a #if that is explicitly false for MEGA2560??

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

ok

Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"





















C:\Users\User\Desktop\piggy_bank\piggy_bank.ino: In function 'void setup()':

piggy_bank:24:3: error: 'lcd' was not declared in this scope

   lcd.init();

   ^~~

C:\Users\User\Desktop\piggy_bank\piggy_bank.ino: In function 'void loop()':

piggy_bank:77:3: error: 'lcd' was not declared in this scope

   lcd.clear();

   ^~~

C:\Users\User\Desktop\piggy_bank\piggy_bank.ino: In function 'uint8_t getFingerprintID()':

piggy_bank:176:5: error: 'lcd' was not declared in this scope

     lcd.clear();

     ^~~

piggy_bank:188:3: error: 'lcd' was not declared in this scope

   lcd.clear();

   ^~~

Multiple libraries were found for "LiquidCrystal_I2C.h"

 Used: C:\Users\User\Documents\Arduino\libraries\LiquidCrystal_I2C-master

 Not used: C:\Users\User\Documents\Arduino\libraries\LiquidCrystal_I2C-1.1.2

exit status 1

'lcd' was not declared in this scope



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

Hi,
There are several libraries for I2C,
post the link of which one you are using.

#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)

SoftwareSerial mySerial(10, 11);
LiquidCrystal_I2C lcd(0x27, 16, 2);

#else
#define mySerial Serial1

#endif

@RayLivingston has a good point. The LCD is instantiated if the processor is not Mega2560.
Move the LCD constructor. to within the else.

And you will need to connect the serial device (RFID?) to Serial1 not SoftwareSerial on pins 10 and 11.

There is a much better library for the LCD. That is the hd44780 library by Bill Perry. It is available via the IDE library manager. The library comes with lots of example code and documentation.

Change that to:

#if (defined(__AVR__) || defined(ESP8266)) && !defined(__AVR_ATmega2560__)
SoftwareSerial mySerial(10, 11);
#else
#define mySerial Serial1
#endif

LiquidCrystal_I2C lcd(0x27,16,2);

yes it is working now... thanks everyone

it is a fingerprint sensor actually

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