0
\$\begingroup\$

Sending "1" starts the write operation into the eeprom every 1 second to the next available address, and sending "2" starts reading the written values into the eeprom. When there's power everything works but once i remove the power cable and plug it again , i try to send the "2" again so i can read the written data but there's none. Any suggestion?

    #include <Wire.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    #include <SparkFunBQ27441.h>
    
    #define EEPROM_ADR 0x50 // Address of the 24LC512 EEPROM
    #define ONE_WIRE_BUS 27  // Pin connected to the One-Wire bus
    
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    const unsigned int BATTERY_CAPACITY = 500;
    
    int C1 = 30;
    int C2 = 31;
    int C3 = 32;
    int C4 = 33;
    int C5 = 34;
    int C6 = 35;
    
    bool isCounting = false;
    unsigned long startTime;
    unsigned long lastWriteTime;
    const int writeInterval = 1000; // Write to EEPROM every 1 second
    const int inputDelay = 100; // Delay after reading serial input
    
    int eepromCount = 0; // Stores the number of written values
    
    // Address for the EEPROM flag (just after the data for each write)
    int flagAddress;
    
    void setup() {
      Wire.begin(); // Initialize I2C communication
      sensors.begin(); // Initialize temperature sensor
      lipo.begin(); // Initialize battery fuel gauge
      lipo.setCapacity(BATTERY_CAPACITY); // Set battery capacity
      Serial.begin(115200); // Start serial communication
    
      // Initialize flagAddress to point to the beginning of EEPROM
      flagAddress = 0;
    }
    
    void loop() {
      sensors.requestTemperatures(); // Read from temperature sensor
      unsigned int soc = lipo.soc(); // Get state of charge
      int tempC = sensors.getTempCByIndex(0); // Get temperature in °C
    
      // Check for serial input
      if (Serial.available() > 0) {
        char inputChar = Serial.read();
    
        // Start counting on '1'
        if (inputChar == '1') {
          if (!isCounting) {
            startTime = millis(); // Start timer
            lastWriteTime = millis(); // Set last write time
            isCounting = true; // Start counting
            eepromCount = 0; // Reset written values counter
            Serial.println("Counter started.");
          }
        }
    
        // Stop counting and read EEPROM on '2'
        else if (inputChar == '2') {
          if (isCounting) {
            isCounting = false; // Stop counting
            Serial.println("Counter stopped.");
            Serial.println("Reading EEPROM:");
    
            // Loop through all written values and read them
            for (int i = 0; i < eepromCount; i++) {
              int value1 = readValueFromEEPROM(i * 8);
              int value2 = readValueFromEEPROM(i * 8 + 1);
              int value3 = readValueFromEEPROM(i * 8 + 2);
              int value4 = readValueFromEEPROM(i * 8 + 3);
              int value5 = readValueFromEEPROM(i * 8 + 4);
              int value6 = readValueFromEEPROM(i * 8 + 5);
              int value7 = readValueFromEEPROM(i * 8 + 6);
              int value8 = readValueFromEEPROM(i * 8 + 7);
    
              // Print the read values
              Serial.print("Value1 read from EEPROM: ");
              Serial.println(value1);
    
              Serial.print("Value2 read from EEPROM: ");
              Serial.println(value2);
    
              Serial.print("Value3 read from EEPROM: ");
              Serial.println(value3);
    
              Serial.print("Value4 read from EEPROM: ");
              Serial.println(value4);
    
              Serial.print("Value5 read from EEPROM: ");
              Serial.println(value5);
    
              Serial.print("Value6 read from EEPROM: ");
              Serial.println(value6);
    
              Serial.print("Value7 read from EEPROM: ");
              Serial.println(value7);
    
              Serial.print("Value8 read from EEPROM: ");
              Serial.println(value8);
            }
          }
        }
      }
    
      // Write data to EEPROM every second if counting
      if (isCounting) {
        if (millis() - lastWriteTime >= writeInterval) {
          // Write temperature, state of charge, and additional values
          writeValueToEEPROM(eepromCount * 8, tempC);
          writeValueToEEPROM(eepromCount * 8 + 1, soc);
          writeValueToEEPROM(eepromCount * 8 + 2, C1);
          writeValueToEEPROM(eepromCount * 8 + 3, C2);
          writeValueToEEPROM(eepromCount * 8 + 4, C3);
          writeValueToEEPROM(eepromCount * 8 + 5, C4);
          writeValueToEEPROM(eepromCount * 8 + 6, C5);
          writeValueToEEPROM(eepromCount * 8 + 7, C6);
    
          // Write a flag indicating successful write just after the data
          writeFlagToEEPROM(flagAddress + eepromCount);
    
          Serial.print("Data written to EEPROM at address: ");
          Serial.println(eepromCount);
          eepromCount++; // Increment written value counter
          lastWriteTime = millis(); // Update last write time
        }
      }
    }
    
    // Function to write a value to the EEPROM
    void writeValueToEEPROM(int eeAddress, int value) {
      Wire.beginTransmission(EEPROM_ADR); // Start I2C transmission
      Wire.write((int)(eeAddress >> 8)); // Write MSB of address
      Wire.write((int)(eeAddress & 0xFF)); // Write LSB of address
      Wire.write(value); // Write data value
      Wire.endTransmission(); // End I2C transmission
      delay(10); // Allow time for EEPROM write cycle
    }
    
    // Function to write a flag to indicate successful write
    void writeFlagToEEPROM(int eeAddress) {
      Wire.beginTransmission(EEPROM_ADR); // Start I2C transmission
      Wire.write((int)(eeAddress >> 8)); // Write MSB of address
      Wire.write((int)(eeAddress & 0xFF)); // Write LSB of address
      Wire.write(1); // Write a non-zero value as a flag
      Wire.endTransmission(); // End I2C transmission
      delay(10); // Allow time for EEPROM write cycle
    }
    
    // Function to read a value from the EEPROM
    int readValueFromEEPROM(int eeAddress) {
      Wire.beginTransmission(EEPROM_ADR); // Start I2C transmission
      Wire.write((int)(eeAddress >> 8)); // Write MSB of address
      Wire.write((int)(eeAddress & 0xFF)); // Write LSB of address
      Wire.endTransmission(); // End I2C transmission
      Wire.requestFrom(EEPROM_ADR, 1); // Request one byte of data
    
      int result = -1;
      if (Wire.available()) { // Check if data is available
        result = Wire.read(); // Read the data byte
      }
    
      return result; // Return the read value
    }
\$\endgroup\$
3
  • \$\begingroup\$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. \$\endgroup\$
    – Community Bot
    Commented Dec 8, 2023 at 2:20
  • \$\begingroup\$ Your code specifically starts to overwrite the stored data from the beginning after a power cycle. One suggestion is to not do that, of course. But my guess is, not much of your data ends up written propely anyway, so it's not the first problem. \$\endgroup\$
    – Justme
    Commented Dec 8, 2023 at 6:11
  • \$\begingroup\$ @Justme any suggestions sir ? \$\endgroup\$
    – walid Bmd
    Commented Dec 8, 2023 at 22:26

0

Browse other questions tagged or ask your own question.