How to write my projet on EEPROM Arduino Mega

Hello everyone on the forum.

In the rest of my project I want my program to remain in the arduino card even when I disconnect the power supply, so not to store it only in the flash memory. In order to no longer have to have a computer connected to my project.

I heard about an EEPROM memory in the card but how to do (in the coding) to write my code below in this memory.

Thanking you (the code works, I'm just trying to write it to memory):

#include <SPI.h>
#include <SD.h>
#include <TimeLib.h>
#include <EEPROM.h>
#include "mcp2515_can.h"

//*********************CAN Sheild declarations*********************

const int SPI_CS_PIN = 53;
const int CAN_INT_PIN = 2;
mcp2515_can CAN(SPI_CS_PIN);

//*********************SD Sheild declarations*********************

File myFile;
const int SD_CS_PIN = 9;

//*********************Record Excel declarations*********************

char filename[16];
unsigned long reads_recorded = 0;
uint16_t i = 0;

//*********************Switch and Led declarations*********************

const int buttonPin = 40;     // the number of the pushbutton pin
const int ledPin =  41;      // the number of the LED pin


void writeToFile(float press_pascal, float temp_degC, float co2_true, float humi_pourcent)
{
  myFile = SD.open(filename, FILE_WRITE);
  if (myFile) // it opened OK
  {
    myFile.print(String(minute()));
    myFile.print(":");
    myFile.print(String(second()));
    myFile.print(";");
    myFile.print(String(press_pascal, 2));
    myFile.print(";");
    myFile.print(String(temp_degC, 2));
    myFile.print(";");
    myFile.print(String(co2_true, 2));
    myFile.print(";");
    myFile.println(String(humi_pourcent, 2));
    myFile.close();
  }
  else
    SERIAL_PORT_MONITOR.println("Error opening file");
}

//********************* Program SETUP ***********************

void setup() {
  
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  
  SERIAL_PORT_MONITOR.begin(115200);

  while (CAN_OK != CAN.begin(CAN_500KBPS, MCP_16MHz)) { // init can bus : baudrate = 500k
    SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
    while (1); //wait here forever
  }
  SERIAL_PORT_MONITOR.println("CAN init ok!");

  if (SD.begin(SD_CS_PIN))
  {
    SERIAL_PORT_MONITOR.println("SD card is present & ready");

    //initialise filename
    sprintf(filename, "can_%.03d.csv", i);
    
    myFile = SD.open(filename, FILE_WRITE);
    if (myFile) // it opened OK
    {
      //add header to file
      myFile.println("Time,Pression,Temperature,co2_hydrogen,humidity");
      myFile.close();
      SERIAL_PORT_MONITOR.println("File created!");
    }
    else
      SERIAL_PORT_MONITOR.println("Error opening file");
  }
  else
  {
    SERIAL_PORT_MONITOR.println("SD card missing or failure");
    while (1); //wait here forever
  }
}

//********************* Program LOOP ************************

void loop() {
  unsigned char len = 0;
  unsigned char buf[8];

  if (CAN_MSGAVAIL == CAN.checkReceive()) {    // check if data coming
    CAN.readMsgBuf(&len, buf);                 // read data,  len: data length, buf: data buf

    unsigned long canId = CAN.getCanId();

    SERIAL_PORT_MONITOR.println("_____Pression_____");
    uint16_t press_raw;
    memcpy(&press_raw, &buf[0], 2); //buf[0] is the first byte in buf, 2 because you want 2 bytes
    float press_pascal = ((press_raw * 0.0078125) - 250);
    SERIAL_PORT_MONITOR.println(press_pascal, 2);  // value to copy on excel

    SERIAL_PORT_MONITOR.println("_____Temperature_____");
    uint16_t temp_raw;
    memcpy(&temp_raw, &buf[2], 2); //buf[2] is the third byte in buf, 2 because you want 2 bytes
    float temp_degC = ((temp_raw * 0.3125) - 273) / 100;
    SERIAL_PORT_MONITOR.println(temp_degC, 2);

    SERIAL_PORT_MONITOR.println("_____CO2_Hydrogen_____");
    uint16_t co2_raw;
    memcpy(&co2_raw, &buf[4], 2); //buf[4] is the fifth byte in buf, 2 because you want 2 bytes
    float co2_true = ((co2_raw * 1) - 0) / 100;
    SERIAL_PORT_MONITOR.println(co2_true, 2);

    SERIAL_PORT_MONITOR.println("_____Humidity_%_____");
    uint16_t humi_raw;
    memcpy(&humi_raw, &buf[6], 2); //buf[6] is the six byte in buf, 1 because you want 1 bytes
    float humi_pourcent = ((humi_raw * 0.4) - 0);
    SERIAL_PORT_MONITOR.println(humi_pourcent, 2);

    if (digitalRead(buttonPin) == HIGH) {
    digitalWrite(ledPin, HIGH);  // turn LED on
    writeToFile(press_pascal, temp_degC, co2_true, humi_pourcent);
    ++reads_recorded;}
    else {
    digitalWrite(ledPin, LOW); // turn LED off
  }
  }

  //********************* Creation new files excel ***********************
  
  //create new filenames for every 1800 lines written to file
  if (reads_recorded == 1800) {
    //reset counter
    reads_recorded = 0;

    //increment file counter
    ++i;
    
    //update finename
    sprintf(filename, "can_%.03d.csv", i);

    myFile = SD.open(filename, FILE_WRITE);
    if (myFile) // it opened OK
    {
      //add header to new file
      myFile.println("Time,Pression,Temperature,co2_hydrogen,humidity");
      myFile.close();
      SERIAL_PORT_MONITOR.println("New File created!");
    }
    else
      SERIAL_PORT_MONITOR.println("Error opening file");
  }

}

Thanking you for your kindness and your help, have a nice day. :slight_smile:

That's what the flash memory is for. EEPROM is non-volatile memory for storing data.

1 Like

When you upload a sketch it is stored in non volatile memory. You can remove the power to the Arduino and the sketch will remain in memory ready for the next time that power is restored

What makes you think that is not the case ?

1 Like

Ha damn I thought it was from memory but it's my code then...

I think then that it does not loop properly. Because once I do these steps:

My code should perform these steps:

  1. Check CAN + SD card connect well

  2. Create exel file in SD card

  3. Reading then writing in the data table

All with an ON/OFF button to activate or stop the recording phase

And when I put the SD card back, nothing works, the led seems to be blqouer and on the monitor everything is stopped.

I then tried to turn off and on again but the same nothing restarts as if the code does not loop...

I'm a little lost on the error.

Sounds like a corrupted SD card; your software checks for a SD card but is likely handing on the error condition. A very simple way to verify is to use a LED + resistor on any free Output pin. Then in setup after doing all the pin configuration requirements for the selected pin, turn on the LED before calling the SD card initialize routine. After the SD stuff turn off the LED.

Reformat your data card. Insert into Arduino, run your code. The LED should Illuminate, the LED should go off after a successful SD check.

SD can become corrupt by the programmer (you) not properly closing the SD file system (before power off.) You could use another free input pin to check for a button closure and close the SD file, illuminate the LED again, and enter an infinite loop indicating OK to power-off.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.