Go Down

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

johnwasser


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) {  <<<< YOU CHANGED THIS LINE. Change it back
    // 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

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

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


johnwasser


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


Code: [Select]

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

Harnam Thakur

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.

johnwasser

How long is the 'first instant' where the buttons are ignored and the LEDs stay off?
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up