I seem to have run into a bit of a snag with a project, and I’d like some insight into why this may be happening.
My project requires the use of a keypad, and to start I added some simple code to test functionality before getting in too deep.
The function I’m having problems with is called editRevokedLIST().
In my main program loop, if I place my keypad function where I would like it to be (deep within some if statements), it will restart my Mega once it reaches that function (goes through void setup() and all). However, it only does this when I try to use code relating to the keypad. If I put in code, say, to print something to Serial or the LCD, it works fine. I also tried running that function by itself (with the keypad code), and it works as advertised. Also note that the rest of the program also seems to be working as it should.
Main loop as I would like it to be (ignore all the debug and SRAMfree functions everywhere, I thought perhaps I was running out of SRAM, but that doesn’t seem to be the case.)
You can see the function about halfway through the code:
There is a lot of code there - is it possible to reproduce the problem in a simpler sketch? It will make it easier to track down, and along the way learning which pieces of code affect the problem will be useful information. Also, it's not obvious which section of code is the one which causes the problem to occur. If you know, can you point it out? Also please post the complete code, not just the parts that you think are relevant - if there's code which you know is irrelevant then remove it.
PeterH:
There is a lot of code there - is it possible to reproduce the problem in a simpler sketch? It will make it easier to track down, and along the way learning which pieces of code affect the problem will be useful information. Also, it's not obvious which section of code is the one which causes the problem to occur. If you know, can you point it out? Also please post the complete code, not just the parts that you think are relevant - if there's code which you know is irrelevant then remove it.
Do you have the hardware watchdog enabled?
Thanks for your reply, I will try to be more clear, and have edited my original post to reflect this. I do not have a hardware watchdog timer. I will have to edit down the full code, it is too long to post.
I have approx 5.5kb of RAM free right before running the function (while nested), according to the memory check library I used. It seems accurate, as when I add or remove strings from the code the available RAM adjusts accordingly.
I will divy up and post the rest of the code after work.
I purchased they keypad from adafruit, and am using their library. waitForKey blocks all code until a keypress is registerred. I have tried the other commands as well(getKey), with no change in result.
void SRAMfree()
{
Serial.print("freeMemory()=");
Serial.println(freeMemory());
}
// READ READ-ONLY TAG ID --------------------------------------------------------------------------------------------------
void readTagID()
{
int err; // define an int for the error code
do
{
while(RFIDserial.available() > 0)
{
RFIDserial.read();
}
currentMillis = millis();
if(currentMillis - previousTimeoutMillis > timeoutInterval) // time out if no card present or card not being read
{
timeout = true;
return;
}
else
{
RFIDserial.print("!RW"); //Call for RFID tag address
RFIDserial.write(byte(RFID_READ));
RFIDserial.write(byte(32));
while(RFIDserial.available() < 1); // wait for data to become available
err = RFIDserial.read();
}
}
while(err != 1); // repeat above until error code is 1 (ERR:OK)
while(RFIDserial.available() < 4); // wait for all 4 bytes of data to become available
int count = 0;
do
{
buffID[count++] = RFIDserial.read(); //read bytes until array size is reached
}
while(count < 4);
}
// READ STORED PASS CODE -------------------------------------------------------------------------------------------------
void readTagCODE()
{
int err; // define an int for the error code
do
{
while(RFIDserial.available() > 0)
{
RFIDserial.read();
}
currentMillis = millis();
if(currentMillis - previousTimeoutMillis > timeoutInterval) // time out if no card present or card not being read
{
timeout = true;
return;
}
else
{
RFIDserial.print("!RW"); //Call for RFID tag address
RFIDserial.write(byte(RFID_READ));
RFIDserial.write(byte(3));
while(RFIDserial.available() < 1); // wait for data to become available
err = RFIDserial.read();
}
}
while(err != 1); // repeat above until error code is 1 (ERR:OK)
while(RFIDserial.available() < 4); // wait for all 4 bytes of data to become available
int count = 0;
do
{
buffCODE[count++] = RFIDserial.read(); //read bytes until array size is reached
}
while(count < 4);
}
//READ NUMBER OF CHARACTERS STORED FOR NAME ARRAY -----------------------------------------------------------------------------
void readTagNAMELENGTH()
{
int err;
do
{
while(RFIDserial.available() > 0)
{
RFIDserial.read();
}
currentMillis = millis();
if(currentMillis - previousTimeoutMillis > timeoutInterval) // time out if no card present or card not being read
{
timeout = true;
return;
}
else
{
RFIDserial.print("!RW"); //Call for RFID tag address
RFIDserial.write(byte(RFID_READ));
RFIDserial.write(byte(31));
while(RFIDserial.available() < 1); // wait for data to become available
err = RFIDserial.read();
}
}
while(err != 1); // repeat above until error code is 1 (ERR:OK)
while(RFIDserial.available() < 4); // wait for all 4 bytes of data to become available
nameLength = RFIDserial.read();
}
//CALCULATE ENDING ADDRESS FOR NAME STORAGE---------------------------------------------------------------------------
void addressEND() // calculate the ending address for the name
{ // stored on the tag
addressEnd = 0;
if(nameLength == 4 ||
nameLength == 8 ||
nameLength == 12 ||
nameLength == 16 ||
nameLength == 20 ||
nameLength == 24 ||
nameLength == 28 ||
nameLength == 32)
{
addressEnd = (4 + (nameLength/4));
}
else
{
addressEnd = (5 + (nameLength/4));
}
}
//READ NAME ARRAY ------------------------------------------------------------------------------------------------------
void readTagNAME()
{
int err;
writeindex = 0; // reset array index to zero
for( // select which addresses to write the name to
address = 4;
address < addressEnd;
address++)
{
do
{
while(RFIDserial.available() > 0)
{
RFIDserial.read();
}
currentMillis = millis();
if(currentMillis - previousTimeoutMillis > timeoutInterval) // time out if no card present or card not being read
{
timeout = true;
return;
}
else
{
RFIDserial.print("!RW"); //Call for RFID tag address
RFIDserial.write(byte(RFID_READ));
RFIDserial.write(byte(address));
while(RFIDserial.available() < 1); // wait for data to become available
err = RFIDserial.read();
}
}
while(err != 1); // repeat above until error code is 1 (ERR:OK)
while(RFIDserial.available() < 4); // wait for all 4 bytes of data to become available
int count = 0;
do
{
buffNAME[writeindex++] = RFIDserial.read(); //read bytes until array size is reached
count++;
}
while(count < 4 && writeindex < nameLength);
}
}
//------------------------------------------------------------------------------------------------------------------------
I did not include functions like cueFail, cueSuccess etc. as all they do is print a few words to an LCD. And of course the main program loop as already been posted.