MCP2515 not updating CAN output with new values?

I have a Nano and an MCP2515
I read the analog input signals
and I want to send them out over CAN to another Device
I can see in the Serial Monitor that it is Updating properly
but when I look at the Physical Can output, it does not update values in the CAN Stream

I can see that it is transmitting on the correct channel, and the signal looks good, just the values dont update…

I dont know if I’m missing something or doing something wrong in my code

the other device is a listen only device, not sending data back to the arduino

#include <Wire.h>

#include <SPI.h>
#include <mcp_can.h>

// Initialize Sensor Inputs /////////////////////////////////////

int sensorValue1 = 0;
int sensorValue2 = 0;
int sensorValue3 = 0;
int sensorValue4 = 0;
int sensorValue5 = 0;
int sensorValue6 = 0;

int Voltage1 = 0;
int Voltage2 = 0;
int Voltage3 = 0;
int Voltage4 = 0;
int Voltage5 = 0;
int Voltage6 = 0;

unsigned char canbyte01[8] {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char canbyte02[8] {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char canbyte03[8] {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char canbyte04[8] {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char canbyte05[8] {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char canbyte06[8] {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char canbyte07[8] {0, 0, 0, 0, 0, 0, 0, 0};
unsigned char canbyte08[8] {0, 0, 0, 0, 0, 0, 0, 0};






//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











//######################################################################################

void setup() {



  Serial.begin(115200);



  // init can bus, baudrate: 500k
  if (CAN.begin(CAN_500KBPS, MCP_8MHz) == CAN_OK) Serial.print("can init ok!!\r\n");
  else Serial.print("Can init fail!!\r\n");

}




//#######################################################################################




//########################################################################################

void loop(void)
{

  sensorValue1 = analogRead(A0);
  int Voltage1 = sensorValue1 * 0.488758553;
  sensorValue2 = analogRead(A1);
  int Voltage2 = sensorValue2 * 0.488758553;
  sensorValue3 = analogRead(A2);
  int Voltage3 = sensorValue3 * 0.488758553;
  sensorValue4 = analogRead(A3);
  int Voltage4 = sensorValue4 * 0.488758553;
  sensorValue5 = analogRead(A6);
  int Voltage5 = sensorValue5 * 0.488758553;
  sensorValue6 = analogRead(A7);
  int Voltage6 = sensorValue6 * 0.488758553;

  // For testing to Software Serial Manager
  // Serial.println("Sensor 1");
  // Serial.println(Voltage1);
  // Serial.println("Sensor 2");
  // Serial.println(Voltage2);
  // Serial.println("Sensor 3");
  // Serial.println(Voltage3);
  // Serial.println("Sensor 4");
  // Serial.println(Voltage4);
  // Serial.println("Sensor 5");
  // Serial.println(Voltage5);
  // Serial.println("Sensor 6");
  // Serial.println(Voltage6);


  //######################################################################################



  // send data:  id = 0x000, 0=standard frame 1= Extended frame, data len = 8, canbyte: data buf

  //Analog 1-4
  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);

  CAN.sendMsgBuf(0x047, 1, 8, canbyte01);

  Serial.print(canbyte01[0]);
  Serial.print("  ");
  Serial.print(canbyte01[1]);
  Serial.print("  ");
  Serial.print(canbyte01[2]);
  Serial.print("  ");
  Serial.print(canbyte01[3]);
  Serial.print("  ");
  Serial.print(canbyte01[4]);
  Serial.print("  ");
  Serial.print(canbyte01[5]);
  Serial.print("  ");
  Serial.print(canbyte01[6]);
  Serial.print("  ");
  Serial.print(canbyte01[7]);
  Serial.println("  ");
  Serial.println("  ");

  delay(100);

}

]

Also…
if I add a 10 seconds delay at the end of the loop — delay(10000);
I get 3 updates to the CAN values before the CAN stops updating and gets stuck on a value

Why do you have two sets of variables called “Voltage”?

Have you heard of for loops? (You’ve clearly heard of arrays - why not make better use of them, before you wear your keyboard out?)

Why is there so much whitespace?

TheMemberFormerlyKnownAsAWOL:
Why do you have two sets of variables called “Voltage”?

Have you heard of for loops? (You’ve clearly heard of arrays - why not make better use of them, before you wear your keyboard out?)

Why is there so much whitespace?

If you are going to Comment about my coding choices that you dont like, then please offer solutions to change it by showing me an example of what I should do instead

Whitespace…
I’m new to coding, I need the space to separate elements to make it easier for me to work on things
it doesnt change how the code works

literally my first project…
learning the things necessary to make my device work the way I need it to work
making the code smaller/prettier/more tidy/more efficient… can come much later once everything actually works properly to begin with
still learning…
consider that when making replies

literally my first project…

And I know this because . . ?
Number of posts?
Nope. That doesn’t work. (think about that one before you reply)

I don’t know many noobs who would chose CAN as a first project.

Most would work through the basics of programming simple stuff.

Consider that when asking questions.

TheMemberFormerlyKnownAsAWOL:
I don't know many noobs who would chose CAN as a first project.

Most would work through the basics of programming simple stuff.

Consider that when asking questions.

I suspect that it is a work project and he doesn't have the luxury of learn the basics which is unfortunate.

Some people want to "impress" their management with something "cheap".
"cheap" is great if the employee isn't trying to learn while on the job.
Work projects should be using proper equipment.
When I worked briefly on CAN, I bought commercial equipment.

.

scottydoodah:
I have a Nano and an MCP2515
I read the analog input signals
and I want to send them out over CAN to another Device

Basic tutorial which I suggest you read: Arduino MCP2515 CAN Bus Interface Tutorial - CAN Protocol

.

TheMemberFormerlyKnownAsAWOL:
And I know this because . . ?
Number of posts?
Nope. That doesn't work. (think about that one before you reply)

I don't know many noobs who would chose CAN as a first project.

Most would work through the basics of programming simple stuff.

Consider that when asking questions.

you know this because I'm informing you... not because I'm asking you to already know this...
I'm not saying you should have already known this....
I'm telling you this so you understand that it is my first project so you can help me better

I've actually worked thru the basics already in my project...
read analog inputs...very basic

part 2, send the signals to another device....

I chose CAN, because it fits the need for my simple project of taking some measurements and sending them out to another device to display on a screen
I already use CAN devices a lot, so CAN, is not the hard part
I've done process of elimination to try to eliminate a hardware issue on my end
and the only thing left is the hardware I am not familiar with or the coding which I am still learning

ieee488:
I suspect that it is a work project and he doesn't have the luxury of learn the basics which is unfortunate.

Some people want to "impress" their management with something "cheap".
"cheap" is great if the employee isn't trying to learn while on the job.
Work projects should be using proper equipment.
When I worked briefly on CAN, I bought commercial equipment.

.

the need is not work related, and I am not trying to impress anybody
but I need a relatively inexpensive solution for a problem, in a reasonably short time, as a temporary solution and this was recommended to me by a friend as an option he has used in the past for something else

theres actually quite a bit more to the code, and all of the code works just fine when I look at it in the serial monitor,
I'm trying to eliminate it being a problem with the code itself causing the values to stop changing on the physical CAN output

and I actually have found out its a hardware issue.....
thru some CAN sniffing I was able to determine that it was a handshaking issue, and rectified the problem...
code works the way I intended it to, THAT is what I was trying to confirm

as mentioned in my first post
I dont know if I'm missing something or doing something wrong in my code

a simple reply with an answer such as, your code looks correct and should function, but it could be improved upon to make it more efficient by doing "X" or "Y"....
that is all that should have been needed

if you dont know how to troubleshoot CAN, then you can stop there once the software side has been confirmed as "should be functional assuming no hardware issues"

How do you know you are transmitting on the correct channel and that the signal looks "good"?
Do you have a CAN bus analyzer hooked up?

ieee488:
Basic tutorial which I suggest you read: https://www.electronicshub.org/arduino-mcp2515-can-bus-tutorial/

.

I actually looked at that exact one when I was trying to understand how to make things transmit over CAN on Arduino Devices....

to be honest.....
not a very good example...
its actually not a relevant example to anything other than how to properly initialize and wire the card
anybody can transmit static values on can......
it would literally only be used for very basic troubleshooting of the connection between 2 devices at that point... which you can do with any data, could just be a string of 0's.... because hardware doesnt care about the data in the stream.. it only cares that you transmit/receive on the same rate and bit depth

I actually found the built in examples in the CAN libraries to be better for the programming side
and none of them actually explain using multiple bits for data so you can have values over 256 and that you have to use whole number intergers with can and multiply/divide in either end

ieee488:
How do you know you are transmitting on the correct channel and that the signal looks "good"?
Do you have a CAN bus analyzer hooked up?

yes...
I'm using a Pico Scope and I have another device to read CAN with as well when I want to see the values converted back to decimal values

Perhaps there is some sort of incompatibility.

The only way to know is to have a device that you know for certain works with the other device.

Right now, it can be your code that is a problem or it can be the CAN board you are using or something else.

When I was working with CAN, I used National Instrument boards valued at hundreds of dollars.

EDIT:
Do you have another MCP2515 board to try?

.

ieee488:
Perhaps there is some sort of incompatibility.

The only way to know is to have a device that you know for certain works with the other device.

Right now, it can be your code that is a problem or it can be the CAN board you are using or something else.

When I was working with CAN, I used National Instrument boards valued at hundreds of dollars.

EDIT:
Do you have another MCP2515 board to try?

.

I confirmed it is working now…
receiving hardware issue… handshaking on the receive side wasnt happening correctly…
I solved the issue

My code might not be the prettiest, but I have confirmed it works as needed.

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