Object groups
Group switching
This sketch demonstrates how to use group enable/disable to implement page or mode switching. This is useful when you have a limited number of physical controls but want them to serve different functions depending on the active mode.
In this example, a "mode" button switches between two groups:
- Group A: the potentiometer controls volume.
- Group B: the potentiometer controls filter cutoff.
Only one group is active at a time. The potentiometer is registered to both groups, but only the enabled group's handler fires.
The code
#include <CtrlGroup.h>
#include <CtrlBtn.h>
#include <CtrlPot.h>
CtrlGroup volumeGroup;
CtrlGroup filterGroup;
CtrlPot knob(A0, 127, 0.5);
void onVolumeChange(Groupable& pot, int value) {
Serial.print("Volume: ");
Serial.println(value);
}
void onFilterChange(Groupable& pot, int value) {
Serial.print("Filter: ");
Serial.println(value);
}
void onModeSwitch() {
if (volumeGroup.isEnabled()) {
volumeGroup.disable();
filterGroup.enable();
knob.setGroup(&filterGroup);
Serial.println("Switched to Filter mode");
} else {
filterGroup.disable();
volumeGroup.enable();
knob.setGroup(&volumeGroup);
Serial.println("Switched to Volume mode");
}
}
CtrlBtn modeButton(2, 15, onModeSwitch);
void setup() {
Serial.begin(9600);
volumeGroup.setOnValueChange(onVolumeChange);
filterGroup.setOnValueChange(onFilterChange);
knob.setGroup(&volumeGroup);
// Start with volume mode active, filter mode disabled.
volumeGroup.enable();
filterGroup.disable();
Serial.println("Volume mode active");
}
void loop() {
modeButton.process();
volumeGroup.process();
filterGroup.process();
}