Deckdyl
Deckdyl

Reputation: 103

Why does delay() cause my arduino to reset?

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

Upvotes: 2

Views: 1733

Answers (2)

Jethro
Jethro

Reputation: 3359

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!

Upvotes: 0

datafiddler
datafiddler

Reputation: 1835

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

Upvotes: 0

Related Questions