Hi All.
I'm trying to build a can-bus to bluetooth adapter. The goal is to read the can-bus on my car, and when specific messages are received, perform a corresponding action.
To simplify -
I press a button on my steering wheel (I'll use track forward for example)
The can-bus shield reads the can-bus message for that action ("1D6 2 E000")
Arduino then tells the bluetooth adapter to send a hex command via software serial:
if(CANbus == "1D6 2 E000") // (button up) sends (track?) forward command then release
{
- blueSmirf.write(0xFD);*
- blueSmirf.write(0x03);*
- blueSmirf.write(0x03);*
- blueSmirf.write(0x100);*
- blueSmirf.write(0x00);*
- blueSmirf.write(0xFD);*
- blueSmirf.write(0x03);*
- blueSmirf.write(0x03);*
- blueSmirf.write(0x00);*
- blueSmirf.write(0x00);*
- }*
My Android tablet then performs track forward.
I know all the write commands to perform the actions I need.
Where I'm stuck is reading the can-bus message, storing it as a value, and having the corresponding action performed if the value matches one of many "if" statements.
Here's my code so far:
#include <SPI.h>
#include <mcp_can.h>
#include <mcp_can_dfs.h>
#include <SoftwareSerial.h>
SoftwareSerial blueSmirf (10, 11);
void setup()
{
Serial.begin(115200);
blueSmirf.begin(115200);
CAN.begin(CAN_500KBPS); // init can bus : baudrate = 500k
attachInterrupt(0, MCP2515_ISR, FALLING); // start interrupt
}
void MCP2515_ISR()
{
Flag_Recv = 1;
}
void loop()
{
if(Flag_Recv) // check if get data
{
Flag_Recv = 0; // clear flag
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
}
int CANbus = CAN.readMsgBuf;
void Action()
{
if(CANbus == "1D6 2 E000") // (button up) sends (track?) forward command then release
{
blueSmirf.write(0xFD);
blueSmirf.write(0x03);
blueSmirf.write(0x03);
blueSmirf.write(0x100);
blueSmirf.write(0x00);
blueSmirf.write(0xFD);
blueSmirf.write(0x03);
blueSmirf.write(0x03);
blueSmirf.write(0x00);
blueSmirf.write(0x00);
}
if(CANbus == "1D6 2 D000") // (button down) sends (track?) back command then release
{
blueSmirf.write(0xFD);
blueSmirf.write(0x03);
blueSmirf.write(0x03);
blueSmirf.write(0x200);
blueSmirf.write(0x00);
blueSmirf.write(0xFD);
blueSmirf.write(0x03);
blueSmirf.write(0x03);
blueSmirf.write(0x00);
blueSmirf.write(0x00);
}
if(CANbus == "1D6 2 C100") // (phone button) momentarily send play/pause command then release
{
blueSmirf.write(0xFD);
blueSmirf.write(0x03);
blueSmirf.write(0x03);
blueSmirf.write(0x80);
blueSmirf.write(0x00);
blueSmirf.write(0xFD);
blueSmirf.write(0x03);
blueSmirf.write(0x03);
blueSmirf.write(0x00);
blueSmirf.write(0x00);
}
}
I don't believe int CANbus = CAN.readMsgBuf; is the write way to do this, and it pukes on compile:
can_to_bt_test_1.ino: In function 'void setup()':
can_to_bt_test_1.ino:14:1: error: 'CAN' was not declared in this scope
can_to_bt_test_1.ino: In function 'void MCP2515_ISR()':
can_to_bt_test_1.ino:20:5: error: 'Flag_Recv' was not declared in this scope
can_to_bt_test_1.ino: In function 'void loop()':
can_to_bt_test_1.ino:25:8: error: 'Flag_Recv' was not declared in this scope
can_to_bt_test_1.ino:28:7: error: 'CAN' was not declared in this scope
can_to_bt_test_1.ino:28:23: error: 'len' was not declared in this scope
can_to_bt_test_1.ino:28:28: error: 'buf' was not declared in this scope
can_to_bt_test_1.ino:31:14: error: 'CAN' was not declared in this scope
can_to_bt_test_1.ino:34:1: error: a function-definition is not allowed here before '{' token
can_to_bt_test_1.ino:83:1: error: expected '}' at end of input
'CAN' was not declared in this scope
Any ideas on how to fix it?