Running into a dead end with as simple int and Eeprom

Hi guys,

Normally I wander here around and copy some hints and tips to get my simple projects working, it all worked fine until I ran into what I would think a simple issue but I'm trying already for a week without any real breakthroughs.

My project is about clicking a button and activating 8 relais one by one, I made it on wokwi with leds but the principle is the same, every time you click the next led will be lid. Kind of a loop with every button click. This I got to work: link: Blinking lights - Wokwi ESP32, STM32, Arduino Simulator

Now I want to store my integer into eeprom, so whenever the power is off, It will remember the last status, and will continue with every click, I have tried already some things but out of the blue my led's will run by itself, looks like the button is stuck in the program code.

Can someone help me out?! It would be highly appreciated the code below is what I have now

int ledPin[8] = { 4, 5, 6, 7, 8, 9, 10, 11};

const int buttonPin = 13;
int buttonState = HIGH;
int pushCounter = 0;
int numberOfLED = 8;
void setup() {
  pinMode(buttonPin, INPUT);
  for (int i = 0; i <= 8; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    for (int i = 0; i < numberOfLED; i++) {
      if (pushCounter % numberOfLED == i) {
        digitalWrite(ledPin[i], HIGH);
        delay(5000);
        digitalWrite(ledPin[i],LOW);
      }
      
      else {
        digitalWrite(ledPin[i], LOW);
      }
    }
    pushCounter++;
    delay(400);
  }
}

Where is that done in your code?

1 Like

To use EEPROM on an ESP32, see this link

This is what I got so far :frowning:

#include <EEPROM.h>

int ledPin[8] = { 4, 5, 6, 7, 8, 9, 10, 11};

const int buttonPin = 13;
int buttonState = HIGH;
int pushCounter = 0;
int numberOfLED = 8;

void setup() {

 Serial.begin(9600);


  pinMode(buttonPin, INPUT);
  for (int i = 0; i <= 8; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    for (int i = 0; i < numberOfLED; i++) {
      if (pushCounter % numberOfLED == i) {
        digitalWrite(ledPin[i], HIGH);
        delay(2000);
        digitalWrite(ledPin[i],LOW);
        EEPROM.update(0, ledPin[i]);
          int value1 = EEPROM.read(0);
          Serial.println(value1);
      }
      
      else {
        digitalWrite(ledPin[i], LOW);
      }
    }
    pushCounter++;
    delay(400);
  }
}

You are emulating an Arduino not the processor you are using.

Did you read and understand that link I posted?

The

Only writes bytes.

So you have to split your int variable into two bytes and write them in two adjacent addresses. Then to read them you have to read the two bytes and combine them into an integer variable.

Otherwise you have to use a different call.
You can find all the calls this library will support by reading the documentation on it.

Note this is for an Arduino AVR chip only.

The category you posted in is not correct, so I have moved it here.

Hi, sorry for the misplacement , will give your link a go.
Why do I need to use two bytes, I thought one byte could store a number up to 255?
My numbers are only from 4 till 12 :slight_smile:

Because you are using an int type variable. Use a byte or a char variable and you would not need to.

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