is there a way to update a CAN ID value dynamically? with a pin?

I have a project where I need the ability to offer multiple CAN configurations without a Reflash

My brain says this probably isnt an issue to do, but I am new and dont understand the way to do it properly

In short, what I want to do…
if no pins are grounded, use ID 0x046
if Pin D1 is grounded, use ID 0x047
if Pin D2 is grounded, use ID 0x048

this value appears at the end of my code
I have highlighted it in Bold to make it easier to see

#include <mcp_can.h>


//Can Board Initialize ////////////////////////////////////

// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
MCP_CAN CAN(10);                                      // Set CS to pin 10


int Voltage1;
int Voltage2;
int Voltage3;
int Voltage4;
int Voltage5;
int Voltage6;
int VoltageA;


void setup() {


  //Serial Computer Port Speed//
  Serial.begin(115200);

 //CAN Setup//
  //  START_INIT:
  CAN.begin(CAN_500KBPS,MCP_8MHz);
      while (CAN_OK != CAN.begin(CAN_500KBPS,MCP_8MHz))              // init can bus : baudrate = 500k
     {
        Serial.println("CAN BUS Shield init fail");
        Serial.println(" Init CAN BUS Shield again");
         delay(100);
    }
    Serial.println("CAN BUS Shield init ok!");

}

unsigned char canbyte01[8] = {1, 1, 1, 1, 1, 1, 1, 1};


void loop() {
 
  Voltage1 = analogRead(A0) * 0.488758553;

  Voltage2 = analogRead(A1) * 0.488758553;
 
  Voltage3 = analogRead(A2) * 0.488758553;
  
  Voltage4 = analogRead(A3) * 0.488758553;
  
  Voltage5 = analogRead(A6) * 0.488758553;
  
  Voltage6 = analogRead(A7) * 0.488758553;


//Analog 1-4
      // Multiplier .01
      canbyte01[0] = highByte(Voltage1);
      canbyte01[1] = lowByte(Voltage1);
      canbyte01[2] = highByte(Voltage2);
      canbyte01[3] = lowByte(Voltage2);
      canbyte01[4] = highByte(Voltage3);
      canbyte01[5] = lowByte(Voltage3);
      canbyte01[6] = highByte(Voltage4);
      canbyte01[7] = lowByte(Voltage4); 
      
      [b]CAN.sendMsgBuf(0x046, 1, 8, canbyte01);[/b]
      delay(.05);
}

I see no references to any D1, D2 in that code... please update the code so it at least addresses the pins you want to use. I think the answer is not too hard, but we shouldn't have to do all that tedious stuff. Then please re-post, don't go back and edit.

theres no reference, because im not adding it if its not doable…

I’ve already dumbed down my code to the bare minimum to make it so I can post the entire code because nobody on here seems to be capable of thinking in the snippets or the parts of code that require attention or help

feel free to add it yourself in an example…
I’m not redoing the entire code just because you cant add two lines in your head
to use as an example

int canswitch

canswitch = digitalRead(D1)

which then I can only guess it would be an If/then type of statement

if canswitch = non existant
make CAN.sendMsgBuf(“Change This Number”, 1, 8, canbyte01); equal value 1
if canswitch = grounded
make CAN.sendMsgBuf(“Change This Number”, 1, 8, canbyte01); equal value 2

but I need some help writing the part that would do this function

Pretty bad attitude for somebody asking for FREE HELP.

BTW, the reason I don't deal with snippets is that I always test compile my suggested solutions before posting them. So, if you can't be bothered to supply a complete MRE, I'll pass on it.

here you go…
I added the digital reads and canswitch1/canswitch2 pins

#include <mcp_can.h>


//Can Board Initialize ////////////////////////////////////

// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
MCP_CAN CAN(10);                                      // Set CS to pin 10


int Voltage1;
int Voltage2;
int Voltage3;
int Voltage4;
int Voltage5;
int Voltage6;
int VoltageA;
int canswitch1;
int canswitch2;


void setup() {


canswitch1 = digitalRead(2);
canswitch2 = digitalRead(3);

  //Serial Computer Port Speed//
  Serial.begin(115200);

 //CAN Setup//
  //  START_INIT:
  CAN.begin(CAN_500KBPS,MCP_8MHz);
      while (CAN_OK != CAN.begin(CAN_500KBPS,MCP_8MHz))              // init can bus : baudrate = 500k
     {
        Serial.println("CAN BUS Shield init fail");
        Serial.println(" Init CAN BUS Shield again");
         delay(100);
    }
    Serial.println("CAN BUS Shield init ok!");

}

unsigned char canbyte01[8] = {1, 1, 1, 1, 1, 1, 1, 1};


void loop() {
 
  Voltage1 = analogRead(A0) * 0.488758553;

  Voltage2 = analogRead(A1) * 0.488758553;
 
  Voltage3 = analogRead(A2) * 0.488758553;
  
  Voltage4 = analogRead(A3) * 0.488758553;
  
  Voltage5 = analogRead(A6) * 0.488758553;
  
  Voltage6 = analogRead(A7) * 0.488758553;


//Analog 1-4
      // Multiplier .01
      canbyte01[0] = highByte(Voltage1);
      canbyte01[1] = lowByte(Voltage1);
      canbyte01[2] = highByte(Voltage2);
      canbyte01[3] = lowByte(Voltage2);
      canbyte01[4] = highByte(Voltage3);
      canbyte01[5] = lowByte(Voltage3);
      canbyte01[6] = highByte(Voltage4);
      canbyte01[7] = lowByte(Voltage4); 
      //ID 70
      CAN.sendMsgBuf(0x046, 1, 8, canbyte01);
      delay(.05);
}
if (canswitch1 == LOW)
      CAN.sendMsgBuf(0x048, 1, 8, canbyte01);
else if (canswitch2 == LOW)
     CAN.sendMsgBuf(0x047, 1, 8, canbyte01);
else
     CAN.sendMsgBuf(0x046, 1, 8, canbyte01);

is there a way to do this with a single variable so it doesnt look at it every loop?

I want it to only read once at boot up

example... can I do this??
if (canswitch1 == LOW)
canChannel = 0x048;
else if (canswitch2 == LOW)
canChannel = 0x049;
else
canChannel = 0x049;

and later in my can routine section....
I can just do this?
CAN.sendMsgBuf(canChannel, 1, 8, canbyte01);

Yes, that should work. You should be using identifiers like 'canChannel' anyway, even if they are constants.

Do you really want to send a Canbus message every time through loop()? That would normally tie up the bus.

aarg:
Yes, that should work. You should be using identifiers like 'canChannel' anyway, even if they are constants.

Do you really want to send a Canbus message every time through loop()? That would normally tie up the bus.

well... how can I not send one every time thru the loop??
I'm here to learn...
I just need signal updates to reach the receiving device 100 times a second

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.