Human Cyborg Relations
Human Cyborg Relations

Reputation: 1244

Reading/writing to EEPROM - incorrect values

I'm using the Arduino IDE to program a ESP8266 board.

The idea is to play a sound of a random frequency for a random amount of time, and save the last frequency played to the EEPROM. Then, I have a watchdog ISR that restarts the board if the delay time is 4 seconds or more. When this happens and the board restarts, I want to play the last played frequency for 1 second, and then resume normal functionality again.

Here is my code,

#include <Ticker.h>
#include <EEPROM.h>
#define PIN_BUZZER 13 // the digital pin the Buzzer is attached to

PROGMEM const int freqs[] = {31, 49, 78, 123, 196, 311, 494, 784, 1245, 1976, 3136,
4978};

Ticker secondTick;

volatile int watchdogCount = 0;
volatile int freqIdx = 0; //the index that will store the last frequency before it restarts

int EEPROM_Addr = 42;

//The Watchdog Interrupt Service Routine (ISR)
void ISRwatchdog() {
  watchdogCount++;
  //The watchdog will be waken up when the couter reaches 4
  if (watchdogCount == 4) {
    ESP.restart(); //restarting the board
  }
}

void setup() {
  EEPROM.begin(4096);
  Serial.begin(115200);
  secondTick.attach(1, ISRwatchdog); //registering the watchdog ISR
  pinMode(PIN_BUZZER, OUTPUT);

  int prevFreq = EEPROM.read(EEPROM_Addr); // read previous frequency
  if (prevFreq != 255){
    Serial.println("Previous frequency found : ");
    Serial.println(prevFreq);
    analogWrite(PIN_BUZZER, 256);
    analogWriteFreq(prevFreq);
    delay(1000);
  }
}

void loop() {
  Serial.print("Watchdog counter = ");
  Serial.println(watchdogCount);
  watchdogCount = 0;

  int freq = freqs[random(0, 11)];
  Serial.print("Frequency: ");
  Serial.println(freq);
  Serial.println("Saving to EEPROM");
  EEPROM.write(EEPROM_Addr, freq);
  EEPROM.commit();

  // generating 50% PWM
  analogWrite(PIN_BUZZER, 256);
  analogWriteFreq(freq);

  //depending on the value of delay, the program may wake up the watchdog
  int delayTime = random(1, 5) * 1000;
  Serial.print("Delay time: ");
  Serial.println(delayTime);
  delay(delayTime);
}

The problem I'm facing is that values are either written to the EEPROM incorrectly or they are being read incorrectly. For example, here is some of the output I got,

Watchdog counter = 2
Frequency: 31
Saving to EEPROM
Delay time: 3000
Watchdog counter = 3
Frequency: 1245
Saving to EEPROM
Delay time: 4000

 ets Jan  8 2013,rst cause:2, boot mode:(3,0)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
Previous frequency found : 
221

The previous frequency in this case is incorrect.

And again in this output snippet,

Watchdog counter = 1
Frequency: 784
Saving to EEPROM
Delay time: 4000
Watchdog counter = 4
Frequency: 1976
Saving to EEPROM
Delay time: 1000

 ets Jan  8 2013,rst cause:2, boot mode:(3,0)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld
Previous frequency found : 
184

There are some cases where my output is correct, but that is rare.

Upvotes: 1

Views: 1536

Answers (1)

user149341
user149341

Reputation:

The EEPROM library for ESP8266 only stores one byte. That means it cannot store values over 255 -- the value you get back is least significant byte of the value you stored (i.e, freq % 256).

Consider storing the index of the value in the array instead of the value itself, e.g.

uint8_t idx = random(0, 11);
int freq = freqs[idx];
...
EEPROM.write(EEPROM_addr, idx);

and

uint8_t idx = EEPROM.read(EEPROM_addr);
int freq = freqs[idx];

Upvotes: 2

Related Questions