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. ![]()