#define POSITION_NUM 4
#define ON LOW
#define OFF HIGH
// define the pins connected to the dip switch
const int SWITCH_PINS[] = { 26, 27, 14, 12 };
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set the DIP switch pins as inputs with pull-up resistors enabled
for (int i = 0; i < POSITION_NUM; i++)
pinMode(SWITCH_PINS[i], INPUT_PULLUP);
}
void loop() {
int encoded_state = 0;
for (int i = 0; i < POSITION_NUM; i++) {
int state = digitalRead(SWITCH_PINS[i]);
if (state == ON)
encoded_state |= 1 << (POSITION_NUM - i - 1);
}
Serial.print("encoded state: ");
Serial.println(encoded_state);
// add a delay to prevent rapid readings
delay(500);
}