Can shield to bluetooth HID assitance

Hi all,

I’m working on a new project, but I’m kind of baffled where to start.

What I’m trying to do is read certain can-bus messages, and based on the message content, transmit a particular HID hex code to my Android phone.

The purpose is for steering wheel integration.

I could purchase a can-bus adapter that converts can to ladder resistances (I already have the code written for that), but I purchased a can-bus shield instead to maybe learn a little more and allow a little more flexibility.

Here’s two examples of what I’m trying to do:

Action - Volume up button depressed

Can-bus message to listen for - 1D6 2 C800

Bluetooth message to transmit:

Serial.write(0xFD);
Serial.write(0x03);
Serial.write(0x03);
Serial.write(0x10);
Serial.write(0x00);
Serial.write(0xFD);
Serial.write(0x03);
Serial.write(0x03);
Serial.write(0x00);
Serial.write(0x00);

Action - Volume down button depressed

Can-bus message to listen for - 1D6 2 C400

Bluetooth message to transmit:

Serial.write(0xFD);
Serial.write(0x03);
Serial.write(0x03);
Serial.write(0x20);
Serial.write(0x00);
Serial.write(0xFD);
Serial.write(0x03);
Serial.write(0x03);
Serial.write(0x00);
Serial.write(0x00);

I have 6 different steering wheel buttons total.

Would someone be able to parse me in the write direction?

Here’s my ladder resistor code I was originally working on. If someone would be able to manipulate it with the can-bus code, that would be awesome. I just need a start - once I have two working buttons I can add in the additional buttons. I’m good at manipulating existing code, but horrible at writing it from scratch… :expressionless:

This was someone else’s code manipulated to write the hid hex command to the serial port (bluetooth HID adapter):

/*
AnalogButton_Combos
Version 0.1

Connection more then one button to a single analog pin. Utilizing
software debounce to prevent registering multiple button press
while allow for 2 button combos to be registered.  

The Circuit:
  Most other analog buttons circuits call for the resistors to be
  lined up in series from ground. The analog pin and each button
  connect off one of the resistors. My cuicuit requires that the
  resistors tie in from +5 to the buttons. The buttons all connect
  to the analog pin which is tied to ground threw a 1k resistor as
  seen in the diagram below. 
 
         Analog pin 5
            |
Ground--1K--|--------|--------|-------|-------|
            |        |        |       |       |
           btn1     btn2     btn3    btn4    btn5
            |        |        |       |       |
         220 Ohm  390 Ohm  680 Ohm   2.2K    4.7K
            |--------|--------|-------|-------|-- +5V
            
Created By: Michael Pilcher
February 24, 2010

*/
int j = 1; // integer used in scanning the array designating column number
//2-dimensional array for asigning the buttons and there high and low values
int Button[21][3] = {{1, 837, 838}, // vol up
                     {2, 737, 738}, // vol down
                     {3, 610, 611}, // track fwd
                     {4, 318, 319}, // track back
                     {5, 178, 179}, // phone play / pause

int analogpin = 5; // analog pin to read the buttons
int label = 0;  // for reporting the button label
int counter = 0; // how many times we have seen new value
long time = 0;  // the last time the output pin was sampled
int debounce_count = 50; // number of millis/samples to consider before declaring a debounced input
int current_state = 0;  // the debounced input value
int ButtonVal;
int radarpin = 2;
int dtcpin = 3;

void setup()
{
  Serial.begin(115200); 

}

void loop()
{
   // If we have gone on to the next millisecond
  if (millis() != time)
  {
    // check analog pin for the button value and save it to ButtonVal
    ButtonVal = analogRead(analogpin);
    if(ButtonVal == current_state && counter >0)
    { 
      counter--;
    }
    if(ButtonVal != current_state)
    {
      counter++;
    }
    // If ButtonVal has shown the same value for long enough let's switch it
    if (counter >= debounce_count)
    {
      counter = 0;
      current_state = ButtonVal;
      //Checks which button or button combo has been pressed
      if (ButtonVal > 0)
      {
        ButtonCheck();
      }
    }
    time = millis();
  }
}

void ButtonCheck()
{
  // loop for scanning the button array.
  for(int i = 0; i <= 21; i++)
  {
    // checks the ButtonVal against the high and low vales in the array
    if(ButtonVal >= Button[i][j] && ButtonVal <= Button[i][j+1])
    {
      // stores the button number to a variable
      label = Button[i][0];
      Action();      
    }
  }
}

void Action()
{
  if(label == 1) // sends vol up command then release
  {
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x10);
    Serial.write(0x00);
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x00);
    Serial.write(0x00);
  }
  
  if(label == 2) // sends vol down command then release
  {
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x20);
    Serial.write(0x00);
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x00);
    Serial.write(0x00);
  }
  
  if(label == 3) // sends track forward command then release
  { 
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x100);
    Serial.write(0x00);
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x00);
    Serial.write(0x00);
  }
  
  if(label == 4) // sends track back command then release
  {
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x200);
    Serial.write(0x00);
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x00);
    Serial.write(0x00);
  }
  
  if(label == 5) // sends play/pause command then release
  {
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x80);
    Serial.write(0x00);
    Serial.write(0xFD);
    Serial.write(0x03);
    Serial.write(0x03);
    Serial.write(0x00);
    Serial.write(0x00);
       
 
}

So essentially, I know who to transmit my desired action, I just don’t know how to set up the can-bus shield to listen for it (and tell the sketch to transmit the corresponding action).

Any ideas?

Hi,

I have a similar project, have you made any progress on yours?