Am reading analog values over AO pin.
This pin is connected to my water / fuel probe.
Push button is connected to PIN 2
Question:
i want to compare last used value on A0 pin and value that will be recorded after i detect that the pin 2 has changed state from low to high. In more details, i want to detect push button state and if i read out that the button state has changed i should trigger "compare" function which will compare last recorded A0 value and any new values that will come ( if any ). Time stamp could be an option....
To do so i was thinking on using eeprom.write and read function.
So basically, I’m not doing anything else then value compare if i detect button change.
Btw. i should overwrite eeprom values or write / erase them because A0 will send values cca each 2 seconds.
Is eeprom approach good solution for such project ?
This is the test code that am using:
void loop() {
pinMode(ledPin, OUTPUT);
pinMode(carLockPin, INPUT);
//
int sensorValue = analogRead(0);
int eepromWriteValue= analogRead(0) / 4;
carLockState = digitalRead(carLockPin);
//
if (carLockState == HIGH) { // car is ON
// SEND SMS AND TURN LED ON
digitalWrite(ledPin, HIGH);
//********** Writing to EEPROM ***********
EEPROM.write(addrEEPROM, eepromWriteValue);
addrEEPROM = addrEEPROM + 1;
if (addrEEPROM == 512)
addrEEPROM = 0;
//*****************************************
}
else { // car is OFF
//*********** Reading from EEPROM *********
digitalWrite(ledPin, LOW);
valueRead = EEPROM.read(addressRead);
addressRead = addressRead + 1;
lcd.print(valueRead, DEC);
delay(500);
lcd.clear();
if (addressRead == 512)
addressRead = 0;
//*****************************************
}
void loop() {
pinMode(ledPin, OUTPUT);
pinMode(carLockPin, INPUT);
//
int sensorValue = analogRead(0);
int eepromWriteValue= analogRead(0) / 4;
carLockState = digitalRead(carLockPin);
//---------------------------------------------
if (carLockState != lastButtonState) {
if (carLockState == HIGH) { // car is ON
buttonPushCounter++;
digitalWrite(ledPin, HIGH);
//********** Writing to EEPROM ***********
EEPROM.write(addrEEPROM, eepromWriteValue);
addrEEPROM = addrEEPROM + 1;
if (addrEEPROM == 512)
addrEEPROM = 0;
Serial.println("on");
Serial.println("--------------------");
}
else { // car is OFF
//*********** Reading from EEPROM *********
valueRead = EEPROM.read(addressRead);
if (valueRead != lastSavedValue )
{
}
digitalWrite(ledPin, LOW);
//addressRead = addressRead + 1;
//lcd.clear();
//lcd.print(valueRead, DEC);
Serial.print(addressRead); Serial.print("\t");
Serial.print(valueRead, DEC); Serial.println();
delay(500);
if (addressRead == 512)
addressRead = 0;
//*****************************************
Serial.println("off"); Serial.println("--------------------");
}
}
lastButtonState = carLockState;
}
I changed the code as you suggested.
After few tests i run into few problems.
1.) probe should read the state all the time ( regardless if car is on or off ).
How can i do that and save the values to eeprom ?
2.) If i want to compare the last saved value with new / active probe value, how can i know which eeprom adress should i read. Can i read out just the last eeprom address ?
Do you really need to change the mode of these pins? If not, the pinMode() calls belong in setup().
if (carLockState != lastButtonState) {
There is no reason to suspect that these names should have any relationship. On the other hand, currLockState and prevLockState DO look like there is a relationship.
buttonPushCounter++;
Why? You never use this value.
1.) probe should read the state all the time ( regardless if car is on or off ).
How can i do that and save the values to eeprom ?
If that is the case, I can not imagine why you care whether the car is on or off.
2.) If i want to compare the last saved value with new / active probe value, how can i know which eeprom adress should i read. Can i read out just the last eeprom address ?
I can not imagine why you keep incrementing addresses. So, I can not help you with that.
If that is the case, I can not imagine why you care whether the car is on or off.
** because i will check the probe and sensor state and this can be done only when the car is off.
I can not imagine why you keep incrementing addresses. So, I can not help you with that.
** this is the part where i need help. I dont need to increment address but i dont know how to "rewrite" and read existing ( last saved value ).
Maybe this will help to explain the situation:
when the probe is in tank, program should read probe state
if the car is OFF and probe last saved value != to last read value
trigger alarm, because someone is stealing fuel or some other liquid from tank.
I have similar situations but with pumps. General idea is the same.
If the sensor PIN gets LOW and probe is still measuring (changing values) then we have problem.
Maybe there is simpler way to do this but am still not sure how to be sure that am reading last saved value from teh eeprom. This is just idea ( i will add state change later ):
I dont need to increment address but i dont know how to "rewrite" and read existing ( last saved value ).
Where do you put your car keys when you get home? Do you put them in a different place every day? I put mine in the same place every day. That way, I can find them.
If you don't (uselessly) change the address every time you store data, finding it is easy.