I'm writing a device configuration script using CAN. I have written other CAN scripts using arrays to store desired message data. Up until now, I haven't needed to overwrite this data.
One particular device has an extraordinarily large number of messages needing to be sent. Many of these messages follow a pattern in the data field, so, I'm attempting to iterate through these messages where possible.
My approach is to update a single array location with each iteration.
This one line of code is giving me problems:
payload2[0] = x;
I'm getting this error:
Compilation error: incompatible types in assignment of 'unsigned char' to 'unsigned char [2]'
Could someone please help me either make this work or suggest an alternative solution?
This code is stripped down:
#include <Arduino_CAN.h>
void setup() {
Serial.begin(9600);
while (!Serial) { }
if (!CAN.begin(CanBitRate::BR_250k))
{
Serial.println("CAN.begin(...) failed.");
for (;;) {}
}
Serial.println("ready");
}
unsigned char payload2[1][2] = {0x00,0x41};
unsigned char *payload = payload2[0];
uint32_t CAN_ID = 0;
uint8_t hold2 = 0x5F; //variable for new node IDs
void loop() {
hold2 = 0x60;
M01();
}
void M01(){
unsigned char x = 0;
CAN_ID=0x51;
sNS2(0);
for(int i = 0; i<=253; i++){
x = i;
Serial.println(x, HEX);
payload2[0] = x;
payload = payload2[0];
sNS2(0);
}
while(1);
}
void sNS2(char p){ //Sort-N-Send for an 2 databyte message,
int rc = 0;
if(sizeof(CAN_ID)==4){
CanMsg msg(CanExtendedId(CAN_ID), sizeof(payload2[p]), payload); //for CAN_ID, write CanExtendedID(CAN_ID) or CanStandardID(CAN_ID)
rc = CAN.write(msg);
Serial.println(msg);
}else{
CanMsg msg(CanStandardId(CAN_ID), sizeof(payload2[p]), payload); //for CAN_ID, write CanExtendedID(CAN_ID) or CanStandardID(CAN_ID)
rc = CAN.write(msg);
}
}