Arduino Low Power corrupted integer value after a few minutes

Hello, I'm fairly new about 2-3 months into Arduino so forgive me if I'm missing something. I've spent a lot of time searching for anyone who has a related issue but I have not found any answers. I was following a tutorial on how to build a low-power e-paper display that refreshes every 24 hours. I've spent a lot of time understanding how this project works. I've found that sleep mode is working as expected except a variable im incrementing [refreshCounter] every time it wakes up is losing its value, meaning when I print to the console it shows up as a weird string [][]. This only happens after a decent amount of time waking and sleeping. It used to be an hour but now its a few minutes. This ruins the functionality and I've messed with a lot of things trying to get this to work, like putting a delay and uncommenting out the e-paper functionality.

#include "LowPower.h"

#include "epd.h"

const int wake_up = 6;

// const int reset = 5;

const int lcd_on = 4;

const int button = 3;

int refreshRate = 10800; //time between loading images. number you enter * 8 = seconds between refresh (10800 = 24h)

int counter = 1;

int refreshCounter = 0;

int ByteReceived;

bool errorFlag = false;

bool picSend = false;

bool picLoaded = false;

// bool buttonP() {

//   if (digitalRead(button) == HIGH) {

//     delay(500);

//     return true;

//   } else {

//     return false;

//   }

// }

void setup(void)

{
  Serial.begin(9600);

  pinMode(lcd_on, OUTPUT);

  pinMode(13, OUTPUT);

  // pinMode (buttonP, INPUT);

  digitalWrite(13, LOW);

  // attachInterrupt(1, buttonPr, RISING);

}

void loop(void) {

  DrawPic();

  counter++;

}

// void buttonPr() {

//   refreshCounter = refreshRate;

//   wakeUp();

// }

void wakeUp() {
  refreshCounter++;
  Serial.println(refreshCounter);
  if (refreshCounter < refreshRate ) enterSleep();

}

void DrawPic() {

  // digitalWrite(13, LOW);

  // //delay(2000);

  // digitalWrite(lcd_on, HIGH);

  // delay(300);

  // epd_init(wake_up, reset);

  // epd_wakeup(wake_up);

  // epd_set_memory(MEM_TF);

  // epd_clear();

  // digitalWrite(13, HIGH);

  // //int index = 7;

  // String indexStr = String(counter);

  // String str = 'i' + indexStr + ".BMP ";

  // char character[str.length()] ;

  // str.toCharArray(character, str.length());

  // epd_disp_bitmap(character, 0, 0);

  // epd_udpate();

  // epd_enter_stopmode();

  // while (1) {

  //   ByteReceived = Serial.read();

  //   if (ByteReceived == 13) {

  //     picSend = true;

  //   }

  //   if (ByteReceived == 69) {

  //     errorFlag = true;

  //     //if(!picSend && counter == 1) noSDcard();

  //     //if(picSend && counter == 1) noPic();

  //     //resetFunc();

  //     counter = 0;

  //     //Serial.flush();

  //     break;

  //   }

  //   if (ByteReceived == 75 && picSend && picLoaded) {

  //     break;

  //   }

  //   if (ByteReceived == 75 && picSend) {

  //     picLoaded = true;

  //   }

  // }

  // digitalWrite(13, LOW);

  // digitalWrite(lcd_on, LOW);

  // picLoaded = false;

  // errorFlag = false;

  // picSend = false;

  refreshCounter = 0;

  Serial.println("reset");
  enterSleep();

}

void enterSleep() {

  Serial.flush();

  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);

  wakeUp();

}

I think your problem is recursion.
Note that enterSleep() calls wakeUp() and wakeUp() calls enterSleep().
For the best it does it up to 10800 times!
Surely it runs out of stack and resets.

2 Likes

Hey, Maximo thanks for the reply! I believe you're correct, I'm not very familiar with how the stack works with Arduino. I put the functionality of these functions inside of the loop instead and it works!

2 Likes

This is a quick explanation. The stack is a pointer into memory (memory address) that will either increment or decrement (point to the next location) when putting or getting something from it. When you put something on the stack it writes the data at the memory location the stack pointer is pointing at then increments/decrements the stack pointer to the next location. It does this for each byte. When getting data back it reads from where it is pointing then increments/increments the pointer. The process is similar on different processors but you also have post/pre increment/decrement to make sure it gets the correct data. The stack pointer is generally a defined register in the processor. Some have more then one.

When a function gets called data is placed on the stack and removed when it returns. If there is no return it just keeps putting data in memory and updating the pointer. The result is you will eventually run out of and start overwriting other variables etc.

1 Like

Thanks a bunch!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.