2

I am using an Arduino Uno, connected to a USB shield, a RFID shield(adafruit PN532), an LCD, EEPROM(24AA256) and a RTC module(DS1307). I will not post my code here because it is too large and it is separated in multiple files.

In my program, I realize that if my programs enters a certain functions, after entering function after function, if I use a delay() at the end of the function I am currently in, the arduino resets. An example of what I mean is below.

void a() { b(); }
void b() { c(); }
void c() { d(); }
void d()
{
  lcd_string("Testing", 0x80);
  delay(2000);      <---- Arduino resets at the delay here
}

At first, I thought it was because my dynamic memory was at 80%, and when I compiled, they said the Arduino might have some stability issues. So I modified my code such that my dynamic memory is now 57%. Problem still exist.

I thought maybe the delay() function has some overflow or something, so I tried replacing the delay with the following code.

unsigned long timing;

timing = millis();
timing += 2000;
while(millis() < timing);

The Arduino still resets.

Next, I thought maybe because my arduino is connected to my PC, some serial pin might have been causing the reset, so I used an external Power to power up the arduino and disconnected the USB. The arduino still resets.

Next, I thought maybe Timer1 might have been crashing with the delay() function, although the delay function uses Timer0 so I disabled my Timer1 . The arduino still resets.

Is there any other possibilities that I am missing out? My program storage space is at 69% which I believe shouldn't be an issue.

Edit

Here is my code for Timer1 ISR

ISR(TIMER1_OVF_vect)
{
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  OCR1A = 34286;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  TCCR1B |= (1 << CS12);  
  // enable timer compare interrupt
 TIMSK1 |= (1 << TOIE1);
 triggered = 1;
}

Any other interrupt of flags used are in the library header files. I am using the following external libraries
USB Host shield library 2.0
Adafruit PN532 master

Deckdyl
  • 103
  • 1
  • 2
  • 12
  • Thanks for the edit. How hard would it be for you to produce a [Minimal, verifiable and reproducible example](http://stackoverflow.com/help/mcve), so that others can test it? does the current issue disappear if you remove something apparently unnecessary from the sketch? – Patrick Trentin Feb 09 '17 at 15:39
  • Minimal is a good question. Would take me 2 hours or so, here is the link to my code. https://drive.google.com/file/d/0BzCRu6FuIiXJS1ZxVUhrT2N2ME0/view?usp=sharing – Deckdyl Feb 09 '17 at 15:43
  • Ok, just for the sake of clarity, when you say that you 'disabled your Timer1', you mean that none of those lines is ever executed, aka you commented out that code? – Patrick Trentin Feb 09 '17 at 15:45
  • I tried removing a lot of unnecessary stuff from the sketch already, not sure if that is all, but as far as I am working on, Majority of it is needed. The problem is in the administrator.cpp file, change_password() function. It is accessed from the main loop by entering the admin_login() function, same file, and then the admin_menu() function, same file, when successfully authenticated. – Deckdyl Feb 09 '17 at 15:47
  • Yes, I commented it out. Both the ISR and the initialization – Deckdyl Feb 09 '17 at 15:47
  • Just an extra note, any delay before the last delay does not cause any reset. – Deckdyl Feb 09 '17 at 15:51
  • 1
    There are two limits: FLASH and RAM. Missing RAM is causing "instability". Is that what you report as `program storage space is at 69% ` ? What happens if you simply add a local variable in function `void d();` Which size of a byte array is possibble? – datafiddler Feb 09 '17 at 15:52
  • 1
    Well, to produce a *minimal* example perhaps the best idea is to start with an empty sketch and start adding features, first and foremost a call to `delay()` .. and see at which point it crashes. – Patrick Trentin Feb 09 '17 at 15:55
  • I believe program storage is FLASH. They only report that instability may occur when my dynamic memory, which i believe is the RAM, has less than 512 bytes free. – Deckdyl Feb 09 '17 at 15:55
  • @PatrickTrentin I have been doing that for 2 days already, haven't the slightest clue. In the change_password function, the function enter_password is called twice. If i put the code from the enter_password function into the change_password function rather than calling it as a function, the arduino doesn't reset. But I still face the problem at other sections of the sketch. – Deckdyl Feb 09 '17 at 15:59
  • one less call function is one less data-frame on the stack, is there a way to check amount of free memory on Arduino at runtime? – Patrick Trentin Feb 09 '17 at 16:03
  • http://playground.arduino.cc/Code/AvailableMemory I used the following library above, as i progressed through my functions, I constantly had more than 700 bytes available. – Deckdyl Feb 09 '17 at 16:20
  • Any news? did you solve this issue? – Patrick Trentin Feb 26 '17 at 08:40

2 Answers2

0

A little sample to come close to RAM corruption ...

#define MEM_PER_LEVEL 50
#define TRY_TO_SURVIVE 10
void KillMe(int level) {
   byte dummy[MEM_PER_LEVEL];
   for ( byte  i = 0; i < MEM_PER_LEVEL; i++)
      dummy[i]= i;
   Serial.println(level);
   delay(1000);  // not sure why this would hurt more than others
   if (level < TRY_TO_SURVIVE) KillMe(level+1);
   for ( byte  i = 0; i < MEM_PER_LEVEL; i++) {
       if (dummy[i] != i) {
           Serial.println(F("corruption happened")); 
           while(1) {} // HALT
       }

  }
   if (level == 0) 
   Serial.println(F("survived"));
}
void setup() {
  Serial.begin(9600);
  KillMe(0);
}

void loop() { }
datafiddler
  • 1,755
  • 3
  • 17
  • 30
  • I tried your code a few times, had no problem on my arduino. I had to change the TRY_TO_SURVIVE to 30. When I did, the arduino restart once and on the second time, it hanged. When It restart, the amount of RAM available was less than 200. However when I tested my code, I had slightly more than 700 bytes free. So I doubt RAM corruption was the issue with the reset. – Deckdyl Feb 09 '17 at 18:51
0

I had the same problem - wherever I put a delay in my setup function the Arduino would restart.

For me, the problem was an instance of SoftwareSerial with invalid pin numbers.

SoftwareSerial mySerial(30, 31);

Anyone else landing on this question should check their pin numbers are appropriate for the board they're targeting. Not sure why the crash only happens if a delay is called, would be interested if anyone has insight into this!

Jethro
  • 3,029
  • 3
  • 27
  • 56