Programming Arduino, Bluetooth, and push buttons

Hi Everyone,

I’m having a hard time understanding a Bluetooth code. I have some part of the code thanks to this site

These are the components Arduino Nano, HC-05 flashed with RN42, and 2 push buttons. What I would like is you push a button and it sends a keyboard key through Bluetooth to an ipad or tablet. I was trying to understand this site http://www.instructables.com/id/Bluetooth-Keyboard-Mouse-Adapter/ Bluetooth code but got lost in the code lol. Later on I might want to add a 3rd switch for pause and play. I wanted to get the bluefruit EZ-Key it would have been prefect but they have been out of stock for some time now

This project is for my disable son so he can use a switch device instead of touch screen, theres so many apps on ipad and android that he could use. I am happy that the ipad and android has a setting for switchs, its just Bluetooth switches and adapters are $150-$300

sketch_jordin.ino (863 Bytes)

What I would like is you push a button and it sends a keyboard key through Bluetooth to an ipad or tablet.

What do you mean by "a keyboard key"? You can send data. If you can make the data look like "a keyboard key", you are free to do so, and then send that data.

The Keyboard.press() function will not send data to the bluetooth device, unless the bluetooth device is connected to the hardware serial pins.

Yes i want to send an (a) 1st push button and an (b) 2nd push button, in my code i already have the dec in there. On the ardunio and hc 05 is connected rx=tx tx=rx

From your sketch i dont see any data writing to BTSerial. You have to write appropriate data to BTSerial to send it to the connected device. Have a look at one of my post

It just read data from Bluetooth and sends back a string as response.

Hi Sony

I see im missing this in my sketch is that what ur talking about

void loop() { if(soft.available()){ String serialData = soft.readString(); Serial.println(serialData); //echo bluetooth data to serial writeAck(); } }

void writeAck(){ uint8_t payload[]="ACK|0|\n"; soft.write(payload,sizeof(payload));

Would this code work with hc-05 flashed with rn42?
#include <RN42_HIDRaw_Arduino.h>

RN42_HIDRaw_Arduino Raw = RN42_HIDRaw_Arduino();

//Shared DEFINES
#define DEBUG 0
#define THRESHOLD 100
#define NUM_KP 6
#define NUM_BUTTONS 12

#include “iCade_mappings.h”
#include “regular_hid_mappings.h”

//variable to hold which protocol to run
//pin 13 (LT_SHOULDER)is sampled during setup
//to determine which protocol to run
byte iCade_or_Regular = 0; //0=iCade 1=Regular

// This array maps a button index to an arduino pin
byte map_pins[NUM_BUTTONS] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

// debounce counter
char debounce[NUM_BUTTONS];

//------------------------------------------------------------------------------

void setup()
{
//initialize serial port
Serial.begin(115200);

// initializes the pins used for the buttons
for (byte i = 0; i < NUM_BUTTONS; i++)
{
byte pin = map_pins*;*

  • pinMode(pin, INPUT); // set pin to input*
  • digitalWrite(pin, HIGH); // turn on pullup resistor*
    _ debounce = 0; // initialize the debounce array_
    * }*

* //let everything settle*
* delay(500);*

* //read pin 13 (LT_SHOULDER) if it is pressed use regular HID protocol*
* if(digitalRead(13) == LOW)*
* iCade_or_Regular = 1;
_
}_
_
//Regular HID variables*_
char kp[NUM_KP]; //tracks the current keypresses
char kp_prior[NUM_KP]; //holds the prior keypresses
byte kp_ix = 0; //index to indicate how many keys are curently pressed
byte kp_ix_prior; //holds the prior index to help determine if things have changed
void read_btns_regular()
{
* memcpy(kp_prior, kp, NUM_KP); // copy the current keypresses to a holder*
* kp_ix_prior = kp_ix; // copy the keypress index to holder*
* kp_ix = 0; // reset the current number of keys pressed to 0*

* // iterate through the buttons until you have six to send*
* for (byte i = 0; i < NUM_BUTTONS; i++)
_
{_
//byte pin = map_pins;
if (digitalRead(map_pins) == LOW) // normally pulled-up, buttons connect to ground*

* {*
_ if (debounce < THRESHOLD)
* {
debounce += 1;
}
else*

* {_
if (kp_ix < NUM_KP)// if the index has not reached 6 keypresses log that key*

* {*
kp[kp_ix] = map_char*; // put the ASCII value into the keypress variable*
* kp_ix += 1; // increase the number of keys currently pressed*
* }*
* else*
* {*
* // if more than 6 keys are pressed break as this is the max*
* // that a raw record for HID BT can handle*
* break;*
* }*
* }*
* }*
* else*
* {*
* // if the key is not pressed reset its debounce variable*
_ debounce = 0;
* }
}*_

* // make and transmit an HID raw record, but only if something has changed*
* if (kp_ix != kp_ix_prior) //if the indexes are not the same there is more keys to send*
* {*
* send_reg_raw();
_ }
else*

* {
// if the indexes are the same iterate and compare the old and new keypress*

* // variables because one key may have been changed for another even if the*
* // index did not change*
* for (byte i = 0; i < kp_ix; i++)*
* {_
if (kp != kp_prior)
_ {_
send_reg_raw();
_ break;
}
}
} *_

}
void send_reg_raw(){
* char data[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};*
* for (byte i = 0; i < kp_ix; i++)
_ {_
data _= kp;
}
Raw.SendKeyboardRaw(data, (byte)0x00);
}
//iCade variables*

char kp1[NUM_KP];
char kp2[NUM_KP];
byte kp1_changed = 0;
byte kp2_changed = 0;
byte kp_ix1 = 0;
byte kp_ix2
//iterators
#define FIRST_BTNS 6 //end of the first set
#define SECOND_BTNS 12 //end of the second set
void read_btns_icade()
{
* //reset the indexes*
* kp_ix1 = 0;
kp_ix2 = 0;*

* //iterate over the first 6 buttons*
* for (byte i = 0; i < FIRST_BTNS; i++)*
* {_
if (digitalRead(map_pins) == LOW) // normally pulled-up, buttons connect to ground*

* {*
_ if (debounce < THRESHOLD) // if the button is pressed but it hasn’t reached the debounce move on
* {
debounce += 1;
continue;
}*_

* // we are using a multidementional array to hold the current and previous state of the button*
* // index 0 of the array indicates the current state of the button and index 1 is the previous*
* // state of the button (1 indicates the button is pressed 0 indicates it is not pressed)*
btn_state_icade*[0] = 1; // set the current state of the button to pressed*
if(btn_state_icade[0] != btn_state_icade*[1]) // is it the same as the previous state*
* {*
* kp1_changed = 1; //indicate that the button states have changed for later to know whether or not to send a raw report*
kp1[kp_ix1] = map_char_icade*[0]; // set the index of this key to the down key*
btn_state_icade[1] = btn_state_icade*[0]; // set the previous state to the current state for next time around*
* kp_ix1 += 1; // increase the index for iteration in the send loop*
* } *
* }*
* else*
* {*
* //if we are here the button was either released or was not pressed*
btn_state_icade*[0] = 0; // set the current state of th button to not pressed*
if(btn_state_icade[0] != btn_state_icade*[1]) // compare it with the previous state*
* {*
* kp1_changed = 1; //indicate that the buton state has changed*
kp1[kp_ix1] = map_char_icade*[1];//set it to the up key*
btn_state_icade[1] = btn_state_icade*[0]; // copy the new state to the old for next time around*
* kp_ix1 += 1; // increase the index for iteration in the send loop*
* }*
_ debounce = 0; // reset the debounce counter as the button was probably released
* }
}*_

* //same routine as the above except for it is iterating over the second 6 buttons*
* for (byte i = 6; i < SECOND_BTNS; i++)
_ {_
if (digitalRead(map_pins) == LOW) // normally pulled-up, buttons connect to ground*

* {*
_ if (debounce < THRESHOLD)
* {
debounce += 1;
continue;
}*_

btn_state_icade*[0] = 1;
if(btn_state_icade[0] != btn_state_icade[1])
_ {_
kp2_changed = 1;
kp2[kp_ix2] = map_char_icade[0];//set it to the down key*

btn_state_icade[1] = btn_state_icade*[0];
kp_ix2 += 1;
_ }
}
else*

* {_
btn_state_icade[0] = 0;
if(btn_state_icade[0] != btn_state_icade[1])
_ {_
kp2_changed = 1;
kp2[kp_ix2] = map_char_icade[1];//set it to the up key*

btn_state_icade[1] = btn_state_icade*[0];
kp_ix2 += 1;
_ }
debounce = 0;
}
}*_

* // make and transmit an HID raw record, but only if something has changed*
* if(kp1_changed == 1) // has the first set of buttons changed*
* {*
* Raw.SendKeyboardRaw(kp1, (byte)0x00);*
* Raw.SendKeyboardRawBlank();*
* kp1_changed = 0;
_ }*_

* if(kp2_changed == 1) // has the second set of buttons changed*
* {*
* Raw.SendKeyboardRaw(kp2, (byte)0x00);*
* Raw.SendKeyboardRawBlank();*
* kp2_changed*

Would this code work with hc-05 flashed with rn42?

Not a hope in hell. When are you going to post code properly?

I didn't do that code lol i got it from github. Im new to the programming language. I saw this code was used for an RN-42 and it has basically what im looking for.

I have same problem I have build 12 buttons keypad with Arduino and it working with USB very well now I need to use bluetooth module instead of USB, but I can’t find some code which will enable me to send press from Arduino to my PC.

here is my code which I need change for my bluetooth module CH-6 thank you for help.

#include <Keyboard.h>
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the kpd
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the kpd

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long loopCount;
unsigned long startTime;

void setup() {
Serial.begin(9600);
loopCount = 0;
startTime = millis();

}

void loop() {

loopCount++;
if ( (millis()-startTime)>5000 ) {
startTime = millis();
loopCount = 0;
}

// Fills kpd.key array with up-to 10 active keys.
// Returns true if there are ANY active keys.
if (kpd.getKeys())
{
for (int i=0; i<LIST_MAX; i++) // Scan the whole key list.
{
if ( kpd.key*.stateChanged ) // Only find keys that have changed state.*

  • {*
    _ switch (kpd.key*.kstate) { // Report active key state : IDLE, PRESSED, HOLD, or RELEASED*_
    * case PRESSED:*
    _ Keyboard.write(kpd.key*.kchar); *_

_ //Serial.print(kpd.key*.kchar); //active keys number*
* break;
}
}
}
}
} // End loop*_