use of arduino with can bus sheild - arduino

I'm trying to get VGM CAN message out of a Reachstacker 42-45 tonnes
where I am using an arduino MEGA 2560 with a CAN-BUS shield
this my current code:
#include <SPI.h>
#include "mcp_can.h"
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
void loop()
{
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned char canId = CAN.getCanId();
Serial.println("-----------------------------");
Serial.println("get data from ID: ");
Serial.println(canId);
for(int i = 0; i<len; i++) // print the data
{
Serial.print(buf[i]);
Serial.print("\t");
}
Serial.println();
}
}
this was the result at the time of doing the test, the problem that I do not understand the result
according to the documentation should have a result like this :
this is another part of the documentation :
If someone needs more information or does not understand what I'm looking for, you can request what you need to help me
Send data:
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
void setup()
{
Serial.begin(115200);
START_INIT:
if(CAN_OK == CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
Serial.println("CAN BUS Shield init ok!");
}
else
{
Serial.println("CAN BUS Shield init fail");
Serial.println("Init CAN BUS Shield again");
delay(100);
goto START_INIT;
}
}
unsigned char stmp[8] = {0, 1, 2, 3, 4, 5, 6, 7};
void loop()
{
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
CAN.sendMsgBuf(0x00, 0, 8, stmp);
delay(100); // send data per 100ms
}

You have two pieces that do not fit between your documentation and the output you are generating:
The data payload
The ID of the CAN frames
For the data payload it is simply a matter of formatting. You print each byte as integer value, whereas in the documentation it is printed as hexadecimal values. Use
Serial.print(buf[i], HEX)
to get the payload printed as hexadecimal characters.
For the ID of the CAN frames, you see from the documentation that they do not fit inside an unsigned char, as already mentioned in the comment by #Guille. Actually those are 29-bit identifiers, which you should ideally store in an appropriately sized variable. Ideally use an unsigned long:
unsigned long canId = CAN.getCanId();
In the documentation the CAN ID is also printed in hexadecimal, so also here use:
Serial.println(canId, HEX);

Related

Trying to send a float value over SPI between 2 Arduinos

I am currently trying to send a float value across two Arduinos via SPI. Currently I am working to send a static value of 2.25 across and then read it via the Serial.println() command. I would then want to pass a float value from a linear displacement sensor. My end goal is to be able to have the master ask for information, the slave gathers the appropriate data and packages it and then master receives said data and does what it needs with it.
Currently I am getting an error "call of overloaded 'println(byte [7])' is ambiguous" and I am not to sure why I am getting this error. I am currently a mechanical engineering student and I am crash-coursing myself through C/C++. I am not entirely positive about what I am doing. I know that a float is 4 bytes and I am attempting to create a buffer of 7 bytes to store the float and the '\n' char with room to spare. My current code is below.
Master:
#include <SPI.h>
void setup() {
pinMode(SS,OUTPUT);
digitalWrite(SS,HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4);
}
void loop() {
digitalWrite(SS,LOW);
float a = 2.25;
SPI.transfer(a);
SPI.transfer('\n');
digitalWrite(SS,HIGH);
}
My slave code is as follows:
#include <SPI.h>
byte buf[7];
volatile byte pos = 0;
volatile boolean process_it = false;
void setup() {
Serial.begin(9600);
pinMode(MISO,OUTPUT);
digitalWrite(MISO,LOW);
SPCR |= _BV(SPE); // SPI Enable, sets this Arduino to Slave
SPCR |= _BV(SPIE); // SPI interrupt enabled
}
ISR(SPI_STC_vect) {
// Interrupt Service Routine(SPI_(SPI Transfer Complete)_vector)
byte c = SPDR;
// SPDR = SPI Data Register, so you are saving the byte of information in that register to byte c
if (pos < sizeof buf) {
buf[pos++] = c;
if (c == '\n') {
process_it = true;
}
}
}
void loop() {
if (process_it = true) {
Serial.println(buf);
pos = 0;
process_it = false;
}
}
I figured out what I needed to do and I wanted to post my finished code. I also added an ability to transfer more than one float value.
Master:
#include <SPI.h>
float a = 3.14;
float b = 2.25;
uint8_t storage [12];
float buff[2] = {a, b};
void setup()
{
digitalWrite(SS, HIGH);
SPI.begin();
Serial.begin(9600);
SPI.setClockDivider(SPI_CLOCK_DIV8);
}
void loop()
{
digitalWrite(SS, LOW);
memcpy(storage, &buff, 8);
Serial.print("storage[0] = "); Serial.println(storage[0]); // the
following serial prints were to check i was getting the right decimal
numbers for the floats.
Serial.print("storage[1] = "); Serial.println(storage[1]);
Serial.print("storage[2] = "); Serial.println(storage[2]);
Serial.print("storage[3] = "); Serial.println(storage[3]);
Serial.print("storage[4] = "); Serial.println(storage[4]);
Serial.print("storage[5] = "); Serial.println(storage[5]);
Serial.print("storage[6] = "); Serial.println(storage[6]);
Serial.print("storage[7] = "); Serial.println(storage[7]);
SPI.transfer(storage, sizeof storage ); //SPI library allows a user to
transfer a whole array of bytes and you need to include the size of the
array.
digitalWrite(SS, HIGH);
delay(1000);
}
For my Slave code:
#include <SPI.h>
byte storage [8];
volatile byte pos;
volatile boolean process;
float buff[2];
void setup()
{
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
SPCR |= _BV(SPIE);
pos = 0;
process = false;
Serial.begin(9600);
}
ISR(SPI_STC_vect)
{
byte gathered = SPDR;
if( pos < sizeof storage)
{
storage[pos++] = gathered;
}
else
process = true;
}
void loop()
{
if( process )
{
Serial.print("storage[0] = "); Serial.println(storage[0]);
Serial.print("storage[1] = "); Serial.println(storage[1]);
Serial.print("storage[2] = "); Serial.println(storage[2]);
Serial.print("storage[3] = "); Serial.println(storage[3]);
Serial.print("storage[4] = "); Serial.println(storage[4]);
Serial.print("storage[5] = "); Serial.println(storage[5]);
Serial.print("storage[6] = "); Serial.println(storage[6]);
Serial.print("storage[7] = "); Serial.println(storage[7]);
memcpy(buff,&storage,8);
Serial.print("This is buff[0]");Serial.println(buff[0]);
Serial.print("This is buff[1]");Serial.println(buff[1]);
storage[pos] = 0;
pos = 0;
process = false;
}
}
The immediate problem is that Serial.print doesn't know what to do with a byte array. Either declare it as a char array or cast it in the print statement:
char buf[7];
OR
Serial.print((char*) buf);
Either way, though, it's not going to show up as a float like you want.
An easier way to do all this is to use memcpy or a union to go back and forth between float and bytes. On the master end:
uint8_t buf[4];
memcpy(buf, &a, 4);
Then use SPI to send 4 bytes. Reverse it on the peripheral end.
Note that sending '\n' as the termination byte is a bad idea because it can lead to weird behavior, since one of the bytes in the float could easily be 0x0a, the hexadecimal equivalent of '\n'.

Will a CAN Bus network with 2 Arduino / MCP2515 nodes work?

I'd like to set up a CAN network of multiple nodes using Arduino Pro Minis and MCP2515 cards. But I can't get the Receive to work.
#include <mcp_can.h>
#include <SPI.h>
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128];
#define CAN0_INT 2 // Set INT to pin 2
MCP_CAN CAN0(10); // Set CS to pin 10
void setup() {
Serial.begin(115200);
// Initialize MCP2515 running at 8MHz with a baudrate of 125kb/s
// and the masks and filters disabled.
while (CAN_OK != CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_8MHZ)) {
Serial.println("CAN BUS Module Failed to Initialize.");
}
Serial.println("MCP2515 Initialized Successfully!");
CAN0.setMode(MCP_NORMAL);
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
}
void loop() {
if(!digitalRead(CAN0_INT)) { // If CAN0_INT is low, read receive buffer
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
if((rxId & 0x80000000) == 0x80000000) // Is ID standard (11 bits) or extended (29 bits)?
sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len);
else
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
if((rxId & 0x40000000) == 0x40000000) { // Is message a remote request frame?
sprintf(msgString, " REMOTE REQUEST FRAME");
Serial.print(msgString);
} else {
for(byte i = 0; i<len; i++) {
sprintf(msgString, " 0x%.2X", rxBuf[i]);
Serial.print(msgString);
}
}
Serial.println();
}
}
However, all I get out are the error messages, including this:
Entering Configuration Mode Failure
What am I missing here?
I got the circuit to work. The 2-node CAN Bus is communicating.
I found this site and made a couple of changes:
My Arduino ProMini MISI, MISO pins were not aligned with the SI, SO pins on the MCP2515s.
I used the CAN_BUS_Shield library.

Arduino to arduino i2c code

I have an OPT101 connected to a slave arduino to measure light intensity. I want to send the data received from the OPT101 circuit to a master arduino that will print the data on the serial monitor. When I test my code, nothing shows up on the screen. (I know it's not my i2c connection cause I tested it by sending "hello"). I am using an arduino leonardo as the slave and the arduino uno as the master.
The code for the OPT101 circuit is:
#define inPin0 0
void setup() {
Serial.begin(9600);
Serial.println();
}
void loop() {
int pinRead0 = analogRead(inPin0);
double pVolt0 = pinRead0 / 1024.00 * 5.0;
Serial.print(pVolt0, 4 );
Serial.println();
delay(100);
}
I tired to combine the slave code and my OPT101 code to get this:
#include
#define inPin0 0
void setup() {
Wire.begin(2);
}
void loop() {
Wire.beginTransmission(2);
Wire.onRequest(requestEvent);
Wire.endTransmission();
}
void requestEvent()
{
int pinRead0 = analogRead(inPin0);
int pVolt0 = pinRead0 / 1024.0 * 5.0;
Wire.write((byte)pVolt0);
}
And this is my master code:
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(14400);
Wire.requestFrom(2, 8);
while(Wire.available())
{
char c = Wire.read();
Serial.print(c);
}
}
void loop()
{
}
You must follow steps described below to communicate between master and slave I2C devices:
Only master can initiate read or write request.
Read or write requests must be synchronous. It means, slave can only return data after master requests for them and vice versa for write.
Do not use slave address from 0 - 7. They are reserved. Use slave address that ranges between 8 to 127.
On Arduino I2C, you can only send and receive a byte. To send or receive integer, double that have multiple bytes, you need to split them first and on other side, you have to combine them into its equivalent datatype. (Correct me, if I'm wrong.)
Your code should be like this:
Master Sketch:
#include <Wire.h>
#define SLAVE_ADDRESS 0x40
// This macro reads two byte from I2C slave and converts into equivalent int
#define I2C_ReadInteger(buf,dataInteger) \
buf[0] = Wire.read(); \
buf[1] = Wire.read(); \
dataInteger = *((int *)buf);
// Returns light intensity measured by 'SLAVE_ADDRESS' device
int GetLightIntensity()
{
byte Temp[2];
int Result;
// To get integer value from slave, two are required
int NumberOfBytes = 2;
// Request 'NumberOfBytes' from 'SLAVE_ADDRESS'
Wire.requestFrom(SLAVE_ADDRESS, NumberOfBytes);
// Call macro to read and convert bytes (Temp) to int (Result)
I2C_ReadInteger(Temp, Result);
return Result;
}
void setup()
{
// Initiate I2C Master
Wire.begin();
// Initiate Serial communication # 9600 baud or of your choice
Serial.begin(9600);
}
void loop()
{
// Print light intensity at defined interval
Serial.print("Light Intensity = ");
Serial.println(GetLightIntensity());
delay(1000);
}
Slave Sketch:
#include <Wire.h>
#define SLAVE_ADDRESS 0x40
#define inPin0 0
// Preapres 2-bytes equivalent to its int
#define IntegerToByte(buf,intData) \
*((int *)buf) = intData;
// Sends int to Master
void I2C_SendInteger(int Data)
{
byte Temp[2];
// I2C can only send a byte at a time.
// Int is of 2bytes and we need to split them into bytes
// in order to send it to Master.
// On Master side, it receives 2bytes and parses into
// equvivalent int.
IntegerToByte(Temp, Data);
// Write 2bytes to Master
Wire.write(Temp, 2);
}
void setup()
{
// Initiate I2C Slave # 'SLAVE_ADDRESS'
Wire.begin(SLAVE_ADDRESS);
// Register callback on request by Master
Wire.onRequest(requestEvent);
}
void loop()
{
}
//
void requestEvent()
{
// Read sensor
int pinRead0 = analogRead(inPin0);
int pVolt0 = pinRead0 / 1024.0 * 5.0;
// Send int to Master
I2C_SendInteger(pVolt0);
}
This code is tested on Arduino Version: 1.6.7.
For more information regarding I2C communication, refer Arduino
Example: Master Reader
Why are you putting the while loop in the setup() function instead of using the loop() function ?
But more confusing is this line int pVolt0 = pinRead0 / 1024.0 * 5.0;. In the initial code the variable is not int but double. I suggest you try to recode using the original line:
double pVolt0 = pinRead0 / 1024.00 * 5.0;
And only then reduce to int.
In Arduino I2C, you can only send and receive one byte, and it is necessary to combine them in their equivalent data type.

Arduino Wire program seems to stop reading bytes after first i2c payload

I am trying to write a program that receives string data from i2c and displays it on an LCD. The first time data is received to the arduino, it renders it, however subsequent i2c payloads are ignored. My onReceive function has a status line display on the second line of the lcd which display the seconds() field from the timer chip. The seconds number does not seem to increment. However, the per-second dot flash as rendered in loop() does continue to blink, so the mcu is not frozen.
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Time.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.
lcd.clear(); // start with a blank screen
}
void loop()
{
lcd.setCursor(15,1);
if (second() % 2 == 0)
lcd.write(".");
else
lcd.write(" ");
delay(100);
}
void receiveEvent(int howMany)
{
//char buf[howMany];
int i=0;
char output[16];
lcd.clear();
while(Wire.available())
{
char c = Wire.read(); // receive byte as a character
lcd.setCursor(i,0);
lcd.write(c);
i++;
//buf[i++]=c;
//buf[i+1]=0;
}
lcd.setCursor(0,1);
sprintf(output,"s%dNB%dI%d",second(),howMany,i);
lcd.write(output);
}
Your Arduino may be trapped in here:
while(Wire.available())
{
//...
Use:
if(Wire.available() > 0) {
//stuff
}
instead.

Sending Data from Xbee Arduino to PC

I have an application where I am using a MCP3421 18bit ADC to read analog data. The setup is Xbee+Xbee Sheild+Arduino + MCP3421 as Transmitter. This I am reading and transmitting to a remote xbee+arduino module with LCD. The data is displayed fine on the LCD. however I want to receive the data on the Serial port. When i try tp Do a Serial.println(s); on the receiving code the data which i get on serial port is garbled. Would appreciate any help
Here is my Code
Transmitting
#include <Wire.h>
#include <LiquidCrystal.h>
#define TRUE 1
#define FALSE 0
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup(void)
{
Serial.begin(9600);
Wire.begin();
delay(100);
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>"); // just to be sure things are working
lcd.begin(16, 2);
}
void loop(void)
{
byte address, Hi, Lo, Config;
int ADVal;
while(1)
{
address = 0x68;
Wire.beginTransmission(address);
Wire.write(0x88); // config register %1000 1000
// /RDY = 1, One Conversion, 15 samples per, PGA = X1
Wire.endTransmission();
delay(1);
Wire.requestFrom((int)address, (int) 3);
Hi = Wire.read();
Lo = Wire.read();
Config = Wire.read();
Wire.endTransmission();
ADVal = Hi;
ADVal = ADVal * 256 + Lo;
// Serial.print(ADVal, DEC);
//Serial.print(" ");
//Serial.println(Config, DEC);
Serial.print("<");
Serial.print(ADVal);
Serial.print(">");
//lcd.setCursor(0,0);
//lcd.print(ADVal);
lcd.setCursor(0,1);
//float val = ADVal * 0.00006244087;
//lcd.print(val,3);
delay(1);
}
}
and this is the receiving code
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
bool started = false;
bool ended= false;
char inData[10]; // Leave plenty of room
byte index;
float i;
//char inData[24]; // Or whatever size you need
//byte index = 0;
void setup(){
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// initialize the serial communications:
Serial.begin(9600);
}
void loop()
{
//Serial.println(s);
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '<')
{
// Start of packet marker read
index = 0;
inData[index] = '\0'; // Throw away any incomplete packet
started = true;
ended = false;
}
else if(aChar == '>')
{
// End of packet marker read
ended = true;
break; // Done reading serial data for now
}
else
{
if(index < 10) // Make sure there is room
{
inData[index] = aChar; // Add char to array
index++;
inData[index] = '\0'; // Add NULL to end
}
}
}
// When we get here, there is no more serial data to read,
// or we have read an end-of-packet marker
if(started && ended)
{
// We've seen both markers - do something with the data here
lcd.setCursor(0,0);
i = atoi (inData);
float s = (i * 0.3051851); //multiplying with calibration factor
float value = ( s / 1000 );
lcd.setCursor(1,0);
lcd.print(value,3); // print value after multiplying with calibration factor to LCD
lcd.setCursor(0,1 );
lcd.print(i); // Print raw ADC counts as recieved from transmitter
index = 0;
inData[index] = '\0';
started = false;
ended = false;
}
}
The receiving arduino do get the data through Xbee and it displays values perfectly on the LCD( Attached PIC). I also need to receive the data on a PC attached to the receiving arduino through its USB/Serial port.
When i try to use the serial monitor the display on LCD vanishes and the serial monitor displays garbled values. I thing the Serial.print(s) is sending back the data to the XBEE as both the DO and DI LED starts blinking on the XBEE SHIELD.

Resources