Hi everybody,
I am trying to calculate the rotational speed of an encoder and than send the results on a CAN bus.
Hardware :
Arduino UNO
CAN-BUS shield V1.2 seeedstudio
USB to CAN dongle PEAK
Software:
Arduino IDE
PCAN-View
The baud rate is set at 500 kbit/s on both side.
I have the 120 ohms resistor
I have no problem calculating the speed of the encoder and displaying it with serial plotter. My problem is that I am not able to send the value of a variable through CAN. I am able to send a message and to read it in PCAN-View, but if I put a variable in my message I always got a value of 0 in PCAN-View.
with :
int test = 77;
unsigned char CAN_MSG[8] = {test, 0, 0, 0, 0, 0, 0, 0};
// send data: id = 0x00, standard frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x60, 0, 8, CAN_MSG);
I got : 4D 00 00 00 00 00 00 00
But if I tried to change the value of "test" in a script I always got : 00 00 00 00 00 00 00 00
CAN_MSG is an array of 8 chars.
a char is 8 bits
test is an integer which are 16 bits.
Thank you for the hint, I will investigate that this morning
I tried to play with the type of variables ( int, char, byte, etc..), but I am still stuck with the same problem.
The message send on the CAN bus is always the same one. It s always the first message on repeat.
After that, even if the value of the variable are changing I always got the first message.
I did a new test and apparently my program can change the value of the variable three times.
Example :
unsigned char stmp[8]= {0,0,0,0,0,0,0,0};
char test = 2;
void setup() {
Serial.begin(250000); // Serial com for data output
initEncoders();
clearEncoderCount();
//CAN bus initialisation
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init fail");
Serial.println(" Init CAN BUS Shield again");
delay(100);
}
}
void loop() {
test = test+1;
stmp[3] = test ;
CAN.sendMsgBuf(0x07B, 0, 8, stmp);
Serial.println(stmp[3]);
delay(10);
}
In PCAN-View I receive the message :
00 00 00 03 00 00 00 00
00 00 00 04 00 00 00 00
and then :
00 00 00 05 00 00 00 00
until I restart the Arduino or resend the program. But if I use the serial plotter I can see the value of stmp[3] going from 0 to 255 and starting over.