2
votes

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

2
Thanks for the edit. How hard would it be for you to produce a Minimal, verifiable and reproducible example, so that others can test it? does the current issue disappear if you remove something apparently unnecessary from the sketch?Patrick Trentin
Minimal is a good question. Would take me 2 hours or so, here is the link to my code. drive.google.com/file/d/0BzCRu6FuIiXJS1ZxVUhrT2N2ME0/…Deckdyl
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
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
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

2 Answers

0
votes

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() { }
0
votes

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!