Hi everyone ,
I have a problem with my project .Strange my program works flawlessly when connected using USB but when powered using external power supply, I have to manually reset it for it to start working correctly. Once I do this, it works fine.
I have a Led to see when my programs works or no so i think i'm stuck in the setup() part but maybe not if someone can help me
You can see my code here :
#include <SD.h>
#include <TimeLib.h>
#include <mcp_can.h>
#include <SPI.h>
//*********************CAN Sheild declarations********************
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
#define CAN0_INT 2 // Set INT
MCP_CAN CAN0(53); // Set CS
//*********************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
//********************* Program SETUP *****************************
void setup()
{
Serial.begin(115200);
while(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK){
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
while (1); //wait here forever
}
SERIAL_PORT_MONITOR.println("CAN init ok!");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.println("MCP2515 Library Receive Example...");
if (SD.begin(SD_CS_PIN))
{
SERIAL_PORT_MONITOR.println("SD card is present & ready");
sprintf(filename, "can_%.03d.csv",i); //initialise filename
myFile = SD.open(filename, FILE_WRITE);
if (myFile) // it opened OK
{
myFile.println("Time,Pression,Temperature,co2_hydrogen,humidity"); //add header to file
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
}
delay(100);
}
//********************* Fonction ecrire dans Excel ***********************
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 LOOP ***************************
void loop()
{
if (digitalRead(buttonPin)== HIGH)
{
digitalWrite(ledPin, HIGH); // turn LED on
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits)
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame.
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);}
else
{
for(byte i = 0; i<len; i++)
{
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
Serial.println("____Pression_Kpa____");
uint16_t press_raw;
memcpy(&press_raw, &rxBuf[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.println(press_pascal, 2); // value to copy on excel
Serial.println("_____Temperature_____");
uint16_t temp_raw;
memcpy(&temp_raw, &rxBuf[2], 2); //buf[2] is the third byte in buf, 2 because you want 2 bytes
float temp_degC = (((temp_raw * 0.03125)-273)-3) ;
Serial.println(temp_degC, 2);
Serial.println("_____CO2_Hydrogen_____");
uint16_t co2_raw;
memcpy(&co2_raw, &rxBuf[4], 2); //buf[4] is the fifth byte in buf, 2 because you want 2 bytes
float co2_true = ((co2_raw * 1) - 0);
Serial.println(co2_true, 2);
Serial.println("_____Humidity_%_____");
uint16_t humi_raw;
memcpy(&humi_raw, &rxBuf[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.println(humi_pourcent, 2);
writeToFile(press_pascal, temp_degC, co2_true, humi_pourcent);
++reads_recorded;
}
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
Serial.println("off");
}
//********************* Creation new files excel ***********************
//create new filenames for every 5400 lines written to file
if(reads_recorded == 5400) { //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");
}
}