struct Button {
const char* name; // Name of the button
int pin; // Pin number
bool pressed; // State of the button
};
Button buttons[] = {
{"shakeNum", 2, false},
{"scaleX", 3, false},
{"scaleY", 4, false}
};
const int potPin = A0; // Potentiometer connected to analog pin A0
int activeButtonIndex = -1; // Index of the currently active button, -1 means no button is active
void setup() {
for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++) {
pinMode(buttons[i].pin, INPUT_PULLUP);
}
Serial.begin(9600);
}
void loop() {
int potValue = analogRead(potPin);
for (int i = 0; i < sizeof(buttons)/sizeof(buttons[0]); i++) {
if (digitalRead(buttons[i].pin) == LOW) {
if (activeButtonIndex != i) { // Check if a different button is pressed
activeButtonIndex = i; // Update the active button index
for (int j = 0; j < sizeof(buttons)/sizeof(buttons[0]); j++) {
buttons[j].pressed = false; // Reset all buttons
}
buttons[i].pressed = true; // Set the current button as pressed
}
}
}
if (activeButtonIndex != -1) { // Check if there is an active button
Serial.print("\r\r"); // Carriage return to return cursor to the start of the line
Serial.print(buttons[activeButtonIndex].name);
Serial.print(": ");
Serial.print(potValue);
// Fill with spaces to clear out any leftover characters from longer previous messages
//Serial.print(" ");
}
delay(100); // Delay to reduce the rate of updating (adjust as necessary)
}