//---declarations for interrupts
typedef struct {
const uint8_t PIN;
bool changed;
bool value;
} fSwitch;
// SPDT switches connected to pins 27,33,15,32,14 (all adjacent in the Adafruit ESP32 Feather V2)
fSwitch FS[5] = {{37,0,0},{36,0,0},{35,0,0},{34,0,0},{33,0,0}};
volatile bool sFlag = false;
void IRAM_ATTR chkFS(void *arg) {
fSwitch *fs = static_cast<fSwitch *>(arg);
fs->changed = true;
fs->value = digitalRead(fs->PIN);
sFlag = true;
}
//---declarations for Buzzer
int buzzPin = 12;
int buzzChan = 0;
long buzzTimer = 0;
void setup() {
Serial.begin(115200);
Serial.println("Start");
for (byte i=0; i<5; i++) {
pinMode(FS[i].PIN,INPUT_PULLUP);
FS[i].value = digitalRead(FS[i].PIN); // initial state of each switch
Serial.println("Initial State FS" + String(i) + "=" + String(FS[i].value));
attachInterruptArg(FS[i].PIN,chkFS,&FS[i],CHANGE);
}
ledcAttachPin(buzzPin, buzzChan);
}
void loop() {
if (sFlag) { // if any PIN has changed
ledcWriteTone(buzzChan,300);
buzzTimer = millis();
sFlag = false;
for (byte i=0; i<5; i++)
if (FS[i].changed) { // determine which PIN changed
Serial.println("Changed State FS" + String(i) + "=" + String(FS[i].value));
FS[i].changed = false;
}
}
if (millis() - buzzTimer > 40) ledcWriteTone(buzzChan,0);
}
Loading
esp32-s2-devkitm-1
esp32-s2-devkitm-1