CAN BUS Sending issues, im sure its something dumb i have in the code

I am trying to Send 4 analog signals to the can bus with bytes 1 and 2 analog 0, bytes 3 and 4 analog1, bytes 5 and 6 analog 2, and bytes 7 and 8 analog 3. I want it to output in a 0-500 (0-5.00volts) format. The code is working for bytes 1 and 2, but all the other bytes are mirroring analog 0. no matter what i try, all channels are mirroring bytes 1 and 2. any advice?

#include <mcp_canbus.h>
#include <SPI.h>

const int SPI_CS_PIN = 17;              // CANBed V1
// const int SPI_CS_PIN = 3;            // CANBed M0
// const int SPI_CS_PIN = 9;            // CAN Bus Shield

int Analog0_Raw;
int Analog0_Volt;
int Analog0_VoltHigh;
int Analog0_VoltLow;

int Analog1_Raw;
int Analog1_Volt;
int Analog1_VoltHigh;
int Analog1_VoltLow;

int Analog2_Raw;
int Analog2_Volt;
int Analog2_VoltHigh;
int Analog2_VoltLow;


int Analog3_Raw;
int Analog3_Volt;
int Analog3_VoltHigh;
int Analog3_VoltLow;


MCP_CAN CAN(SPI_CS_PIN);                                    

void setup()
{
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  
  CAN.begin(CAN_1000KBPS);
  Serial.begin(9600);
  }


void loop()
{
    unsigned char stmp[8] = {Analog0_VoltHigh, Analog0_VoltLow, Analog1_VoltHigh, Analog1_VoltLow, Analog2_VoltHigh, Analog2_VoltLow, Analog3_VoltHigh, Analog3_VoltLow,};


  Analog0_Raw = analogRead(A0);   
  Analog0_Volt = Analog0_Raw/2.046;
  Analog0_VoltHigh=(Analog0_Volt/256);
  Analog0_VoltLow=Analog0_Volt-(Analog0_VoltHigh*256);


  Analog1_Raw = analogRead(A1);   
  Analog1_Volt = Analog1_Raw/2.046;
  Analog1_VoltHigh=(Analog1_Volt/256);
  Analog1_VoltLow=Analog1_Volt-(Analog1_VoltHigh*256);

  Analog2_Raw = analogRead(A2);   
  Analog2_Volt = Analog2_Raw/2.046;
  Analog2_VoltHigh=(Analog2_Volt/256);
  Analog2_VoltLow=Analog2_Volt-(Analog2_VoltHigh*256);
  
  Analog3_Raw = analogRead(A3);   
  Analog3_Volt = Analog3_Raw/2.046;
  Analog3_VoltHigh=(Analog3_Volt/256);
  Analog3_VoltLow=Analog3_Volt-(Analog3_VoltHigh*256);     
  
    CAN.sendMsgBuf(0x91, CAN_STDID, 8, stmp);
   
    delay(10);   // send data per 10ms

Fill the array stmp[] AFTER all the values have been determined, not before.

1 Like

Like this? i tried this and results are the same.

   
#include <mcp_canbus.h>
#include <SPI.h>

const int SPI_CS_PIN = 17;              // CANBed V1
// const int SPI_CS_PIN = 3;            // CANBed M0
// const int SPI_CS_PIN = 9;            // CAN Bus Shield

int Analog0_Raw;
int Analog0_Volt;
int Analog0_VoltHigh;
int Analog0_VoltLow;

int Analog1_Raw;
int Analog1_Volt;
int Analog1_VoltHigh;
int Analog1_VoltLow;

int Analog2_Raw;
int Analog2_Volt;
int Analog2_VoltHigh;
int Analog2_VoltLow;


int Analog3_Raw;
int Analog3_Volt;
int Analog3_VoltHigh;
int Analog3_VoltLow;


MCP_CAN CAN(SPI_CS_PIN);                                    

void setup()
{
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  
  CAN.begin(CAN_1000KBPS);
  Serial.begin(9600);
  }


void loop()
{
  

  Analog0_Raw = analogRead(A0);   
  Analog0_Volt = Analog0_Raw/2.046;
  Analog0_VoltHigh=(Analog0_Volt/256);
  Analog0_VoltLow=Analog0_Volt-(Analog0_VoltHigh*256);


  Analog1_Raw = analogRead(A1);   
  Analog1_Volt = Analog1_Raw/2.046;
  Analog1_VoltHigh=(Analog1_Volt/256);
  Analog1_VoltLow=Analog1_Volt-(Analog1_VoltHigh*256);

  Analog2_Raw = analogRead(A2);   
  Analog2_Volt = Analog2_Raw/2.046;
  Analog2_VoltHigh=(Analog2_Volt/256);
  Analog2_VoltLow=Analog2_Volt-(Analog2_VoltHigh*256);
  
  Analog3_Raw = analogRead(A3);   
  Analog3_Volt = Analog3_Raw/2.046;
  Analog3_VoltHigh=(Analog3_Volt/256);
  Analog3_VoltLow=Analog3_Volt-(Analog3_VoltHigh*256); 

      unsigned char stmp[8] = {Analog0_VoltHigh, Analog0_VoltLow, Analog1_VoltHigh, Analog1_VoltLow, Analog2_VoltHigh, Analog2_VoltLow, Analog3_VoltHigh, Analog3_VoltLow,};
  
    CAN.sendMsgBuf(0x91, CAN_STDID, 8, stmp);
   
    delay(10);   // send data per 10ms

    Serial.println (Analog3_Raw);

Put in Serial.print() statements to see the actual analog values. Raw is fine; maybe some are in fact zero.

These are all defined as being of type int. That means they only hold whole numbers.

So that gets rounded down. Any decimal part just gets truncated off. You just get the whole number part.

Same here. The result of that division will likely be less than 1 so it just gets truncated to 0.

And then this calculation is all messed up because those other two values aren't right.

It also gives me this error on ever int before sending the code.

warning: narrowing conversion of 'Analog2_VoltHigh' from 'int' to 'unsigned char' inside { }

that's ok. It's a warning and you are aware. You've shifted all your values down to the low 8 bits. It's just warning you in case you didn't know that you were turning an int into two characters. But you did it on purpose.

If you don't want to see it, then recast the int as an array of two char.

But it won't have any effect on your problem.

i need it to come back as whole numbers, that part of the code works fine for analog 0 to bytes 1 and 2. I have a pot on it and it reads 0 at one end and 500 at another end. exactly how I want it to read. the issue is, when I look at bytes 3 and 4, they also follow the pot that is on analog0 and I have it disconnected.

im a beginner on this so any help is appreciated. thanks

Yes. But you also need to make sure you don't lose something in the calculation.

Throw a line in there to print them all before you send them to the CAN bus. What are the values before you send them.

when i put Analog1_Raw, or (2 or 3) in the serial monitor it follows what Analog0_Raw is doing. this is before any conversions or anything. should just be whatever the pin is reading. if i put a voltmeter on the pin it reads 0v.

Show.

Here is what the serial is doing when i run this code. Note that analog1,2 and 3 are all reading 0 voltage on a meter and are not hooked up to anything.

14:14:40.732 -> Analog 1:959
14:14:40.732 -> Analog 2:904
14:14:40.732 -> Analog 3:884
14:14:40.732 -> Analog 0:1022
14:14:40.732 -> Analog 1:970
14:14:40.732 -> Analog 2:920
14:14:40.732 -> Analog 3:888
14:14:40.732 -> Analog 0:1023
14:14:40.732 -> Analog 1:997
14:14:40.732 -> Analog 2:965
14:14:40.732 -> Analog 3:922
14:14:40.780 -> Analog 0:1023

   
#include <mcp_canbus.h>
#include <SPI.h>

const int SPI_CS_PIN = 17;              // CANBed V1
// const int SPI_CS_PIN = 3;            // CANBed M0
// const int SPI_CS_PIN = 9;            // CAN Bus Shield

int Analog0_Raw;
int Analog0_Volt;
int Analog0_VoltHigh;
int Analog0_VoltLow;

int Analog1_Raw;
int Analog1_Volt;
int Analog1_VoltHigh;
int Analog1_VoltLow;

int Analog2_Raw;
int Analog2_Volt;
int Analog2_VoltHigh;
int Analog2_VoltLow;


int Analog3_Raw;
int Analog3_Volt;
int Analog3_VoltHigh;
int Analog3_VoltLow;


MCP_CAN CAN(SPI_CS_PIN);                                    

void setup()
{
  pinMode(A0,INPUT);
  pinMode(A1,INPUT);
  pinMode(A2,INPUT);
  pinMode(A3,INPUT);
  
  CAN.begin(CAN_1000KBPS);
  Serial.begin(9600);
  }


void loop()
{
  

  Analog0_Raw = analogRead(A0);   
  Analog0_Volt = Analog0_Raw/2.046;
  Analog0_VoltHigh=(Analog0_Volt/256);
  Analog0_VoltLow=Analog0_Volt-(Analog0_VoltHigh*256);


  Analog1_Raw = analogRead(A1);   
  Analog1_Volt = Analog1_Raw/2.046;
  Analog1_VoltHigh=(Analog1_Volt/256);
  Analog1_VoltLow=Analog1_Volt-(Analog1_VoltHigh*256);

  Analog2_Raw = analogRead(A2);   
  Analog2_Volt = Analog2_Raw/2.046;
  Analog2_VoltHigh=(Analog2_Volt/256);
  Analog2_VoltLow=Analog2_Volt-(Analog2_VoltHigh*256);
  
  Analog3_Raw = analogRead(A3);   
  Analog3_Volt = Analog3_Raw/2.046;
  Analog3_VoltHigh=(Analog3_Volt/256);
  Analog3_VoltLow=Analog3_Volt-(Analog3_VoltHigh*256); 

      unsigned char stmp[8] = {Analog0_VoltHigh, Analog0_VoltLow, Analog1_VoltHigh, Analog1_VoltLow, Analog2_VoltHigh, Analog2_VoltLow, Analog3_VoltHigh, Analog3_VoltLow,};
  
    CAN.sendMsgBuf(0x391, CAN_STDID, 8, stmp);
   
    delay(10);   // send data per 10ms

    Serial.print("Analog 0:");
    Serial.println (Analog0_Raw);
     Serial.print("Analog 1:");
    Serial.println (Analog1_Raw);
     Serial.print("Analog 2:");
    Serial.println (Analog2_Raw);
     Serial.print("Analog 3:");
    Serial.println (Analog3_Raw);

If they're not connected then they are floating. You can't expect them to read 0 unless they are connected to ground. It pretty common for a floating analog input to read similar to the last read because the mux still has whatever residual voltage just sitting there. Connect the analog inputs to something if you want a sensible reading from them.

that worked. lol i knew it was something stupid i was doing, i grounded the 3 other inputs and they all read 0, if i move one to the pot it read the output voltage of the pot. Thank you for your help!

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