Arduino Uno Code not working on Mega

The code below works fine with uno but does not work with mega.

Im using a fingerprint scanner from adafruit which works fine with uno but not mega.

As far as im aware the code should remain the same apart from pin assignments.

Please help in any way.

ps and yes i was connected to the correct board (:

//Patrick McGrath:
//Main Code:
//2 Tier Security Case

#include <LiquidCrystal.h> //include LCD library (standard library)
#include <Keypad.h> //include keypad library - first you must install library (library link in the video description)
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(20, 21);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

int ledButton = 6; //led to register keypad pressses
int ledPir = 7;
int buzzer = 8;
int fingerprintLed = 10;
const int pirPower = 11;
int ledPinCorrect = 13;
int inputPir = 18; // choose the input pin (for PIR sensor)

char* password =“0000”; //create a password
int i = 0; //keypad position
int c = 0;
const byte ROWS = 4; //number of the keypad’s rows and columns
const byte COLS = 3;

char keyMap[ROWS][COLS]= //define the cymbols on the buttons of the keypad

{
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};

byte rowPins[ROWS] = {43, 53, 51, 47}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {45, 41, 49}; //connect to the column pinouts of the keypad

Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);

const int rs = 31, en = 29, d4 = 33, d5 = 35, d6 = 37, d7 = 39;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup()
{
lcd.begin(16, 2);
Serial.begin(9600); //open serial port
pinMode(ledButton, OUTPUT); //define led pin as output
pinMode(ledPinCorrect, OUTPUT); //define led pin as output
pinMode(ledPir, OUTPUT); //define led pin as output
pinMode(inputPir, INPUT); //define pir pin as input
pinMode(pirPower,OUTPUT);
pinMode(fingerprintLed,OUTPUT);

setLocked (true); //state of the password
digitalWrite(pirPower, HIGH); // pir is powered up
delay(3000); // pir

lcd.setCursor(0, 0);
lcd.print("*** CAUTION ");
lcd.setCursor(0, 1);
lcd.print("
************");

while (!Serial); // For Yun/Leo/Micro/Zero/…
delay(100);
Serial.println("\n\nAdafruit finger detect test");

// set the data rate for the sensor serial port
finger.begin(67200);

if (finger.verifyPassword()) {
Serial.println(“Found fingerprint sensor!”);
}
else
{
Serial.println(“Did not find fingerprint sensor :(”);
while (1)
{
delay(1);
}
}
Serial.println(“Waiting for valid finger…”);

}

void loop()
{
int myInt = getFingerprintIDez();
Serial.println(myInt);

delay(50); //don’t ned to run this at full speed.
digitalWrite(ledButton,HIGH);
delay(300);
digitalWrite(ledButton,LOW);
delay(300);
digitalWrite(ledButton,HIGH);
delay(300);
digitalWrite(ledButton,LOW);
}

// //
// Fingerprint //
// //

int getFingerprintID()
{
int 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);

return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez()
{
int p = finger.getImage();

if (p != FINGERPRINT_OK)
{
Serial.println(“getImage #”);
Serial.println(FINGERPRINT_OK);
return -1;
}

p = finger.image2Tz();
if (p != FINGERPRINT_OK)
{
Serial.print(“image2Tz #”);
return -1;
}

p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK)
{
Serial.print(“fingerFastSearch #”);
return -1;
}

// found a match!

digitalWrite(fingerprintLed,HIGH);
delay(2000);
digitalWrite(fingerprintLed,LOW);
Serial.print(“Found ID #”);
Serial.print(finger.fingerID);
Serial.print(" with confidence of ");
Serial.println(finger.confidence);
return finger.fingerID;
}

// //
// PIR //
// //

void pir_detection()
{
digitalWrite(ledPir, HIGH); // turn pir led
digitalWrite(buzzer, HIGH); // turn buzzer
digitalWrite(pirPower, LOW); // pir is powered off
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" Enter Password");
delay(3000);
digitalWrite(ledPir, LOW); // turn pir led off
}

//
// Locked //
//

void setLocked(int locked)
{
if(locked)
{
digitalWrite(ledPir, LOW);
digitalWrite(ledPinCorrect, LOW);
}
else
{
digitalWrite(ledPir, LOW);
digitalWrite(ledPinCorrect, HIGH);
}
}

// //
// KEYPAD //
// //

void keypad_input()
{
char whichKey = myKeypad.getKey(); //define which key is pressed with getKey

if (whichKey) //if a key is pressed
{
digitalWrite(ledButton, HIGH); //turn on ledPin
delay(100);
digitalWrite(ledButton, LOW); //turn off ledPin
delay(100);
lcd.setCursor(5+c,1);
lcd.print(whichKey);
c++;

if ( c == 4)
{
c = 0;
}
}

if(whichKey == ‘*’ || whichKey == ‘#’)
{
i = 0;
setLocked (true);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Invalid Key!");
delay(1000);
lcd.clear();
}

if(whichKey == password )

  • {*
  • i ++;*
  • }*
  • if(i == 4)*
  • {*
  • setLocked (false);*
  • lcd.clear();*
  • lcd.setCursor(0, 0);*
    _ lcd.print("*** Verified ***");_
  • delay(3000);*
  • lcd.clear();*
  • lcd.setCursor(0, 0);*
  • lcd.print(“Place Finger”);*
  • lcd.setCursor(0, 1);*
  • lcd.print(“To Continue”);*
  • delay(7000);*
  • lcd.clear(); *
  • }*
    }

Please edit your post and use code tags around your code; that will prevent that the forum software translates [i] (from your code) to a web browser instruction to display stuff in italics.

Where did this piece of non-conforming code creep in?

  // set the data rate for the sensor serial port
  finger.begin(67200);

From the Adafruit docs::

Baud rate: 9600, 19200, 28800, 38400, 57600 (default is 57600)

SoftwareSerial mySerial(20, 21);

Using SoftwareSerial when you have three unused HardwareSerial ports.
Why?

SoftwareSerial RX pin must be a pin that supports pin change interrupts.
20 and 21 are not such pins on a Mega.