Hello! I am hoping someone can help out with this. I am using a SeedCAN shield on an Arduino Uno and need to send 4 CAN messages. One of them, BCM_R_G needs to change byte 2 based on switch input. I am not a code expert so everything I write is best described as "caveman code". but here it is
#include <SPI.h>
#define CAN_2515
// #define CAN_2518FD
// Set SPI CS Pin according to your hardware
#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN = BCM8;
const int CAN_INT_PIN = BCM25;
#else
// For Arduino MCP2515 Hat:
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
#endif
#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin
#endif
#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#endif
const int PowerPin = 3 ; // Power switch to wake up SCM (High Active)
const int VentLow = 4 ;// Vent Medium mode Pin (High Active)
const int VentHigh = 5 ;// Vent High Pin (High Active)
const int BackHeatMed =6 ;// Cushin Heat Medium Pin (High Active)
const int BackHeatHigh =7 ;// Cushin Heat High Pin (High Active)
const int SeatHeatMed = 8 ;// Seat Heat Medium Pin (High Active)
const int SeatHeatHigh = 0 ;// Seat Heat High Pin (High Active)
const int OnInd = 1 ;// Seat Heat High Pin (High Active)
int mode_2 = 0x00; // initialize mode Byte 2 data
void setup() {
SERIAL_PORT_MONITOR.begin(115200);
while(!Serial){};
while (CAN_OK != CAN.begin(CAN_500KBPS)) { // init can bus : baudrate = 500k
SERIAL_PORT_MONITOR.println("CAN init fail, retry...");
delay(100);
}
SERIAL_PORT_MONITOR.println("CAN init ok!");
pinMode(PowerPin, INPUT_PULLUP);
pinMode(VentLow, INPUT_PULLUP);
pinMode(VentHigh, INPUT_PULLUP);
pinMode(BackHeatMed, INPUT_PULLUP);
pinMode(BackHeatHigh, INPUT_PULLUP);
pinMode(SeatHeatMed, INPUT_PULLUP);
pinMode(SeatHeatHigh, INPUT_PULLUP);
pinMode(OnInd, OUTPUT);
unsigned char alwayson1[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char alwayson2[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char alwayson3[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char mode[8] = {0, 0, 0, 0, 0, 0, 0, 0};
}
void loop() {
//Vent Mode Select
if (digitalRead(VentLow)==HIGH && digitalRead(VentHigh)== HIGH)
mode_2 = 0x00;
else if (digitalRead(VentLow)==LOW && digitalRead(VentHigh)==HIGH)
mode_2 = 0x04;
else if (digitalRead(VentLow)==HIGH && digitalRead(VentHigh)== LOW)
mode_2 = 0x0C;
else
mode_2 = 0x00;
// Cushin and Back Heat Mode Select
if (digitalRead(digitalRead(VentLow)==HIGH && digitalRead(VentHigh)== HIGH))
{
if (digitalRead(BackHeatMed)==LOW && digitalRead(BackHeatHigh)==HIGH && digitalRead(SeatHeatMed) == HIGH && digitalRead(SeatHeatHigh)==HIGH)
mode_2 = 0x80;
else if (digitalRead(BackHeatMed)==HIGH && digitalRead(BackHeatHigh)==LOW && digitalRead(SeatHeatMed) == HIGH && digitalRead(SeatHeatHigh)==HIGH)
mode_2 = 0xC0;
else if (digitalRead(BackHeatMed)==HIGH && digitalRead(BackHeatHigh)==HIGH && digitalRead(SeatHeatMed) == LOW && digitalRead(SeatHeatHigh)==HIGH)
mode_2 = 0x20;
else if (digitalRead(BackHeatMed)==HIGH && digitalRead(BackHeatHigh)==HIGH && digitalRead(SeatHeatMed) == HIGH && digitalRead(SeatHeatHigh)==LOW)
mode_2 = 0x30;
else if (digitalRead(BackHeatMed)==LOW && digitalRead(BackHeatHigh)==HIGH && digitalRead(SeatHeatMed) == LOW && digitalRead(SeatHeatHigh)==HIGH)
mode_2 = 0xA0;
else if (digitalRead(BackHeatMed)==LOW && digitalRead(BackHeatHigh)==HIGH && digitalRead(SeatHeatMed) == HIGH && digitalRead(SeatHeatHigh)==LOW)
mode_2 = 0xB0;
else if (digitalRead(BackHeatMed)==HIGH && digitalRead(BackHeatHigh)==LOW && digitalRead(SeatHeatMed) == LOW && digitalRead(SeatHeatHigh)==HIGH)
mode_2 = 0xE0;
else if (digitalRead(BackHeatMed)==HIGH && digitalRead(BackHeatHigh)==LOW && digitalRead(SeatHeatMed) == HIGH && digitalRead(SeatHeatHigh)==LOW)
mode_2 = 0xF0;
}
if (digitalRead(PowerPin)==LOW)
{
digitalWrite(1, HIGH);
unsigned char mode[8] = {0, 0, mode_2, 0, 0, 0, 0, 0};
unsigned char alwayson1[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char alwayson2[8] = {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char alwayson3[8] = {0, 0, 0, 0, 0, 0, 0, 0};
CAN.sendMsgBuf(0x408,0,8,mode);
CAN.sendMsgBuf(0x409,0,8,alwayson1);
CAN.sendMsgBuf(0x400,0,8,alwayson2);
CAN.sendMsgBuf(0x600,0,8,alwayson3);
}
else
digitalWrite(1, LOW);
delay(100);
}
When I try to send the CAN messages, I notice only one is going through and the CAN bus load is way too hi, at 90%. So the bus seems flooded with the one message. I also cant stop the message from sending. I would like each CAN message to be sent once per loop. Even adding a delay in there doesnt seem to help. I suspect something is just causing the one message to keep going forever but I am not sure.
Any ideas? I have never used the CAN bus shield before but I used a mcp2515 CAN board before and didnt have this issue. So I am at a bit of a loss