EEPROM memory save & reset save memory by one push switch

Hello Exparte's,

Hope you are all well.

I'm trying to make some arduino customize project but faching some problem to ready the code.

here is the code, where I want to save the last valu of the six LED's (LED 11, LED12, LED13, LED 14 LED15, LED16 ) LED's in EEPROM memory.

and also I need a special key for reset all the save data value's, then start the functin at it's starting possition again.

Please help me Exparte's, to complete the code.

Code:

const int BUTTON1 = 2;
const int BUTTON2 = 3;
const int BUTTON3 = 4;
const int BUTTON4 = 5;
const int BUTTON5 = 6;
const int BUTTON6 = 7;
const int LED1 = 8;
const int LED2 = 9;
const int LED3 = 10;
const int LED4 = 11;
const int LED5 = 12;
const int LED6 = 13;
const int LED11 = A0;
const int LED12 = A1;
const int LED13 = A2;
const int LED14 = A3;
const int LED15 = A4;
const int LED16 = A5;
int BUTTONstate1 = 0;
int BUTTONstate2 = 0;
int BUTTONstate3 = 0;
int BUTTONstate4 = 0;
int BUTTONstate5 = 0;
int BUTTONstate6 = 0;

void setup()
{
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(BUTTON3, INPUT);
pinMode(BUTTON4, INPUT);
pinMode(BUTTON5, INPUT);
pinMode(BUTTON6, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);

pinMode(LED11, OUTPUT);
pinMode(LED12, OUTPUT);
pinMode(LED13, OUTPUT);
pinMode(LED14, OUTPUT);
pinMode(LED15, OUTPUT);
pinMode(LED16, OUTPUT);

}

void loop()
{
digitalWrite(LED11, HIGH);
digitalWrite(LED12, HIGH);
digitalWrite(LED13, HIGH);
digitalWrite(LED14, HIGH);
digitalWrite(LED15, HIGH);
digitalWrite(LED16, HIGH);

LOOOP:

BUTTONstate1 = digitalRead(BUTTON1);
BUTTONstate2 = digitalRead(BUTTON2);
BUTTONstate3 = digitalRead(BUTTON3);
BUTTONstate4 = digitalRead(BUTTON4);
BUTTONstate5 = digitalRead(BUTTON5);
BUTTONstate6 = digitalRead(BUTTON6);

if (BUTTONstate1 == HIGH && BUTTONstate2 == HIGH )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED11, LOW);
digitalWrite(LED12, LOW);

}
else if (BUTTONstate1 == HIGH && BUTTONstate3 == HIGH )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED11, LOW);
digitalWrite(LED12, LOW);
digitalWrite(LED13, LOW);
}
else if (BUTTONstate1 == HIGH && BUTTONstate4 == HIGH )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED11, LOW);
digitalWrite(LED12, LOW);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);

}
else if (BUTTONstate1 == HIGH && BUTTONstate5 == HIGH )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED11, LOW);
digitalWrite(LED12, LOW);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);
digitalWrite(LED15, LOW);
}
else if (BUTTONstate1 == HIGH && BUTTONstate6 == HIGH )
{
digitalWrite(LED1, HIGH);
digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED11, LOW);
digitalWrite(LED12, LOW);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);
digitalWrite(LED15, LOW);
digitalWrite(LED16, LOW);
}
else if (BUTTONstate2 == HIGH && BUTTONstate3 == HIGH )
{

digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED12, LOW);
digitalWrite(LED13, LOW);

}
else if (BUTTONstate2 == HIGH && BUTTONstate4 == HIGH )
{

digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED12, LOW);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);

}
else if (BUTTONstate2 == HIGH && BUTTONstate5 == HIGH )
{

digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED12, LOW);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);
digitalWrite(LED15, LOW);

}
else if (BUTTONstate2 == HIGH && BUTTONstate6 == HIGH )
{

digitalWrite(LED2, HIGH);
digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED12, LOW);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);
digitalWrite(LED15, LOW);
digitalWrite(LED16, LOW);
}
else if (BUTTONstate3 == HIGH && BUTTONstate4 == HIGH )
{

digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);

}
else if (BUTTONstate3 == HIGH && BUTTONstate5 == HIGH )
{

digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);
digitalWrite(LED15, LOW);

}
else if (BUTTONstate3 == HIGH && BUTTONstate6 == HIGH )
{

digitalWrite(LED3, HIGH);
digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED13, LOW);
digitalWrite(LED14, LOW);
digitalWrite(LED15, LOW);
digitalWrite(LED16, LOW);
}
else if (BUTTONstate4 == HIGH && BUTTONstate5 == HIGH )
{

digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED14, LOW);
digitalWrite(LED15, LOW);

}
else if (BUTTONstate4 == HIGH && BUTTONstate6 == HIGH )
{

digitalWrite(LED4, HIGH);
digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED14, LOW);
digitalWrite(LED15, LOW);
digitalWrite(LED16, LOW);
}
else if (BUTTONstate5 == HIGH && BUTTONstate6 == HIGH )
{

digitalWrite(LED5, HIGH);
digitalWrite(LED6, HIGH);
digitalWrite(LED15, LOW);
digitalWrite(LED16, LOW);
}

else if (BUTTONstate1 == HIGH)
{
digitalWrite(LED1, HIGH);
digitalWrite(LED11, LOW);
}

else if (BUTTONstate2 == HIGH)
{
digitalWrite(LED2, HIGH);
digitalWrite(LED12, LOW);
}
else if (BUTTONstate3 == HIGH)
{
digitalWrite(LED3, HIGH);
digitalWrite(LED13, LOW);

}
else if (BUTTONstate4 == HIGH)
{
digitalWrite(LED4, HIGH);
digitalWrite(LED14, LOW);
}
else if (BUTTONstate5 == HIGH)
{
digitalWrite(LED5, HIGH);
digitalWrite(LED15, LOW);
}
else if (BUTTONstate6 == HIGH)
{
digitalWrite(LED6, HIGH);
digitalWrite(LED16, LOW);
}

else{

digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);

}
delay(1500);

goto LOOOP;

}

Your code is too long.

If you start putting numeric suffices on variable, it's usually a sign that you should be looking at using arrays.

If you start putting gotos in your code, it's often a sign that you're about to attract a lot of flak.

FLACK!!

Code tags?

Look up State Machine using the "Search on Arduino Forum" search box.

Oh.

Really? Using a goto? Flack! Flack! Flack!

How's it going on that state machine thingy?

If you only want to run that once put it at the end of your setup() function, then get rid of goto.

Oh, notice the use of code tags?

Hi @saddamdssr ,

I didn't quite understand your difficulty.
You say:
" I want to save the last value of the six LED's (LED 11, LED12, LED13, LED 14, LED15, LED16 ) LED's in EEPROM memory."

When do you want to save this data in EEPROM?

The arduino EEPROM has a limit of around 100,000 writes. (or would it be 10,000?).
So you can't keep recording all the time, because if each cycle of the program lasts a little more than 1500 ms, 100,000 recordings will be made in approximately 18 hours.

I modified your program to have the ports of the leds in a variable type int.
This variable you can write to EEPROM using the EEPROM.put(addr, Data) function.

RV mineirin


byte btBit = 0;
unsigned int ledBit = 0x0FC0;
//-----------------------------------------------------------------------
void setup()
{
//  Serial.begin(115200);
  for ( int i = 2; i < 8; i++)
  {
    pinMode(i, INPUT_PULLUP);
  }
  for ( int i = 8; i < 20; i++)
  {
    pinMode(i, OUTPUT);
  }
     //Serial.print("ledBit  "); Serial.print( ledBit,BIN );
}
//-----------------------------------------------------------------------
void loop()
{
  //  button       6 5   4 3 2 1
  //  Pin          7 6   5 4 3 2
  //  btBit    0 0 0 0   0 0 0 0

  //    Led    y y y y   16 15 14 13   12 11  6  5    4  3 2 1
  //    Pin    x x x x   19 18 17 16   15 14 13 12   11 10 9 8
  // ledBit    0 0 0 0    0  0  0  0    0  0  0  0    0  0 0 0

  ledBit = 0x0FC0;

  for ( int i = 2; i < 8; i++)
  {
    if (digitalRead(i))
    //if (!digitalRead(i))
      bitSet(btBit, i - 2);
    else
      bitClear(btBit, i - 2);
  }
  //Serial.print("Button  "); Serial.println(btBit, BIN);
  switch (btBit)
  {
    case 0x03:           // BT 1 and 2                  //   btBit     0 0 0 0   0 0 1 1
      ledBit |= 0x0003;  // On LED 1,2                  //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   0 0 1 1
      ledBit &= 0x0F3F;  // Off LED 11,12               //  !ledBit    0 0 0 0   1 1 1 1   0 0 1 1   1 1 1 1
      break;

    case 0x05:           // BT 1 and 3                  //   btBit     0 0 0 0   0 1 0 1
      ledBit |= 0x0007;  // On LED 1,2,3                //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   0 1 1 1
      ledBit &= 0x0E3F;  // Off LED 11,12,13            //  !ledBit    0 0 0 0   1 1 1 0   0 0 1 1   1 1 1 1
      break;

    case 0x09:           // BT 1 and 4                  //   btBit     0 0 0 0   1 0 0 1
      ledBit |= 0x000F;  // On LED 1,2,3,4              //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   1 1 1 1
      ledBit &= 0x0C3F;  // Off LED 11,12,13,14         //  !ledBit    0 0 0 0   1 1 0 0   0 0 1 1   1 1 1 1
      break;

    case 0x11:           // BT 1 and 5                  //   btBit     0 0 0 1   0 0 0 1
      ledBit |= 0x001F;  // On LED 1,2,3,4,5            //   ledBit    0 0 0 0   0 0 0 0   0 0 0 1   1 1 1 1
      ledBit &= 0x083F;  // Off LED 11,12,13,14,15      //  !ledBit    0 0 0 0   1 0 0 0   0 0 1 1   1 1 1 1
      break;

    case 0x21:           // BT 1 and 6                  //   btBit     0 0 1 0   0 0 0 1
      ledBit |= 0x003F;  // On LED 1,2,3,4,5,6          //   ledBit    0 0 0 0   0 0 0 0   0 0 1 1   1 1 1 1
      ledBit &= 0x03FF;  // Off LED 11,12,13,14,15,16   //  !ledBit    0 0 0 0   0 0 0 0   0 0 1 1   1 1 1 1
      break;

    case 0x06:           // BT 2 and 3                  //   btBit     0 0 0 0   0 1 1 0
      ledBit |= 0x0006;  // On LED 2,3                  //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   0 1 1 0
      ledBit &= 0x0E7F;  // Off LED 12,13               //  !ledBit    0 0 0 0   1 1 1 0   0 1 1 1   1 1 1 1
      break;

    case 0x0A:           // BT 2 and 4                  //   btBit     0 0 0 0   1 0 1 0
      ledBit |= 0x000E;  // On LED 2,3,4                //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   1 1 1 0
      ledBit &= 0x0C7F;  // Off LED 12,13,14            //  !ledBit    0 0 0 0   1 1 0 0   0 1 1 1   1 1 1 1
      break;

    case 0x12:           // BT 2 and 5                  //   btBit     0 0 0 1   0 0 1 0
      ledBit |= 0x001E;  // On LED 2,3,4,5              //   ledBit    0 0 0 0   0 0 0 0   0 0 0 1   1 1 1 0
      ledBit &= 0x087F;  // Off LED 12,13,14,15         //  !ledBit    0 0 0 0   1 0 0 0   0 1 1 1   1 1 1 1
      break;

    case 0x22:           // BT 2 and 6                  //   btBit     0 0 1 0   0 0 1 0
      ledBit |= 0x003E;  // On LED 2,3,4,5,6            //   ledBit    0 0 0 0   0 0 0 0   0 0 1 1   1 1 1 0
      ledBit &= 0x007F;  // Off LED 12,13,14,15,16      //  !ledBit    0 0 0 0   0 0 0 0   0 1 1 1   1 1 1 1
      break;

    case 0x0C:           // BT 3 and 4                  //   btBit     0 0 0 0   1 1 0 0
      ledBit |= 0x000C;  // On LED 3,4                  //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   1 1 0 0
      ledBit &= 0x0CFF;  // Off LED 13,14               //  !ledBit    0 0 0 0   1 1 0 0   1 1 1 1   1 1 1 1
      break;

    case 0x14:           // BT 3 and 5                  //   btBit     0 0 0 1   0 1 0 0
      ledBit |= 0x001C;  // On LED 3,4,5                //   ledBit    0 0 0 0   0 0 0 0   0 0 0 1   1 1 0 0
      ledBit &= 0x08FF;  // Off LED 13,14,15            //  !ledBit    0 0 0 0   1 0 0 0   1 1 1 1   1 1 1 1
      break;

    case 0x24:           // BT 3 and 6                  //   btBit     0 0 1 0   0 1 0 0
      ledBit |= 0x003C;  // On LED 3,4,5,6              //   ledBit    0 0 0 0   0 0 0 0   0 0 1 1   1 1 0 0
      ledBit &= 0x00FF;  // Off LED 13,14,15,16         //  !ledBit    0 0 0 0   0 0 0 0   1 1 1 1   1 1 1 1
      break;

    case 0x18:           // BT 4 and 5                  //   btBit     0 0 0 1   1 0 0 0
      ledBit |= 0x0018;  // On LED 4,5                  //   ledBit    0 0 0 0   0 0 0 0   0 0 0 1   1 0 0 0
      ledBit &= 0x09FF;  // Off LED 14,15               //  !ledBit    0 0 0 0   1 0 0 1   1 1 1 1   1 1 1 1
      break;

    case 0x28:           // BT 4 and 6                  //   btBit     0 0 1 0   1 0 0 0
      ledBit |= 0x0038;  // On LED 4,5,6                //   ledBit    0 0 0 0   0 0 0 0   0 0 1 1   1 0 0 0
      ledBit &= 0x01FF;  // Off LED 14,15,16            //  !ledBit    0 0 0 0   0 0 0 1   1 1 1 1   1 1 1 1
      break;

    case 0x30:           // BT 5 and 6                  //   btBit     0 0 1 1   0 0 0 0
      ledBit |= 0x0030;  // On LED 5,6                  //   ledBit    0 0 0 0   0 0 0 0   0 0 1 1   0 0 0 0
      ledBit &= 0x03FF;  // Off LED 15,16               //  !ledBit    0 0 0 0   0 0 1 1   1 1 1 1   1 1 1 1
      break;

    case 0x01:           // BT 1                        //   btBit     0 0 0 0   0 0 0 1
      ledBit |= 0x0001;  // On LED 1                    //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   0 0 0 1
      ledBit &= 0x0FBF;  // Off LED 11                  //  !ledBit    0 0 0 0   1 1 1 1   1 0  1 1   1 1 1 1
      break;

    case 0x02:           // BT 2                        //   btBit     0 0 0 0   0 0 1 0
      ledBit |= 0x0002;  // On LED 2                    //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   0 0 1 0
      ledBit &= 0x0F7F;  // Off LED 12                  //  !ledBit    0 0 0 0   1 1 1 1   0 1 1 1   1 1 1 1
      break;

    case 0x04:           // BT 3                        //   btBit     0 0 0 0   0 1 0 0
      ledBit |= 0x0004;  // On LED 3                    //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   0 1 0 0
      ledBit &= 0x07FF;  // Off LED 13                  //  !ledBit    0 0 0 0   1 1 1 0   1 1 1 1   1 1 1 1
      break;

    case 0x08:           // BT 4                        //   btBit     0 0 0 0   1 0 0 0
      ledBit |= 0x0008;  // On LED 4                    //   ledBit    0 0 0 0   0 0 0 0   0 0 0 0   1 0 0 0
      ledBit &= 0x0DFF;  // Off LED 14                  //  !ledBit    0 0 0 0   1 1 0 1   1 1 1 1   1 1 1 1
      break;

    case 0x10:           // BT 5                        //   btBit     0 0 0 1   0 0 0 0
      ledBit |= 0x0010;  // On LED 5                    //   ledBit    0 0 0 0   0 0 0 0   0 0 0 1   0 0 0 0
      ledBit &= 0x0BFF;  // Off LED 15                  //  !ledBit    0 0 0 0   1 0 1 1   1 1 1 1   1 1 1 1
      break;

    case 0x20:           // BT 6                        //   btBit     0 0 0 1   0 0 0 0
      ledBit |= 0x0020;  // On LED 6                    //   ledBit    0 0 0 0   0 0 0 0   0 0 1 0   0 0 0 0
      ledBit &= 0x07FF;  // Off LED 16                  //  !ledBit    0 0 0 0   0 1 1 1   1 1 1 1   1 1 1 1
      break;

    default:
      ledBit &= 0x0FC0;  // Off LED 1,2,3,4,5,6         //  !ledBit    0 0 0 0   1 1 1 1   1 1 0 0   0 0 0 0
      break;
  }
  //Serial.print("LED  "); Serial.println(ledBit, BIN);
  for (int i = 8; i < 20; i++)
  {
    //Serial.print("Port  "); Serial.print( i );
    //Serial.print("  READ  "); Serial.println(bitRead(ledBit, i - 8));
    digitalWrite(i, bitRead(ledBit, i - 8));
  }
  delay(1500);
}