EEPROM problem

Hello there !
In the following code, I am having problem with the pumppin led when the power is restored.
The following are the working requirement and it is working OK
1.When all the three buttonPins(1,2,3) are OFF its corresponding leds are also OFF
and pumppin led is ON
2.When all buttonPins(1,2,3) are ON, its corresponding leds are also ON and pumppin led is OFF

Now I added EEPROM function to remember the pumpin led status so that in case of
power failure it remembers the pumppin status when the power is restored and switch ON/OFF
the pumppin led accordingly.

All the buttonPins are turning ON and OFF, its corresponding leds (working OK)
Removed the power from the system when pumpin led, led1 & led2 were ON
Restored the power to the system when only led1 was ON, the pumppin led also ON (working OK)

when the buttonPin3 was switches ON all the leds (led1,2,3) are ON and pumppin led is turned OFF,
(working OK)

When buttonPin3 is turned OFF, led3 is also turned OFF, led1 and led2 are ON (working OK)
but pumppin led also turned ON which is abnormal. pumppin led should come ON when
buttonPin 1 is ON.

This problem occurred after the EEPROM statments were added. Please help me to resolve this problem.

#include <EEPROM.h>
// constants won't change.
// INPUT PINS
#define buttonPin1 3
#define buttonPin2 4
#define buttonPin3 5


// OUTPUT PINS
#define ledPin1 8
#define ledPin2 9
#define ledPin3 10
#define pumppin 13

//EEPROM MEEMORY POSITIONS
#define memposL1 1
// variables will change:

int pumppinstatus =0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;


void setup() {

  // initialize the pushbutton pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(pumppin, OUTPUT);

  // pinMode(ledPin5, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);


  //Turn on the internal pull-up resistor, default state is HIGH
  digitalWrite(buttonPin1,HIGH);
  digitalWrite(buttonPin2,HIGH);
  digitalWrite(buttonPin3,HIGH);


  //Restore pumppin staus from EEPROM
  //check pumppin status

  pumppinstatus=EEPROM.read(memposL1);
  if(pumppinstatus=1){
    digitalWrite(pumppin,HIGH);
  }
  if(pumppinstatus=0){
    digitalWrite(pumppin,LOW);
  }
}

void loop(){

  buttonState1 = digitalRead(buttonPin1);// read the state of the pushbutton value:

  if (buttonState1 == LOW) { // check if the pushbutton is pressed.:

    digitalWrite(ledPin1, HIGH); // turn LED on:
    delay(100);
  }

  else {

    digitalWrite(ledPin1, LOW); // turn LED off:
    delay(100);
  }

  buttonState2 = digitalRead(buttonPin2);

  if (buttonState2 == LOW) {

    digitalWrite(ledPin2, HIGH);
  }
  else {

    digitalWrite(ledPin2, LOW);
  }

  buttonState3 = digitalRead(buttonPin3);

  if (buttonState3 == LOW) {

    digitalWrite(ledPin3, HIGH);
  }
  else {

    digitalWrite(ledPin3, LOW);
  }

  if (buttonState1 == LOW) { // check if the pushbutton is pressed.

    digitalWrite(pumppin, HIGH); // turn LED on:
  }

  if (((buttonState1 == LOW)&&(buttonState2 == LOW)&&(buttonState3 == LOW)))
  { // check if the pushbutton is pressed.

    digitalWrite(pumppin, LOW); // turn LED on:
  }

  if (buttonPin1 == LOW){
    EEPROM.write(memposL1,1);
  }

  if (buttonPin3 == LOW){
    EEPROM.write(memposL1,0);
  }
}

I think this corresponds to your description.

#include <EEPROM.h>
// constants won't change.
// INPUT PINS
const int buttonPin1 = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;


// OUTPUT PINS
const int ledPin1 = 8;
const int ledPin2 = 9;
const int ledPin3 = 10;
const int pumppin = 13;

//EEPROM MEEMORY POSITIONS
#define memposL1 1

// variables will change:
int pumppinstatus = LOW;
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;


void setup() {

  // initialize the output pins:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(pumppin, OUTPUT);

  // pinMode(ledPin5, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);

  // Restore pumppin staus from EEPROM
  // check pumppin status

  pumppinstatus=EEPROM.read(memposL1);
  digitalWrite(pumppin,pumppinstatus);
}

void loop(){

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  
  digitalWrite(ledPin1, buttonState1);
  digitalWrite(ledPin2, buttonState2);
  digitalWrite(ledPin3, buttonState3);
  
  // If all three buttons are HIGH and pump is running
  if (buttonState1 && buttonState2 && buttonState3 && pumppinstatus) {
    // Turn the pump off
    pumppinstatus = LOW;
    digitalWrite(pumppin, LOW);
    EEPROM.write(memposL1, LOW);
  }
    
  // If all three buttons are LOW and pump is not running
  if (buttonState1 && buttonState2 && buttonState3 && !pumppinstatus) {
    // Turn the pump on
    pumppinstatus = HIGH;
    digitalWrite(pumppin, HIGH);
    EEPROM.write(memposL1, HIGH);
  }

    delay(100);
}

Thanks Mr.johnwasser
But in both conditions
(1) When all three buttons are HIGH
(2) When all three buttons are LOW

The pumppin is always remaining ON

Oops. Change:

  // If all three buttons are LOW and pump is not running
  if (buttonState1 && buttonState2 && buttonState3 && !pumppinstatus) {

to

  // If all three buttons are LOW and pump is not running
  if (!buttonState1 && !buttonState2 && !buttonState3 && !pumppinstatus) {

I forgot to add the '!' (logical NOT) operator on the three buttons. Sorry.

Thanks Mr.johnwasser
After making the following changes, pumpin still always remaining ON

 // If all three buttons are HIGH and pump is running
  if (buttonState1 && buttonState2 && buttonState3 && !pumppinstatus) {
    // Turn the pump off
    pumppinstatus = LOW;
    digitalWrite(pumppin, LOW);
    EEPROM.write(memposL1, LOW);
  }

  // If all three buttons are LOW and pump is not running
  if (!buttonState1 && !buttonState2 && !buttonState3 && !pumppinstatus) {
    // Turn the pump on
    pumppinstatus = HIGH;
    digitalWrite(pumppin, HIGH);
    EEPROM.write(memposL1, HIGH);
  }

  delay(100);
}

Thank you very much Mr. johnwasser
Now the pumppin led status is OK when power is restored after the power failure.
but in the following code: When starting led1,2 and 3 are ON and pumppin led is OFF
where as it should be other way around i.e. led1,2,3 should be OFF and pumppin led ON

#include <EEPROM.h>
// constants won't change.
// INPUT PINS
const int buttonPin1 = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;


// OUTPUT PINS
const int ledPin1 = 8;
const int ledPin2 = 9;
const int ledPin3 = 10;
const int pumppin = 13;

//EEPROM MEEMORY POSITIONS
#define memposL1 1

// variables will change:
int pumppinstatus = LOW;
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;


void setup() {

  // initialize the output pins:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(pumppin, OUTPUT);

  // pinMode(ledPin5, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);

  // Restore pumppin staus from EEPROM
  // check pumppin status

  pumppinstatus=EEPROM.read(memposL1);
  digitalWrite(pumppin,pumppinstatus);
}

void loop(){

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  digitalWrite(ledPin1, buttonState1);
  digitalWrite(ledPin2, buttonState2);
  digitalWrite(ledPin3, buttonState3);

  // If all three buttons are HIGH and pump is running
  if (buttonState1 && buttonState2 && buttonState3 && pumppinstatus) {
    // Turn the pump off
    pumppinstatus = LOW;
    digitalWrite(pumppin, LOW);
    EEPROM.write(memposL1, LOW);
  }

  // If all three buttons are LOW and pump is not running
  if (!buttonState1 && !buttonState2 && !buttonState3 && !pumppinstatus) {
    // Turn the pump on
    pumppinstatus = HIGH;
    digitalWrite(pumppin, HIGH);
    EEPROM.write(memposL1, HIGH);
  }

  delay(100);
}
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  digitalWrite(ledPin1, buttonState1);
  digitalWrite(ledPin2, buttonState2);
  digitalWrite(ledPin3, buttonState3);

As you can see, the three LED outputs are controlled directly by the three 'button' inputs. Are the LEDs not properly reflecting the current state of the buttons?

Thanks Mr. johnwasser,
You are right. But my requirement is to keep the led1,2,3 OFF and pumpled1 ON at first instant.
As per my first code, but I like your code which is more clean, short and meaningful. How do I change
it to meet my requirement ? Actually, I was using my code to start and stop the water pump automatically and see the water level. Then I felt the necessity to use the EEPROM to co-up with the power failure factor.

How long is the 'first instant' where the buttons are ignored and the LEDs stay off?

When the system is commissioned for the first time. The tank is empty , if led1,2,3, which are suppose to indicate the level of the water are ON and pump is not starting to fill up the tank.

I don't know what you mean by 'commissioned for the first time'.

You have not given enough information for me to understand what you want done different.

Are the three 'buttons' actually water level sensors? If so I can understand why you want the pump to start wen the tank is empty (all three level sensors LOW) and stop when the tank is full (all three level sensors HIGH). It also makes some sense that once the pump has started you want the pump to continue to run after a power failure, even if the tank is partly full. I don't understand what else you want that is different.

Are the three 'buttons' actually water level sensors? If so I can understand why you want
the pump to start wen the tank is empty (all three level sensors LOW) and stop when the tank is
full (all three level sensors HIGH). It also makes some sense that once the pump has started you
want the pump to continue to run after a power failure, even if the tank is partly full.

Yes Sir,You are absoletly right.Now the situation is that when the tank is empty the level leds
are ON which means Tank is full and pump does not start.

The level LEDs are ON because the level sensors are reading HIGH. Why are the level sensors reading HIGH when the tank is EMPTY?!?

Thank you very much Mr. johnwasser
You have saved my day !

#include <EEPROM.h>
// constants won't change.
// INPUT PINS
const int buttonPin1 = 3;
const int buttonPin2 = 4;
const int buttonPin3 = 5;


// OUTPUT PINS
const int ledPin1 = 8;
const int ledPin2 = 9;
const int ledPin3 = 10;
const int pumppin = 13;

//EEPROM MEEMORY POSITIONS
#define memposL1 1

// variables will change:
int pumppinstatus = LOW;
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;


void setup() {

  // initialize the output pins:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(pumppin, OUTPUT);

  // pinMode(ledPin5, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);

  // Restore pumppin staus from EEPROM
  // check pumppin status

  pumppinstatus=EEPROM.read(memposL1);
  digitalWrite(pumppin,pumppinstatus);
}

void loop(){

  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  digitalWrite(ledPin1, !buttonState1);
  digitalWrite(ledPin2, !buttonState2);
  digitalWrite(ledPin3, !buttonState3);

  // If all three buttons are HIGH and pump is running
  if (buttonState1 && buttonState2 && buttonState3 && !pumppinstatus) {
    // Turn the pump off
    pumppinstatus = LOW;
    digitalWrite(pumppin, HIGH);
    EEPROM.write(memposL1, HIGH);
  }

  // If all three buttons are LOW and pump is not running
  if (!buttonState1 && !buttonState2 && !buttonState3 && pumppinstatus) {
    // Turn the pump on
    pumppinstatus = HIGH;
    digitalWrite(pumppin, LOW);
    EEPROM.write(memposL1, LOW);
  }

  delay(100);
}

This code has solved the problem.