CTRL library / Object groups
Grouped potentiometers
This sketch demonstrates the grouping of potentiometers, with a shared event handler.
Groups offer another way to handle a large number of potentiometers. They let you use one shared event handler. If we then provide the potentiometers with some data, in the form of setters: setInteger(), setString() & setBoolean(), we can retrieve this data in the event handler and take action based on this data. Getting the data is done with the following getters: getInteger(), getString() & getBoolean().
The code
#include <CtrlGroup.h>#include <CtrlPot.h> // Classic ESP32 boards (e.g. the ESP32 Dev Module) have no A1,// so A4 is used there instead.#if defined(CONFIG_IDF_TARGET_ESP32)const uint8_t ANALOG_PIN_1 = A4;#elseconst uint8_t ANALOG_PIN_1 = A1;#endif // Create a potentiometer group.CtrlGroup potentiometerGroup; // Create 2 potentiometers with: pin number, max output value & sensitivity margin.CtrlPot potentiometer1(A0, 100, 0.05);CtrlPot potentiometer2(ANALOG_PIN_1, 100, 0.05); // Define an onValueChange handler. This will be triggered by all potentiometers registered to the group.void onValueChange(Groupable& potentiometer, int value) { // Retrieve the variables we need within this handler. bool canPrint = potentiometer.getBoolean("canPrint"); int id = potentiometer.getInteger("id"); String name = potentiometer.getString("name"); // if the potentiometer can print, we print to the serial monitor. if (canPrint) { Serial.print("Turned potentiometer with id: "); Serial.print(id); Serial.print(" / Name: "); Serial.print(name); Serial.print(" / Value: "); Serial.println(value); }} void setup() { Serial.begin(9600); // Register the onValueChange handler with the group. potentiometerGroup.setOnValueChange(onValueChange); // Register the potentiometers to the group. potentiometer1.setGroup(&potentiometerGroup); potentiometer2.setGroup(&potentiometerGroup); // Set all variables needed for potentiometer 1 & 2. potentiometer1.setInteger("id", 0); potentiometer1.setString("name", "Potentiometer 1"); potentiometer1.setBoolean("canPrint", true); potentiometer2.setInteger("id", 1); potentiometer2.setString("name", "Potentiometer 2"); potentiometer2.setBoolean("canPrint", true);} void loop() { // Process all potentiometers registered to the group. potentiometerGroup.process();}