arduino-uno


Arduino Program Reset Bug


I'm using an Arduino Uno and ATMega328P to simply control a few LEDs with switches controlled by the user. However, after about 56 iterations through my main loop (or ~16 seconds) my program resets. I suspect it has something to do with the Watchdog timer, but even with it disabled via wtd_disable(); in my setup, the problem persists. The program does enter a loop that it can only exit if the user flips a switch. Any suggestions?
//Don't worry, I have all necessary libraries and variables set up.
void setup()
{
Serial.begin(9600); // start serial for output
Serial.println(i2c_init());
wdt_disable();
//pinMode(22,INPUT_PULLUP);
//pinMode(23,INPUT_PULLUP);
pinMode(wakePin, INPUT);
pinMode(ACPin, INPUT);
pinMode(PowerPin, INPUT);
pinMode(PowerLED, OUTPUT);
pinMode(ACLED, OUTPUT);
pinMode(Battery1LED, OUTPUT);
pinMode(Battery2LED, OUTPUT);
pinMode(WifiLED, OUTPUT);
pinMode(TesterLED, OUTPUT);
pinMode(EnableLED, OUTPUT);
attachInterrupt(0, wakeUpNow, LOW);
}
void loop()
{
digitalWrite(PowerLED, LOW);
// digitalWrite(ACLED, LOW); Exclude AC Power LED
digitalWrite(Battery1LED, LOW);
digitalWrite(Battery2LED, LOW);
digitalWrite(WifiLED, LOW);
digitalWrite(TesterLED, LOW);
digitalWrite(EnableLED, LOW);
Enable = 0;
Serial.println("Reset Complete");
int ACPower = digitalRead(ACPin);
digitalWrite(ACLED, ACPower);
int v1 = fetchWord(deviceAddress1, VOLTAGE);
int v2 = fetchWord(deviceAddress2, VOLTAGE);
int BatteryVoltage = max(v1,v2);
Serial.print("Highest Battery Voltage: ");
Serial.println(BatteryVoltage);
delay(250);
if((BatteryVoltage >= 7000) | (ACPower == 1)){
int PowerOK = digitalRead(PowerPin);
if (PowerOK == 0){
loop();
}else {
bulk();
}
}else{
loop();
}
}
//This is the main part of my code that is constantly looped through,
//and after 16 seconds, the program resets, going back to loop()
void bulk()
{
Enable = 1;
digitalWrite(EnableLED, HIGH);
int ACPower = digitalRead(ACPin);
digitalWrite(ACLED, ACPower);
//int Battery1State = BatteryState(deviceAddress1);
int Battery1State = 2; // Simulating low battery
switch (Battery1State){
case 1:
digitalWrite(Battery1LED, HIGH);
break;
case 2: //I can't run parallel code to control the blinking LED,
//so I toggle the LED every pass through. Case 2 blinks slowly
if(i >= 4){
toggleLED(Battery1LED);
i = 0;
} else {
i++;
}
break;
case 3: //case 3 blinks quickly
toggleLED(Battery1LED);
break;
}
int Battery2State = 3; // simulating a very low battery
switch (Battery2State){
case 1:
digitalWrite(Battery2LED, HIGH);
break;
case 2:
if(j >= 4){
toggleLED(Battery2LED);
j = 0;
} else {
j++;
}
break;
case 3:
toggleLED(Battery2LED);
break;
}
buttonState = digitalRead(wakePin); //button is HIGH by default
if(buttonState == HIGH){
Serial.println(count);
if(count == 0){
starttime = millis();
} else if (count == 54){
endtime = millis();
runtime = endtime - starttime;
Serial.print("System Run Time: ");
Serial.println(runtime);
}
count++;
int PowerOK = digitalRead(PowerPin);
digitalWrite(PowerLED, PowerOK);
delay(250);
//Repeat this code if power switch is on, restart if power is turned off
if(PowerOK == 0){
loop();
}else {
bulk();
}
I suspect - how appropriate for a bug posted in this site - a stack overflow. I haven't really tried to understand your entire code, but from what I see, both functions (loop and bulk) call either loop() or bulk() at their end. In essence, the end of those functions is never reached.
For starters, try removing every call to loop() inside your code:
Change the code at the end of the loop function
from:
if((BatteryVoltage >= 7000) | (ACPower == 1)){
int PowerOK = digitalRead(PowerPin);
if (PowerOK == 0){
loop();
}else {
bulk();
}
}else{
loop();
}
to:
if((BatteryVoltage >= 7000) | (ACPower == 1)){
int PowerOK = digitalRead(PowerPin);
for ( ; PowerOK != 0 ; )
bulk();
}
and remove completely the following code at the end of your bulk() function:
delay(250);
//Repeat this code if power switch is on, restart if power is turned off
if(PowerOK == 0){
loop();
}else {
bulk();
}
Background:
When you call a function in C, C++ and most other languages, the return address (i.e. the place in your code where execution should continue after the called function ends) is placed on a special part of memory called the stack. When the called function returns, the return address is removed from the stack and all is well. If another function is called before the first one returns, a new return address is added to the stack. If a function repeatedly calls itself without ever returning, eventually the entire stack space (a limited resource) is used up and something bad happens. This is what happens in your code: the functions loop() and bulk() never return, instead they do their thing and call either themselves or the other one ad infinitum.
In arduino, there is an implicit main() function more or less like the following:
void main(void)
{
// system initialisation code
...
...
...
// user code
setup() ;
for( ; ; )
loop() ;
}
That is, loop() is called continuously. There is no reason to call it again at its own end.
Hope this helps.
Never mind, I believe it was a Serial port overflow. The problem resolved itself when I deleted some "Serial.println"s at the end of my code that I thought were irrelevant.
For arduino, when starting the setup() gets called once, then loop is called repeatedly (whenever it completes..
As mentioned there is no need for loop to ever call itself..

Related Links

how to connect arduino ethernet to internet using my laptop's wifi connection
Arduino and motors
Why “fifoBuffer” values of indexes 2,3,6,7,10,11 are not passed in the teapotPacket[] array ?
Sending data from coordinator XBee(API) to router XBee(AT)
how can i add a while statement to this code for my binary game on the arduino uno
Arduino Programming in C: Why output is not the way it supposed to be?
Arduino serial port+delete key+string convert
how to connect wires of step motor 42BYGH403 to driver drv8825 pins
avr:atmega328bb doesn't define a 'build.board' preference. Auto-set to: AVR_ATMEGA328BB error in arduino bootloading
engines do not work in genuine arduino
Arduino code writing
Arduino SoftwareSerial library doesnt work
Ardiuno Uno R3 to wifi network using ESP8266-01 not responding and Wifi shield not Present
Arduino Uno with LCD and I2C showing strange characters
Xively and keypad.h
NRF24L01+ sometimes works and sometimes don't

Categories

HOME
testng
asp.net-core
zeromq
minimum-spanning-tree
gremlin
single-sign-on
nullpointerexception
lodash
read-eval-print-loop
microservices
packages
survey
gps
pivotal-cloud-foundry
uber-api
ios-charts
append
gz
fallback
jsprit
windows-7-x64
samoa
google-cloud-speech
openedx
zapier
visjs
scriptcs
core-text
realex-payments-api
tdd
chromebook
windows-error-reporting
maxmind
unboundid
preg-match
perlin-noise
openoffice.org
android-kernel
cloud-code
google-api-nodejs-client
wpf-controls
jquery-bootgrid
swift3.0.2
neuroscience
hue
android-fingerprint-api
preconditions
email-templates
hp-ux
core-plot
nand2tetris
node-sass
qcombobox
flickr-api
hibernate-tools
promela
dandelion
dropbox-php
setter
nxt
gridpane
probability-density
log4c
fuzzy-search
react-native-listview
feedback
wireshark-dissector
microbenchmark
ideamart
interrupted-exception
method-parameters
sailfish-os
sankey-diagram
kineticjs
python-green
clicktag
ng-animate
id3v2
preferences
android-radiobutton
xceed-datagrid
dexterity
random-seed
limejs
factory-method
bitsharp
isnullorempty
dice
getmessage
gwt-ext
zpt
sortable-tables
gamma
castle-monorail
substrings
ubuntu-9.04
paul-graham

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App