The sketch is a bit of copy paste from an existing code but I am using CANbus
// define names for the 4 Digital pins On the Arduino
// These data pins link to 4 Relay board pins IN1, IN2, IN3
#include <mcp_can.h>
#include <SPI.h>
#include "FIU.h"
#include "Arduino.h"
FIU Ch1(46, 48, 43, A14, A1);
int SerialNr = 0;
long unsigned int CANrx;
long unsigned int CANtx_States;
long unsigned int CANtx_Errors;
long unsigned int CANtx_Measure;
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
int timeout = 0;
int starttime = 0;
unsigned long startMillis;
unsigned long startMillis_I;
unsigned long currentMillis;
unsigned long currentMillis_I;
unsigned long LEDMillis;
const unsigned long can_send_period = 100;
int stateA = 0;
int fiu_mode[] = {0,0,0,0,0,0,0,0};
int error[] = {0,0,0,0,0,0,0,0};
int FIU_Error_Code[19];
bool FIU_HW_Start_Check = false;
int HW_Check_ctr = 0;
bool overcurrent = false;
bool overtemp = false;
bool CAN_Init_Error = false;
bool CAN_Tout = false;
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(53);
#define DIP_1 43
#define DIP_2 45
#define DIP_4 41
#define DIP_8 47
#define LED_1 22
#define LED_2 24
//Analog Inputs Define
//Current sensor
int VpA = 185; // Millivolt pro Ampere (100 fĂĽr 20A Modul und 66 fĂĽr 30A Modul)
int sensorwert1= 0;
int sensorwert2= 0;
int Nullpunkt1 = 2500; // Spannung in mV bei dem keine Stromstärke vorhanden ist
int Nullpunkt2 = 2500; // Spannung in mV bei dem keine Stromstärke vorhanden ist
int Lernwert1 = 0;
int Lernwert2 = 0;
double SensorSpannung1 = 0;
double SensorSpannung2 = 0;
double Ampere1 = 0;
double Ampere2 = 0;
int mAmpere1 = 0;
int mAmpere2 = 0;
uint8_t LSB1 = 0;
uint8_t MSB1 = 0;
uint8_t LSB2 = 0;
uint8_t MSB2 = 0;
int mAmpere1_neu = 2500;
int mAmpere1_alt = 2500;
int mAmpere2_neu = 2500;
int mAmpere2_alt = 2500;
bool adapt_current_mesaurement = false;
unsigned long startMillis_adapt = 0;
int Sens_Curl1;
void setup() {
// put your setup code here, to run once:
// Initialise the Arduino data pins for OUTPUT
//Get Serial Nr.
//SerialNr = !digitalRead(DIP_1) + !digitalRead(DIP_2)*2 + !digitalRead(DIP_4)*4 + !digitalRead(DIP_8)*8; //for new version with mechanical version selector
SerialNr = 0; //for old Version without version selector on board
switch (SerialNr){
case 0:
CANrx = 0x100;
CANtx_States = 0x700;
CANtx_Errors = 0x701;
CANtx_Measure = 0x702;
break;
case 1:
CANrx = 0x101;
CANtx_States = 0x710;
CANtx_Errors = 0x711;
CANtx_Measure = 0x712;
break;
case 2:
CANrx = 0x102;
CANtx_States = 0x720;
CANtx_Errors = 0x721;
CANtx_Measure = 0x722;
break;
case 3:
CANrx = 0x103;
CANtx_States = 0x730;
CANtx_Errors = 0x731;
CANtx_Measure = 0x732;
break;
case 4:
CANrx = 0x104;
CANtx_States = 0x740;
CANtx_Errors = 0x741;
CANtx_Measure = 0x742;
break;
case 5:
CANrx = 0x105;
CANtx_States = 0x750;
CANtx_Errors = 0x751;
CANtx_Measure = 0x752;
break;
}
Serial.begin(115200);
// Initialize MCP2515 running at 8MHz with a baudrate of 500kb/s
if(CAN0.begin(MCP_ANY, CAN_500KBPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
{
CAN_Init_Error = true;
Serial.println("Error Initializing MCP2515...");
}
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
Serial.println("MCP2515 CAN BUS Receiver");
// Data sent by CAN BUS
startMillis = millis();
}
byte data_send[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte data_send_2[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte data_send_3[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
byte ctr_send = 0;
void loop() {
//delay(100);
//read Current sensors
sensorwert1 = analogRead(Sens_Cur1);
SensorSpannung1 = (sensorwert1 / 1024.0) * 200; // Hier wird der Messwert in den Spannungswert am Sensor umgewandelt.
Ampere1 = ((SensorSpannung1 - Nullpunkt1) / VpA); // Im zweiten Schritt wird hier die Stromstärke berechnet.
mAmpere1 = (Ampere1 * 1000)+2500;
mAmpere1_alt = mAmpere1_neu;
mAmpere1_neu = 0.1 * mAmpere1 + 0.9 * mAmpere1_alt;
sensorwert2 = analogRead(Sens_Cur2);
SensorSpannung2 = (sensorwert2 / 1024.0) * 200; // Hier wird der Messwert in den Spannungswert am Sensor umgewandelt.
Ampere2 = ((SensorSpannung2 - Nullpunkt2) / VpA); // Im zweiten Schritt wird hier die Stromstärke berechnet.
mAmpere2 = (Ampere2 * 1000)+2500;
mAmpere2_alt = mAmpere2_neu;
mAmpere2_neu = 0.1 * mAmpere2 + 0.90 * mAmpere2_alt;
MSB1 = (mAmpere1_neu >> 8); // bits 8 to 15
LSB1 = (mAmpere1_neu); // bits 0 to 7
MSB2 = (mAmpere2_neu >> 8); // bits 8 to 15
LSB2 = (mAmpere2_neu); // bits 0 to 7
data_send_3[0] = LSB1;
data_send_3[1] = MSB1;
data_send_3[2] = LSB2;
data_send_3[3] = MSB2;
if((4000 < (mAmpere1_neu-2500) or (mAmpere1_neu-2500) < -4000 or 4000 < (mAmpere2_neu-2500) or (mAmpere2_neu-2500) < -4000))
{
if(startMillis_I == 0)
{
startMillis_I = millis();
}
currentMillis_I = millis();
if(currentMillis_I - startMillis_I >= 600)
{
overcurrent = true;
startMillis_I = 0;
digitalWrite(_pinH, LOW);
digitalWrite(_pinL, LOW);
digitalWrite(_pinOp,LOW);
}
}
if((-50 < (mAmpere1_neu-2500)) and ((mAmpere1_neu-2500) < 50) and (-50 < (mAmpere2_neu-2500)) and ((mAmpere2_neu-2500) < 50) and overcurrent == true) //Reset overcurrent flg
{
overcurrent = false;
}
bitWrite(data_send_2[6],0,overcurrent);
if((true and fiu_mode[0] == 0) and (fiu_mode[1] == 0) and (fiu_mode[2] == 0) and (fiu_mode[3] == 0) and (fiu_mode[4] == 0) and (fiu_mode[5] == 0) and (fiu_mode[6] == 0) and (fiu_mode[7] == 0)) //Adaptiere Nullpunkt der Strommessung
{
if(startMillis_adapt == 0)
startMillis_adapt = millis();
if((millis() - startMillis_adapt) > 100)
{
Lernwert1 = ((mAmpere1_neu - 2500))/10;
Nullpunkt1 = max(min(Nullpunkt1 + Lernwert1, 2550),2450);
startMillis_adapt = 0;
Lernwert2 = ((mAmpere2_neu - 2500))/10;
Nullpunkt2 = max(min(Nullpunkt2 + Lernwert2, 2550),2450);
startMillis_adapt = 0;
}
}
currentMillis = millis();
if(HW_Check_ctr == 0)
{
FIU_HW_Check();
HW_Check_ctr = 1;
}
FIU_HW_Start_Check = HW_Error_active();
if(currentMillis - startMillis >= 100)
{
ctr_send = ctr_send +1;
if(ctr_send >= 15)
{
ctr_send = 0;
};
data_send_2[7] = ctr_send;
CAN0.sendMsgBuf(CANtx_States, 0, 8, data_send);
CAN0.sendMsgBuf(CANtx_Errors, 0, 8, data_send_2);
CAN0.sendMsgBuf(CANtx_Measure, 0, 8, data_send_3);
startMillis = millis();
}
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 == CANrx)
{
CAN_Tout = false;
timeout = 0;
starttime = 0;
Serial.println("Received msg " + rxId);
if(true) //SigA
{
if(stateA == 0 and (rxBuf[0] & 0x01) and !(rxBuf[0] & 0x02) and !(rxBuf[0] & 0x04) and !SCG_active() and !Error_active())
{
stateA = 1;
}
if(stateA == 0 and !(rxBuf[0] & 0x01) and (rxBuf[0] & 0x02) and !(rxBuf[0] & 0x04) and !OL_active() and !Error_active())
{
stateA = 2;
}
if(stateA == 0 and !(rxBuf[0] & 0x01) and !(rxBuf[0] & 0x02) and (rxBuf[0] & 0x04) and !Error_active())
{
stateA = 3;
}
if(((rxBuf[0] & 0x01) and (rxBuf[0] & 0x02)) or ((rxBuf[0] & 0x01) and (rxBuf[0] & 0x04)) or ((rxBuf[0] & 0x02) and (rxBuf[0] & 0x04)) or overcurrent == true)
{
Serial.println("ERROR");
error[0] = 1;
stateA = 4;
}
switch(stateA) {
case 0:
Ch1.normalOperation();
data_send[0] = 0;
break;
case 1:
fiu_mode[0] = 1;
Ch1.openLoad();
data_send[0] = 1;
break;
case 2:
fiu_mode[0] = 2;
Serial.println("Case = 2");
Ch1.SBAT();
data_send[0] = 2;
break;
case 3:
fiu_mode[0] = 4;
Ch1.SGND();
data_send[0] = 4;
break;
}
else
{
if(starttime == 0)
starttime = millis();
else
timeout = millis() - starttime;
}
bool OL_active()
{
bool result;
if((fiu_mode[0] == 1) or (fiu_mode[1] == 1) or (fiu_mode[2] == 1) or (fiu_mode[3] == 1) or (fiu_mode[4] == 1) or (fiu_mode[5] == 1) or (fiu_mode[6] == 1) or (fiu_mode[7] == 1))
{
result = true;
}
else
{
result = false;
}
return result;
}
bool SCG_active()
{
bool result;
if((fiu_mode[0] == 2) or (fiu_mode[1] == 2) or (fiu_mode[2] == 2) or (fiu_mode[3] == 2) or (fiu_mode[4] == 2) or (fiu_mode[5] == 2) or (fiu_mode[6] == 2) or (fiu_mode[7] == 2))
{
result = true;
}
else
{
result = false;
}
return result;
}
bool SCB_active()
{
bool result;
if((fiu_mode[0] == 4) or (fiu_mode[1] == 4) or (fiu_mode[2] == 4) or (fiu_mode[3] == 4) or (fiu_mode[4] == 4) or (fiu_mode[5] == 4) or (fiu_mode[6] == 4) or (fiu_mode[7] == 4))
{
result = true;
}
else
{
result = false;
}
return result;
}
bool Error_active()
{
bool result;
if((error[0] != 0) or (error[1] != 0) or (error[2] != 0) or (error[3] != 0) or (error[4] != 0) or (error[5] != 0) or (error[6] != 0) or (error[7] != 0))
{
result = true;
}
else
{
result = false;
}
return result;
}
bool PinDiagOk(int Pin, int low, int high)
{
bool diag_status = false;
for(int i = 0; i < 5; i++)
{
if((analogRead(Pin) >= low) and analogRead(Pin) <= high)
{
diag_status = true;
break;
}
else
{
delay(10);
}
}
return diag_status;
}
}