Lost in EEPROMS?? Led control failure

Hi all,

I am trying to control the output level of some LEDS then store the values to the EEPRO memory and recall them after power down. This is my first attempt at using the EEPRO memory so slightly confused as to how it actually works. Any advice would be great I will attach my full code below.

#include <EEPROM.h>

int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 9;
int button = 7;
int pot = A0;
int lightCounter = 0;

int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;
int brightness4 = 0;

int level1 = 0;
int level2 = 0;
int level3 = 0;
int level4 = 0;

int address1 = 1;
int address2 = 2;
int address3 = 3;
int address4 = 4;

int updater = 10000;
int longPress = 2000;
unsigned long lastMillis = 0;
unsigned long lastMillis2 = 0;
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();

void setup() {
  Serial.begin(9600);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  
  pinMode(button, INPUT);
  pinMode(pot, INPUT);
}

void loop() {
  buttonRead();
  potRead();
  lightLevels();
  memory();
  
  Serial.print("Level1 = ");
  Serial.println(level1);
  Serial.print(" Level2 = ");
  Serial.println(level2);
  Serial.print(" Level3 = ");
  Serial.println(level3);
  Serial.print(" Level4 = ");
  Serial.println(level4);

  Serial.print("Brightness1 = ");
  Serial.println(brightness1);
  Serial.print(" Brightness2 = ");
  Serial.println(brightness2);
  Serial.print(" Brightness3 = ");
  Serial.println(brightness3);
  Serial.print(" Brightness4 = ");
  Serial.println(brightness4);
  Serial.println(pot);
  
}

 void buttonRead(){

if((digitalRead(button) == HIGH) && (lightCounter<4) && (currentMillis - lastMillis >= longPress)){
    lastMillis = millis();
    analogWrite(led1, 255);
    analogWrite(led2, 255);
    analogWrite(led3, 255);
    analogWrite(led4, 255);
    delay(300);
    analogWrite(led1, LOW);
    analogWrite(led2, LOW);
    analogWrite(led3, LOW);
    analogWrite(led4, LOW);
    delay(300);
    analogWrite(led1, 255);
    analogWrite(led2, 255);
    analogWrite(led3, 255);
    analogWrite(led4, 255);
    delay(300);
    analogWrite(led1, LOW);
    analogWrite(led2, LOW);
    analogWrite(led3, LOW);
    analogWrite(led4, LOW);
    delay(300);
    analogWrite(led1, 255);
    analogWrite(led2, 255);
    analogWrite(led3, 255);
    analogWrite(led4, 255);
    delay(300);
    analogWrite(led1, LOW);
    analogWrite(led2, LOW);
    analogWrite(led3, LOW);
    analogWrite(led4, LOW);
}
   
   if ((digitalRead(button) == HIGH)&&(lightCounter == 0)){
    analogWrite(led1, 255);
    delay(300);
    analogWrite(led1, LOW);
    delay(300);
    analogWrite(led1, 255);
    delay(300);
    analogWrite(led1, LOW);
    delay(300);
    analogWrite(led1, 255);
    delay(300);
    analogWrite(led1, LOW);
    delay(300);
    lightCounter = 1;
    delay(300);
  }

   if ((digitalRead(button) == HIGH)&&(lightCounter == 1)){
    analogWrite(led2, 255);
    delay(300);
    analogWrite(led2, LOW);
    delay(300);
    analogWrite(led2, 255);
    delay(300);
    analogWrite(led2, LOW);
    delay(300);
    analogWrite(led2, 255);
    delay(300);
    analogWrite(led2, LOW);
    delay(300);
    lightCounter = 2;
    delay(300);
    }

   if ((digitalRead(button) == HIGH)&&(lightCounter == 2)){
    analogWrite(led3, 255);
    delay(300);
    analogWrite(led3, LOW);
    delay(300);
    analogWrite(led3, 255);
    delay(300);
    analogWrite(led3, LOW);
    delay(300);
    analogWrite(led3, 255);
    delay(300);
    analogWrite(led3, LOW);
    delay(300);
    lightCounter = 3;
    delay(300);
    }
   if ((digitalRead(button) == HIGH)&&(lightCounter == 3)){
    analogWrite(led4, 255);
    delay(300);
    analogWrite(led4, LOW);
    delay(300);
    analogWrite(led4, 255);
    delay(300);
    analogWrite(led4, LOW);
    delay(300);
    analogWrite(led4, 255);
    delay(300);
    analogWrite(led4, LOW);
    delay(300);
    lightCounter = 0;
    delay(300);
    }
}
    
void potRead(){
  
  if (lightCounter == 1){
    level1 = analogRead(pot);
    brightness1 = map(level1,0,1023,0,255);
    analogWrite(led1, brightness1);
  }
    if (lightCounter == 2){
    level2 = analogRead(pot);
    brightness2 = map(level2,0,1023,0,255);
    analogWrite(led2, brightness2);
  }
  if (lightCounter == 3){
    level3 = analogRead(pot);
    brightness3 = map(level3,0,1023,0,255);
    analogWrite(led3, brightness3);
  }
  if (lightCounter == 0){
    level4 = analogRead(pot);
    brightness4 = map(level4,0,1023,0,255);
    analogWrite(led4, brightness4);
  }
  if (lightCounter == 4){
    level1 = analogRead(pot);
    brightness1 = map(level1,0,1023,0,255);
    brightness2 = map(level1,0,1023,0,255);
    brightness3 = map(level1,0,1023,0,255);
    brightness4 = map(level1,0,1023,0,255);
   }
}

void memory(){

if (currentMillis2 - lastMillis2 >= updater){
  
    if ((brightness1!=0)&&(brightness2!=0)&&(brightness3!=0)&&(brightness4!=0)){
      EEPROM.update(address1,brightness1);
      EEPROM.update(address2,brightness2);
      EEPROM.update(address3,brightness3);
      EEPROM.update(address4,brightness4);
      }
  
    if((EEPROM.read(1)!=0)&&(EEPROM.read(2)!=0)&&(EEPROM.read(3)!=0)&&(EEPROM.read(4)!=0)){
    brightness1 = EEPROM.read(1);
    brightness2 = EEPROM.read(2);
    brightness3 = EEPROM.read(3);
    brightness4 = EEPROM.read(4);
  }
 }
}

void lightLevels(){
  analogWrite(led1, brightness1);
  analogWrite(led2, brightness2);
  analogWrite(led3, brightness3);
  analogWrite(led4, brightness4);

delay(100);
}

You'd have a LOT less code if you learned to use arrays. Put the pin numbers in an array, pinNums. Put the brightness values in an array, brights. The level variables can be discarded. You don't need to keep track of the values read from the pots.

Then, use:

   int level = analogRead(pot);
   brights[lightCounter] = level/4;
   analogWrite(pinNums[lightCounter], brights[lightCounter]);

Make sure lightCounter stays in the range 0 to 3, not 1 to 4.

int address1 = 1;
int address2 = 2;
int address3 = 3;
int address4 = 4;

This is rather silly. If you had an array, addrs, it would look like:
int addrs[] = {1, 2, 3, 4}.

There is no reason to have an array when the nth value is n+1. Just have a baseAddress, and use baseAddress+0, baseAddresss+1, etc. When you use a for loop, then, to read the data, with an index variable named i, baseAddress+i will be the address to read on any given pass through the loop.

I really don't understand what you are trying to accomplish. In loop(), you call buttonRead(), which sets pin values. Then, you call potRead() which sets pin values. Neither function name indicates that it is going to diddle with output pins. Next, you call lightLevel() which also sets pin values, based on no new data. Finally, you call memory(), which has no Serial.print() statements, so you have no clue what is being written to, or read from, EEPROM.

Controlling leds with one button and one dimmer. Button flashes the led being controlled, pushing again selects the next led. Holding for 2 seconds flashes them all.

Save the selected brightness levels and recall after power up. Thats the plan anyway

I have tried to implement arrays like you suggested. Now the code no longer works as in when selecting an led with the button there is no flash to indicate which led is being controlled and the led brightness level is not "remembered" as such when moving on to the next led. can you see where I am going wrong?

#include <EEPROM.h>

int ledPins[] = {3,5,6,9};
int button = 7;
int pot = A0;
int lightCounter = 0;
int pinCount = 4;
int brightness = 0;
int address = 0;
int flashCount = 4;
int flashDelay = 300;
int pin;

int updater = 10000;

unsigned long lastMillis = 0;
unsigned long currentMillis = millis();

void setup() {
  Serial.begin(9600);
  for (int pin = 0; pin < pinCount; pin++){
    pinMode(ledPins[pin],OUTPUT);
  }
  pinMode(button, INPUT);
  pinMode(pot, INPUT);
}

void loop() {

    if((digitalRead(button) == HIGH)&&(lightCounter == 0)){
    for (int i = 0; i < flashCount; i++){
      digitalWrite(ledPins[pin],255);
      delay(flashDelay);
      digitalWrite(ledPins[pin],0);
    }
    lightCounter++;
    pin++;
   }

    else if((digitalRead(button) == HIGH)&&(lightCounter == 1)){
    for (int i = 0; i < flashCount; i++){
      digitalWrite(ledPins[pin],255);
      delay(flashDelay);
      digitalWrite(ledPins[pin],0);
    }
    lightCounter++;
    pin++;
    }

    else if((digitalRead(button) == HIGH)&&(lightCounter == 2)){
    for (int i = 0; i < flashCount; i++){
      digitalWrite(ledPins[pin],255);
      delay(flashDelay);
      digitalWrite(ledPins[pin],0);
    }
    lightCounter++;
    pin++;
    }

    else if((digitalRead(button) == HIGH)&&(lightCounter == 3)){
    for (int i = 0; i < flashCount; i++){
      digitalWrite(ledPins[pin],255);
      delay(flashDelay);
      digitalWrite(ledPins[pin],0);
    }
    lightCounter=0;
    pin=0;
   }
    
  brightness = map(analogRead(pot),0,1023,0,255);
  analogWrite(ledPins[pin], brightness);

  }


void memory(){

}

I don't think it's likely to affect your application, but I'd like to check that you are aware that you can ony (reliably) write to EEPROMs a finite number of times? It's a lot, but a simple mistake in your sketch can rapidly eat through the life expectancy!

pwatsoon:
Hi all,

I am trying to control the output level of some LEDS then store the values to the EEPRO memory and recall them after power down. This is my first attempt at using the EEPRO memory so slightly confused as to how it actually works. Any advice would be great I will attach my full code below.

#include <EEPROM.h>

int brightness1 = 0;
int brightness2 = 0;
int brightness3 = 0;
int brightness4 = 0;

int address1 = 1;
int address2 = 2;
int address3 = 3;
int address4 = 4;

// cut

void memory(){

if (currentMillis2 - lastMillis2 >= updater){
 
   if ((brightness1!=0)&&(brightness2!=0)&&(brightness3!=0)&&(brightness4!=0)){
     EEPROM.update(address1,brightness1);
     EEPROM.update(address2,brightness2);
     EEPROM.update(address3,brightness3);
     EEPROM.update(address4,brightness4);
     }
 
   if((EEPROM.read(1)!=0)&&(EEPROM.read(2)!=0)&&(EEPROM.read(3)!=0)&&(EEPROM.read(4)!=0)){
   brightness1 = EEPROM.read(1);
   brightness2 = EEPROM.read(2);
   brightness3 = EEPROM.read(3);
   brightness4 = EEPROM.read(4);
 }
}
}

Storing data use the EEPROM.h library has some limitations. as EEROMUpate() says:

Write a byte to the EEPROM. The value is written only if differs from the one already saved at the same address.

But a byte is not an int, an int is two bytes; So when you call:
EEPROMUpdate(address, value) you can save a single byte of data, values from 0 to 255.

You should actual be using EEPROMPut()

Write any data type or object to the EEPROM.

But you still need to control where you store and retrieve the data from. Here is a simple example:

void memoryupdate(){
int address=0; // start from the first byte of eeprom

//using EEPROMPut will only update the content of the EEPROM if the 'new' value is different from the old value

EEPROMPut(address,brightness1);
address = address + sizeof(brightness1); // how many byte does it take to store brightness1

EEPROMPut(address,brightness2);
address = address + sizeof(brightness2); // how many byte does it take to store brightness1

EEPROMPut(address,brightness3);
address = address + sizeof(brightness3); // how many byte does it take to store brightness1

EEPROMPut(address,brightness4);
address = address + sizeof(brightness4); // how many byte does it take to store brightness1

// address now contains the 'next' available eeprom cell to store another value.
}

void memoryrecall(){
int address=0; // start from the first byte of eeprom

//using EEPROMGet

EEPROMGet(address,brightness1);
address = address + sizeof(brightness1); // how many byte does it take to store brightness1

EEPROMGet(address,brightness2);
address = address + sizeof(brightness2); // how many byte does it take to store brightness1

EEPROMGet(address,brightness3);
address = address + sizeof(brightness3); // how many byte does it take to store brightness1

EEPROMGet(address,brightness4);
address = address + sizeof(brightness4); // how many byte does it take to store brightness1

// address now contains the 'next' available eeprom cell to store another value.
}

or as @PaulS say: use Arrays.

#define NUMLEDS 4

int brightness[NUMLEDS];

void memoryupdate(){

int address=0;

for(uint8_t i = 0; i<NUMLEDS; i++){
  EEPROMPut(address,brightness[i]);
  address = address + sizeof(brightness[i]);
  }
}

void memoryrecall(){

int address=0;

for(uint8_t i = 0; i<NUMLEDS; i++){
  EEPROMGet(address,brightness[i]);
  address = address + sizeof(brightness[i]);
  }
}

Chuck