I try it but same error :
CAN_programe:12:1: error: 'mcp2515_can' does not name a type mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
Yeah i use the same SPI_CS_PIN for both CAN() and CAN0()
Because on my project i use a arduino mega with a sparkun CANBUS connect to a sensor. And to resume my main code (1) in Seed_Arduino_CAN-master works only if i lauch before this code call MCP_loopback.ino (0) from library MCP_CAN_lib_master and i dont find why...
For my project I need to implement a single code in the board and no longer use a computer (only +12v power supply of arduino)
So I wanted to put the code 0 in the setup and the main code in the main 1 as we talk about just above 
the two code works great independently :
0
#include <mcp_can.h>
#include <SPI.h>
// CAN TX Variables
unsigned long prevTX = 0; // Variable to store last execution time
const unsigned int invlTX = 1000; // One second interval constant
byte data[] = {0xAA, 0x55, 0x01, 0x10, 0xFF, 0x12, 0x34, 0x56}; // Generic CAN data to send
// CAN RX Variables
long unsigned int rxId;
unsigned char len;
unsigned char rxBuf[8];
// Serial Output String Buffer
char msgString[128];
// CAN0 INT and CS
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(53); // Set CS to pin 10
void setup()
{
Serial.begin(115200); // CAN is running at 500,000BPS; 115,200BPS is SLOW, not FAST, thus 9600 is crippling.
// Initialize MCP2515 running at 16MHz with a baudrate of 500kb/s and the masks and filters disabled.
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
// Since we do not set NORMAL mode, we are in loopback mode by default.
//CAN0.setMode(MCP_NORMAL);
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
Serial.println("MCP2515 Library Loopback Example...");
}
void loop()
{
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();
}
if(millis() - prevTX >= invlTX){ // Send this at a one second interval.
prevTX = millis();
byte sndStat = CAN0.sendMsgBuf(0x100, 8, data);
if(sndStat == CAN_OK)
Serial.println("Message Sent Successfully!");
else
Serial.println("Error Sending Message...");
}
}
1
#include <SPI.h>
#include <SD.h>
#include <TimeLib.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");
}
}
it's very frustrating because everything is working fine but divided into two programs and I can't figure out how to do it if anyone can help me 