Hi Guys,
I'm starting out learning arduino and GM Can bus sniffing.
My project what i'm trying to achieve is to read the steering wheel next /previous track buttons
translate the result and send via bluetooth the next track.
I'm very close to achieving this but my coding is letting me down somewhat.
i can filter and read the Canbus results and i can send the next track information to my phone ok however when i code it is the downfall for me.
when i push any of the steering wheel controls with the current code below it will just skip to the next song even if it's volume up or down ( not what im looking for )
obviously this is my code but i'm lacking the knowledge to rectify this if someone can assist?
there are 4 bits from this header 0x100d0060 and it's the 3rd bit that the information changes on
#include <mcp_can.h>
#include <SPI.h>
#include <SoftwareSerial.h>
SoftwareSerial hc06(8, 9);
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
char msgString[128]; // Array to store serial string
#define CAN0_INT 7 // Set Interrupt to pin 2
MCP_CAN CAN0(17); // Set CS to pin 10
void setup()
{
Serial.begin(115200);
while (!Serial)
{
// do nothing
} ;
Serial.println("I am in setup Loop, Initialising now");
if(CAN0.begin(MCP_ANY, CAN_33K3BPS, MCP_16MHZ) == CAN_OK)
Serial.println("MCP2515 Initialized Successfully!");
else
Serial.println("Error Initializing MCP2515...");
CAN0.setMode(MCP_NORMAL); // Set operation mode to normal so the MCP2515 sends acks to received data.
pinMode(CAN0_INT, INPUT); // Configuring pin for /INT input
//Initialize Serial Monitor
Serial.begin(115200);
Serial.println("Bluetooth Initialised:");
//Initialize Bluetooth Serial Port
hc06.begin(115200);
}
void loop()
{
if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer
{
CAN0.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
// 0x100d0060 = Wheel Controls
if ((rxId & 0x1FFFFFFF) == 0x100d0060)
{
sprintf(msgString, "0x%.8lX,%1d,", (rxId & 0x1FFFFFFF), len); //formats text so it prints nice in serial monitor
Serial.print(msgString);
if((rxId & 0x40000000) != 0x40000000) //if rxif contains steering wheel controls ID of 0x100d0060 with 4 bytes of data do below
{
for(byte i = 0; i < len; i++)
{
sprintf(msgString, " 0x%.2X", rxBuf[i]); //0x%.2X helps with hex bit data conversion
Serial.print(msgString);
Serial.print("\n");
Serial.print("Printing RXBuffer 2\n");
Serial.print(rxBuf[2]);
}
if ((rxBuf[2] & 0x01) == 0x01);
{
hc06.write("AT+FORWARD\r\n");
Serial.print("Track Changed Forward\n");
}
} else if ((rxBuf[2] & 0x1F) == 0x1F);
{
hc06.write("AT+BACKWARD\r\n");
Serial.print("Track Changed Backward\n");
}
}
}
}