Go Down

Topic: EEPROM problem (Read 1 time) previous topic - next topic

Harnam Thakur

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.

Code: [Select]

#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);
  }
}



johnwasser

I think this corresponds to your description.

Code: [Select]

#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);
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Harnam Thakur

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


johnwasser


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:
Code: [Select]

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


to
Code: [Select]

  // 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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Harnam Thakur

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

Code: [Select]
// 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);
}

Go Up