#include <OneButton.h>
#include <SoftwareSerial.h>
#define rx 5
#define tx 4
#define bt1 6
#define bt2 7
#define bt3 8
#define bt4 9
#define bt5 10
const int baud = 9600;
const int numButtons = 5;
const int buttonPins[numButtons] = {bt1, bt2, bt3, bt4, bt5};
SoftwareSerial lora(rx, tx);
OneButton buttons[numButtons];
void setup() {
Serial.begin(baud);
lora.begin(baud);
for (int i = 0; i < numButtons; i++) {
buttons[i] = OneButton(buttonPins[i], true);
buttons[i].setDebounceMs(50);
buttons[i].setClickMs(300);
buttons[i].setPressMs(5000);
buttons[i].attachClick(buttonClick, i);
buttons[i].attachLongPressStop(buttonLongPress, i);
buttons[i].attachDoubleClick(buttonDoubleClick, i);
}
}
void loop() {
for (int i = 0; i < numButtons; i++) {
buttons[i].tick();
}
}
void buttonClick(int buttonIndex) {
Serial.print("Button ");
Serial.print(buttonIndex + 1);
Serial.println(" clicked (short press)");
lora.println((char)(buttonIndex + 48));
}
void buttonLongPress(int buttonIndex) {
Serial.print("Button ");
Serial.print(buttonIndex + 1);
Serial.println(" long press");
lora.println((char)(buttonIndex + 5 + 48));
}
void buttonDoubleClick(int buttonIndex) {
if (buttonIndex == 0) {
Serial.print("Button ");
Serial.print(buttonIndex + 1);
Serial.println(" double click");
lora.println((char)(buttonIndex + 10 + 48));
}
}