13.- Multitouch. Press several buttons at the same time.
p110i_esp32_ble_Multitouch.aia (204.6 KB)
In this topic we can see an example of Multitouch with/without Clock using classic Bluetooth:
- Let's see an example with BLE.
// Juan Antonio Villalpando.
// http://kio4.com/arduino/160_Wemos_ESP32_BLE.htm
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
String valor;
byte byte_received;
byte bytes;
#define LED12 12 // LED pin 12
#define LED14 14 // LED pin 14
#define LED27 27 // LED pin 27
#define LED16 16 // LED pin 16
String estado ="";
void motores();
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
valor = "";
for (int i = 0; i < value.length(); i++){
valor = valor + value[i];
}
byte_received = valor.charAt(0);
Serial.print(byte_received);
Serial.print(" ");
/** // print byte_received in byte, ejemplo: 00000110
// Convert valor to bytes.
for(int i=0; i<valor.length(); i++){
char myChar = valor.charAt(i);
for(int i=7; i>=0; i--){
bytes = bitRead(myChar,i);
Serial.print(bytes, BIN); // Binario
}
Serial.println("");
}
*/
motores();
pCharacteristic->setValue(estado.c_str()); // Return status
}
}
};
void setup() {
pinMode(LED12, OUTPUT);
pinMode(LED14, OUTPUT);
pinMode(LED27, OUTPUT);
pinMode(LED16, OUTPUT);
Serial.begin(115200);
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Iniciado.");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
//
}
void motores(){
if (bitRead(byte_received, 0)){digitalWrite(LED12,HIGH); Serial.print("Motor0: RUN ");}
else {digitalWrite(LED12,LOW); Serial.print("Motor0: --- ");}
if (bitRead(byte_received, 1)){digitalWrite(LED14,HIGH); Serial.print("Motor1: RUN ");}
else {digitalWrite(LED14,LOW); Serial.print("Motor1: --- ");}
if (bitRead(byte_received, 2)){digitalWrite(LED27,HIGH); Serial.print("Motor2: RUN ");}
else {digitalWrite(LED27,LOW); Serial.print("Motor2: --- ");}
if (bitRead(byte_received, 3)){digitalWrite(LED16,HIGH); Serial.print("Motor3: RUN ");}
else {digitalWrite(LED16,LOW); Serial.print("Motor3: --- ");}
Serial.println();
estado ="";
if (digitalRead(LED12) == HIGH) {estado = "Motor0: RUN,";} else {estado = "Motor0: ---,";}
if (digitalRead(LED14) == HIGH) {estado = estado + "Motor1: RUN,";} else {estado = estado + "Motor1: ---,";}
if (digitalRead(LED27) == HIGH) {estado = estado + "Motor2: RUN,";} else {estado = estado + "Motor2: ---";}
if (digitalRead(LED16) == HIGH) {estado = estado + "Motor3: RUN";} else {estado = estado + "Motor3: ---";}
}