I am trying to write some code that will let me use the Trill Bar at the same time as a normal linear slider potentiometer and a button. The 3 inputs devices will form a BLE message, I am making a game pad like device.
I have successfully got the Trill Bar and Button to work, but when adding the slider things get a little messy and I am not sure what's happening. Is the Trill bar sensing loop blocking? Will it slow down the loop? Is there a better way to write what I am trying to write.. You can see the code here below:
#include <Arduino.h>
// BLE Gamepad esp32 library: https://github.com/lemmingDev/ESP32-BLE-Gamepad
#include <BleGamepad.h>
BleGamepad bleGamepad("Controller", "BLE", 100);
// Bela trill bar sensor: https://learn.bela.io/using-trill/trill-and-arduino/
#include <Trill.h>
Trill trillSensor;
boolean touchActive = false;
//Button
const int buttonPin1 = 12;
int buttonState1 = 0;
boolean buttonPressed1 = false;
//Analog slider
const int analogInPin = A1; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
//LED
const int ledPin = 13;
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
delay(100);
//Initialise ble
bleGamepad.begin();
//initialise trill bar
int ret = trillSensor.setup(Trill::TRILL_BAR);
if(ret != 0) {
Serial.println("failed to initialise trillSensor");
Serial.print("Error code: ");
Serial.println(ret);
}
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT);
}
void loop() {
if (bleGamepad.isConnected()) {
digitalWrite(ledPin, HIGH);
Serial.print("BLE Connected");
Serial.println("Please enter serial commands to choose mode:");
Serial.println("-------------------------------------------");
// delay(50);
//Start Button read
sensorValue = analogRead(analogInPin1);
mappedSlider = map(sensorValue, 0, 1023, -23767, 32767);
//Start slider read
buttonPressed1 = digitalRead(buttonPin1);
if (buttonPressed1 == HIGH) {
bleGamepad.press(BUTTON_4);
} else {
bleGamepad.release(BUTTON_4);
}
//Start trill read
trillSensor.read();
if(trillSensor.getNumTouches() > 0) {
// Serial.print("Trill: ");
// Serial.println(trillSensor.touchLocation(0));
int location = trillSensor.touchLocation(0);
Serial.println(location);
touchActive = true;
int locationMapped = map(location, 0, 3200, -32767, 32767);
// bleGamepad.setAxes(locationMapped, 0, 0, 0, 0, 0, 0, 0);
} else if(touchActive) {
touchActive = false;
locationMapped = 0;
}
//set 1st axis to Trill position and 2nd one to slider position. Leave all the other ones to zero.
bleGamepad.setAxes(locationMapped, mappedSlider, 0, 0, 0, 0, 0, 0);
// bleGamepad.setSlider1(32767);
// bleGamepad.setRightThumb(32767, 32767); // or bleGamepad.setZ(32767); and bleGamepad.setRZ(32767);
}
}