Editorial Solution

  • From the task, we can understand that we have to keep track of the number of times the microcontroller(in our case Arduino UNO) is restarted.
  • Since we have to store the value we have multiple storage options, such as flash, RAM, EEPROM, etc. 
  • Out of these EEPROM will keep the value even if the controller gets reset/powered off.
    • We can also use ROM (program memory) in some controllers to write, however, it has very much fewer writing cycles than EEPROM, also writing to ROM is risky & complex as well.
    • So we have only the option of EEPROM and EEPROM is made for handling such cases.
  • At every startup of the microcontroller, we can program it in such a way that it will fetch the “Reset Count Value” from the EEPROM, increase it by one, and store it back in the same EEPROM location.
  • We will print the value on the serial monitor and also give the user the option to either reset the counter or not.
  • Based on the option selected by the user we can act accordingly.

Code

#include <EEPROM.h>


#define RESTART_COUNT_ADDR 0     // Address in EEPROM for restart count


void setup() {
  Serial.begin(9600);
  while (!Serial);


  Serial.println("EEPROM-Based Microcontroller Restart Counter");


  // Increment and read the restart counter
  int restartCount;
  EEPROM.get(RESTART_COUNT_ADDR, restartCount);
  restartCount++;
  EEPROM.put(RESTART_COUNT_ADDR, restartCount);


  Serial.print("Microcontroller restarted ");
  Serial.print(restartCount);
  Serial.println(" times.");


  // Provide option to reset the counter
  Serial.println("Do you want to reset the counter. Press Y or N");
  while(1)
  {
    if (Serial.available()) {
    char input = Serial.read();
    if (input == 'Y' || input == 'y') {
      Serial.println("Resetting restart counter...");
      EEPROM.put(RESTART_COUNT_ADDR, 0);
      Serial.println("Restart counter reset. Please restart the microcontroller.");
      break;
    }
    else{
      Serial.println("Counter not resetted");
      break;
    }
   
  }
  }
}


void loop() {
  Serial.println("Doing some task.......");
  delay(5000);
}

Code explanation 

Read, Increment, and Store Restart Count:

  • Use EEPROM.get() to read the current value from the defined address.
  • Increment the value and write it back to EEPROM using EEPROM.put().
int restartCount;
EEPROM.get(RESTART_COUNT_ADDR, restartCount); // Read current value
restartCount++;
EEPROM.put(RESTART_COUNT_ADDR, restartCount); // Write updated value
Serial.print("Microcontroller restarted ");
Serial.print(restartCount);
Serial.println(" times.");

User Option to Reset Counter:

  • Prompt the user via Serial Monitor.
  • Wait for input (Y/y to reset, anything else to continue).
Serial.println("Do you want to reset the counter? Press Y or N");
while (1) {
  if (Serial.available()) {
    char input = Serial.read();
    if (input == 'Y' || input == 'y') {
      Serial.println("Resetting restart counter...");
      EEPROM.put(RESTART_COUNT_ADDR, 0); // Reset counter
      Serial.println("Restart counter reset. Please restart the microcontroller.");
      break;
    } else {
      Serial.println("Counter not reset.");
      break;
    }
  }
}

 

Caution

Keep in mind the limited write cycle with the EEPROM cell.

Also, it is advisable to reset the EEPROM beforehand.

 

Output

 

Output Video


 

Submit Your Solution