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.