Code from Uno R3 not working on Mega 2560

Hello everyone, this is my first post, and I think this is the right forum since I think this is a coding issue.

Here is the code that works perfectly on the Uno R3, but not on the Mega 2560. Both are Elegoo if that matters.

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>




const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup(){
  Serial.begin(9600);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  digitalWrite(11, HIGH);
  digitalWrite(12, HIGH);
}

void initMG() {
    digitalWrite(11, LOW);  //turns on M/G
    delay(1000);            //waits 1000 milliseconds; enough time for the M/G to turn on
    digitalWrite(11, HIGH);   //stops signal going to M/G
    delay(5000);            //waits to start ringing for the M/G to get voltage up
}

void ringToller(long ringLength, int ringDelay) {
  bool tolling = true;
  long nRingLength = ringLength;
  while (tolling)  
  {
    if (customKeypad.getKey() == 'A') {
      nRingLength = 0;
    }
    digitalWrite(12, LOW);  //closes relay which will energize striker
    delay(1000);            //waits 1000 milliseconds, striker retracts
    digitalWrite(12, HIGH);   //shuts off voltage
    delay(ringDelay);            
    nRingLength -= ringDelay + 1000;
    if (nRingLength <= 0) { 
      tolling = false;
    }
    Serial.print(nRingLength);
    Serial.println();
  }
  initMG();
}

int stage = 0;
long ringLength = 0;
int ringDelay = 0;
void loop(){
  char key = customKeypad.getKey();
  
  if (key == '*') {
    stage = 1;
  }

  else if (stage == 1) {
    if (key != NO_KEY) {
      stage = 2;
      switch (key) {
        case '1':
          ringLength = 60000;
          break;
        case '2':
          ringLength = 120000;
          break;
        case '3':
          ringLength = 180000;
          break;
        case '4':
          ringLength = 240000;
          break;
        case '5':
          ringLength = 300000;
          break;
        case '6':
          ringLength = 360000;
          break;
        case '7':
          ringLength = 420000;
          break;
        case '8':
          ringLength = 480000;
          break;
        case '9':
          ringLength = 540000;
          break;
        default: break;
      }
    }
  }

  else if (stage == 2) {
    if (key != NO_KEY) {
      stage = 3;
      switch (key) {
        case '1':
          ringDelay = 1000;
          break;
        case '2':
          ringDelay = 2000;
          break;
        case '3':
          ringDelay = 3000;
          break;
        case '4':
          ringDelay = 4000;
          break;
        case '5':
          ringDelay = 5000;
          break;
        case '6':
          ringDelay = 6000;
          break;
        case '7':
          ringDelay = 7000;
          break;
        case '8':
          ringDelay = 8000;
          break;
        case '9':
          ringDelay = 9000;
          break;
        default: break;
      }
    }
  }

  else if (stage == 3) {
    initMG();
    ringToller(ringLength, ringDelay);

    stage = 0;
  }
}

I’m using the Keypad library by Alexander Brevig. I should note my coding skills are very limited, and much of this code was written by my brother while we were vacationing together. We live in separate time-zones and he’s very busy, so I can’t get his help in the near future. I (sorta) understand the code, but please try to be as simplistic as possible.

I’ve spent several days trying to solve this problem on my own, looking at other posts with similar issues but with no success. Yes, I have changed the board in tools and the port to the proper settings. I know the board itself is good because I’ve tested another project on it and it works as intended. I’ve tried changing up the pins numerous times and changing USB cables. I’ve tested the relay module I’m using with it (further explained below). I’ve tested the keypad with the serial monitor on the Mega and it works. Basically, everything is the exact same except the use of the Mega rather than the Uno.

It’s basically a toller for a church bell I’m making. The way it works is when somebody presses the ‘*’ it will then wait for a number, so let’s say somebody presses ‘5’, this determines how many minutes the toller will ring in total, then the next number they press, say ‘3’, will determine how many seconds BETWEEN individual rings. After these three button presses, it will close a relay circuit that will turn on a motor/generator unit. After the M/G has had enough time to spool up, it repeatedly closes the other relay’s circuits to energize the striker that hits the bell. It clicks the relays on the Uno with no trouble, but it only flashes the orange LED next to pin 13 on the Mega. The serial port counts down how many milliseconds are left when the toller is initialized, which works without issue on the Uno, but nothing on the Mega.

The reason I’m moving it to a Mega is that I am going to try to program a change ringing device based on his sample code here to use as well, so I need more pins, but I haven’t even tried working on that yet because I can’t even get the toller working.

Any help is very much appreciated. Let me know if any more info is needed.

Welcome to the forums! Congratulations for using code tags with your code!

First off, you do not describe how it is not working. Simply stating it is not working is not helpful.

Second, there is a huge difference between a keyboard and a keypad. You are using a keypad but you use the term keyboard. Totally different libraries for different applications.

Third, have you tried the simple keypad examples that come with the library to see if you are getting the correct key presses? I'd start there before moving to your larger, more complex sketch

Thanks for the reply, blh64.

When I say it isn't working, I mean it literally does nothing. The serial monitor doesn't write anything and the pins are not being pulled low to close the circuit on the relays. If I unplug everything and plug it into the Uno, change the board and port accordingly and hit upload, it works like a charm. When I say "works" I mean it will pull one pin low and then repeatedly do the same to the other pin to work the relays (two-channel relay module). The relay just controls the mechanism that smacks a single bell over and over again and the keypad code is what allows anyone to set duration and intervals. It's really a very simple concept. It also prints on the serial monitor perfectly.

I apologize for my use of the word "keyboard." I meant to say keypad. Keyboards have NOTHING to do with this project. Long day at work, my brain is tired (:

Yes, I've done several keypad tutorials, and I used an example sketch where if you press any button on the keypad it will print whatever button I pressed on the serial monitor. This works on both the Uno and Mega. I'm at a loss as to why my project works on the Uno and not the Mega.

How are you powering the relay board?

I don't think that library does anything that's board-specific, just use digitalRead/Write, I'd suspect circuit differences. Double check every connection?

Check the compilation warnings for this though:
"Using pinMode() INPUT_PULLUP AVR emulation"
(should not appear for AVR based boards like Uno and Mega)

david_2018:
How are you powering the relay board?

Through the Arduino 5 volt pin. This is how it's being powered on both the Uno and Mega. The relay module has built-in optocouplers, but I know it should have an independent power source, which it will once I'm ready to start testing it in the church tower. Do you think this could have something to do with the sketch not working on the Mega as it does on the Uno?

MarkT:
I don't think that library does anything that's board-specific, just use digitalRead/Write, I'd suspect circuit differences. Double check every connection?

Check the compilation warnings for this though:
"Using pinMode() INPUT_PULLUP AVR emulation"
(should not appear for AVR based boards like Uno and Mega)

I've double and triple-checked the circuits and all seems fine. I've been wrestling with this problem for two days now.

I get no errors or warnings when I upload the sketch.

As of right now it's very simple:

pins 2-9 are used for the keypad. I know for certain they're plugged in correctly.

pins 11 & 12 are set as outputs to be used for the inputs on the relays.

5v and ground pins also going to the relay. That's all of the wiring in this project so far. If I unplug everything and plug it into the Uno instead of the Mega in exactly the same way it works.

Does the sketch work on the mega if you disconnect the relay module? There really is not sufficient power from the 5v pin to power a relay coil.

Do the LEDs on the relay board light?

david_2018:
Does the sketch work on the mega if you disconnect the relay module? There really is not sufficient power from the 5v pin to power a relay coil.

I agree that using the Arduino isn't the ideal power source, but it's proven sufficient for testing. I tested the relays with a simple pushbutton to see if the Arduino can do it and yes, they click. However, I just tried replacing the relays with LEDs and adjusting the code in this sketch accordingly to see if that works, but no, still no luck.

JCA34F:
Do the LEDs on the relay board light?

The green LED near the reset button and orange LED next to pin 13 stay lit. All of the LEDs flash momentarily when I upload the sketch. One strange thing though is that when I press *,(number),(number) which is supposed to set the thing in action, the orange LED next to pin 13 flashes once every time I punch in that sequence. It also does it when I try changing the output pins.

JCA34F:
Do the LEDs on the relay board light?

JCA34F:

Short answer is yes.