Transmitting button states between two HM-10 modules

Hello, I'm working on a project that consists on creating a remote for a jukebox-like/musical station appliance. The station is communicating with a HM-10 module and a Arduino Leonardo, and the remote I'm planning to make will be communicating with a Arduino Nano and another HM-10 module. I have some questions about programming :

  • The two HM-10 modules are connected together, I put the remote on Master mode and the station on Slave mode, since I will be transmitting data from the remote to the station. Is that correct ?

  • Here's my code so far for the remote, I would appreciate that you would give me any advice on it :

// DĂ©claration fonction Bluetooth
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // (RX, TX)

// Déclaration des boutons utilisés pour la télécommande
const int btn1 = A0;
const int btn2 = A1;
const int btn3 = A2;
const int btn4 = A3;
const int btn5 = A4;
const int btn6 = A5;
 
void setup() {
  // Affectation des boutons en entrée
  pinMode(btn1, INPUT);
  pinMode(btn2, INPUT);
  pinMode(btn3, INPUT);
  pinMode(btn4, INPUT);
  pinMode(btn5, INPUT);
  pinMode(btn6, INPUT);
  // Lancement moniteur série pour visualisation sur logiciel
  Serial.begin(9600);
  // Vitesse du HM-10 par défaut
  BTSerial.begin(9600);
}
 
void loop() {
  // Fonction de lecture des différents boutons
  bst1 = digitalRead(btn1);
  bst2 = digitalRead(btn2);
  bst3 = digitalRead(btn3);
  bst4 = digitalRead(btn4);
  bst5 = digitalRead(btn5);
  bst6 = digitalRead(btn6);

  if (buttonLastState != bst1){
    if (bst1 == 1){
      Bluetooth.write('1');
      buttonLastState = bst1;
    }
  }
}

This code isn't finished, but I plan on writing the same "if" fonction for each button. I read about the pkt. function, but I don't understand how it works and if I can use it with my buttons. I also need to affect a decimal or a hexadecimal value to each button, how can I make it work ?

// DĂ©claration fonction Bluetooth
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2, 3); // (RX, TX)

// Déclaration des boutons utilisés pour la télécommande
const byte btn[] = {A0, A1, A2, A3, A4, A5}; // we assume external pull- up/down resistor

void setup() {
  // Lancement moniteur série pour visualisation sur logiciel
  Serial.begin(9600);
  // Vitesse du HM-10 par défaut
  BTSerial.begin(9600);
}

void loop() {
  static byte OldState = 0;
  byte NowState = 0;

  for (byte i = 0; i < 6; i++)NowState |= digitalRead(btn[i]) << i;

  if (OldState != NowState) {  // Fonction de lecture des différents boutons
    Bluetooth.write(NowState);
    OldState = NowState;
  }
}
1 Like

Thanks for your answer ! So the "for" function manages to read the value of each button, and then, the "if" will send the value through Bluetooth, allowing any receiver to read it ? Am I understanding it correctly ?

idk. are BT devices not need to be paired first?

no. FOR is loop function. IF is condition checker.

Yes, the devices needs to be paired, the two I have are already paired.

Ok so, "IF" will just read and save the data from pushing any button, after that I have to write a function to send the data to the other device, am I right ?

It seems that you didn't notice the main thing in @kolaha code - it reads all the buttons in one loop and packs the result into one byte.
After this, the IF compares the resulting value with the previous one.
His code works for all 6 buttons at once, you don't need to write an IF statement for each button separately.

1 Like

I admit that I didn't fully understood it haha. That one byte is the data I need to send to the other device then, it seems clearer for me now.

Hello sordidduck

Consider:

//https://forum.arduino.cc/t/transmitting-button-states-between-two-hm-10-modules/1200262
//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "Transmitting button states between two HM-10 modules"
#define NotesOnRelease "Arduino MEGA tested"
// -- some useful text replacements used because I'm lazy with typing --
#define equ ==
#define is ==
#define nequ !=
#define view(x) Serial.print(x)
#define viewLn(x) Serial.println(x)
// make names
enum TimerEvent {NotExpired, Expired};
enum Numbers {One, Two, Three, Four, Five, Six};
// make variables
constexpr uint8_t ButtonPins[] {A0, A1, A2, A3, A4, A5};  // [pin] --- [button] --- [gnd]
// make structures
struct DTIMER
{
  uint32_t interval;
  uint32_t now;
  uint8_t expired(uint32_t currentMillis)
  {
    uint8_t timerEvent = currentMillis - now >= interval;
    if (timerEvent is Expired) now = currentMillis;
    return timerEvent;
  }
};
struct BUTTON2HM10
{
  const char toHM10;
  const uint8_t ButtonPin;
  uint8_t stateOld;
  DTIMER debounce;
  void make()
  {
    pinMode(ButtonPin, INPUT_PULLUP);
  }
  void run(uint32_t currentMillis)
  {
    if (debounce.expired(currentMillis) equ Expired)
    {
      uint8_t stateNew = digitalRead(ButtonPin) ? LOW : HIGH;
      if (stateOld nequ stateNew)
      {
        stateOld = stateNew;
        if (stateNew equ HIGH) view(toHM10);
      }
    }
  }
} button2HM10s[]
{
  {'1', ButtonPins[One], LOW, 20, 0},
  {'2', ButtonPins[Two], LOW, 20, 0},
  {'3', ButtonPins[Three], LOW, 20, 0},
  {'4', ButtonPins[Four], LOW, 20, 0},
  {'5', ButtonPins[Five], LOW, 20, 0},
  {'6', ButtonPins[Six], LOW, 20, 0},
};

// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application
void setup()
{
  Serial.begin(115200);
  Serial.print("Source: "), Serial.println(__FILE__);
  Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
  for (auto button2HM10 : button2HM10s) button2HM10.make();
  Serial.println(" =-> and off we go\n");
}
void loop()
{
  uint32_t currentMillis = millis();
  heartBeat(LED_BUILTIN, currentMillis);
  for (auto &button2HM10 : button2HM10s) button2HM10.run(currentMillis);
}

Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.