I cannot save the flowmeter values to the 24c04 integrated circuit and read them again. When I cut off the power of the Arduino, the total liter of water passed is reset.
My aim is not to reset the total liters when the power is cut.Can you help?
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <EEPROM.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define EEPROM_I2C_ADDRESS 0x50
volatile int flow_frequency; // sensor palsi
unsigned int litre;
float l_minute;
float toplam;
unsigned char flowsensor = 2; // Sensor girişi
unsigned long currentTime;
unsigned long cloopTime;
int memoryaddress = 0;
void flow () // Interrupt
{
flow_frequency++;
}
void setup()
{
// sensor giriş ayarları
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH);
Serial.begin(9600);
Wire.begin();
attachInterrupt(0, flow, RISING);// Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
float storedFlow = readEEPROM(EEPROM_I2C_ADDRESS,memoryaddress);
float toplam = storedFlow,l_minute;
lcd.begin();
}
void akissensor(){
currentTime = millis();
// Every second, calculate and print litres/hour
if(currentTime >= (cloopTime + 1000))
{
//flow_frequency = 0;
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_minute = (flow_frequency / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
l_minute = l_minute/60;
toplam=toplam + l_minute;
writeEEPROM(EEPROM_I2C_ADDRESS, memoryaddress, toplam);
delay(10);
float storedFlow = readEEPROM(EEPROM_I2C_ADDRESS,memoryaddress);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Toplam:");
lcd.print(storedFlow);
lcd.print(" L");
Wire.endTransmission();
flow_frequency = 0; // Reset Counter
}
}
void writeEEPROM(int deviceAddress, int memoryAddress, float value) {
union {
float floatValue;
byte byteArray[4];
} floatToBytes;
floatToBytes.floatValue = value;
Wire.beginTransmission(deviceAddress);
Wire.write((memoryAddress >> 8) & 0xFF); // High byte of memory address
Wire.write(memoryAddress & 0xFF); // Low byte of memory address
for (int i = 0; i < 4; i++) { // A float is 4 bytes
Wire.write(floatToBytes.byteArray[i]);
}
Wire.endTransmission();
delay(5); // Delay to ensure the EEPROM completes the write
}
float readEEPROM(int deviceAddress, int memoryAddress) {
union {
float floatValue;
byte byteArray[4];
} bytesToFloat;
Wire.beginTransmission(deviceAddress);
Wire.write((memoryAddress >> 8) & 0xFF); // High byte of memory address
Wire.write(memoryAddress & 0xFF); // Low byte of memory address
Wire.endTransmission();
Wire.requestFrom(deviceAddress, 4); // Request 4 bytes (for float)
for (int i = 0; i < 4; i++) {
if (Wire.available()) {
bytesToFloat.byteArray[i] = Wire.read();
}
}
return bytesToFloat.floatValue;
}
void loop ()
{
akissensor();
}
3 posts - 3 participants