Last active
January 23, 2026 19:00
-
-
Save cdolek/40ff037c1e78dc3d3e71626122af23cc to your computer and use it in GitHub Desktop.
dad multiplexer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| PMDG 737 Overhead - Pin & Mux Finder | |
| Finds which standard pin or mux (74HC4067/CD74HC4067) channel changes when you flip a switch. | |
| Assumptions: | |
| - Using Arduino Mega (pins 2-53 exist). | |
| - Mux address pins S0..S3 drive all mux boards in parallel. | |
| - Each mux board has its own SIG line wired to one Arduino analog pin in sigPins[]. | |
| - EN on every mux is tied LOW (enabled). | |
| - Switches connect channel -> GND (so INPUT_PULLUP works, LOW = ON). | |
| */ | |
| // ===== DEVICE SELECTION ===== | |
| // Device 1: S0-S3 on pins 2,3,4,5 | |
| // Device 2: S0-S3 on pins 29,30,31,32 | |
| // Set via Serial at startup, or change default here: | |
| uint8_t activeDevice = 2; // Default to Device 2 (send '1' via Serial to switch) | |
| // Device 1 pins | |
| const uint8_t D1_S0 = 2; | |
| const uint8_t D1_S1 = 3; | |
| const uint8_t D1_S2 = 4; | |
| const uint8_t D1_S3 = 5; | |
| // Device 2 pins | |
| const uint8_t D2_S0 = 29; | |
| const uint8_t D2_S1 = 30; | |
| const uint8_t D2_S2 = 31; | |
| const uint8_t D2_S3 = 32; | |
| // Active pins (set after device selection) | |
| uint8_t S0, S1, S2, S3; | |
| const uint8_t sigPins[] = {A0, A1, A2, A3, A4, A5, A6, A7}; | |
| const uint8_t numMux = sizeof(sigPins) / sizeof(sigPins[0]); | |
| bool lastMuxState[numMux][16]; | |
| bool lastPinState[54]; | |
| // DEBUG: Print full state every N milliseconds (set to 0 to disable) | |
| const unsigned long DEBUG_INTERVAL_MS = 5000; | |
| unsigned long lastDebugPrint = 0; | |
| static inline void setMuxChannel(uint8_t ch) { | |
| digitalWrite(S0, (ch >> 0) & 0x01); | |
| digitalWrite(S1, (ch >> 1) & 0x01); | |
| digitalWrite(S2, (ch >> 2) & 0x01); | |
| digitalWrite(S3, (ch >> 3) & 0x01); | |
| delayMicroseconds(50); // settle time | |
| } | |
| static inline void printP3(int n) { | |
| Serial.print("P"); | |
| if (n < 100) Serial.print("0"); | |
| if (n < 10) Serial.print("0"); | |
| Serial.print(n); | |
| } | |
| void selectDevice(uint8_t device) { | |
| activeDevice = device; | |
| if (device == 1) { | |
| S0 = D1_S0; S1 = D1_S1; S2 = D1_S2; S3 = D1_S3; | |
| Serial.println("Device 1 selected (pins 2,3,4,5)"); | |
| } else { | |
| S0 = D2_S0; S1 = D2_S1; S2 = D2_S2; S3 = D2_S3; | |
| Serial.println("Device 2 selected (pins 29,30,31,32)"); | |
| } | |
| } | |
| void directPinTest() { | |
| // Direct test: read A0-A7 without mux logic | |
| Serial.println("\n--- DIRECT PIN TEST (A0-A7) ---"); | |
| Serial.println("Short each pin to GND with a wire to verify."); | |
| for (uint8_t idx = 0; idx < 8; idx++) { | |
| uint8_t pin = A0 + idx; | |
| pinMode(pin, INPUT_PULLUP); | |
| delay(5); | |
| bool state = digitalRead(pin); | |
| Serial.print("A"); | |
| Serial.print(idx); | |
| Serial.print(": "); | |
| Serial.println(state == LOW ? "LOW (connected to GND)" : "HIGH (floating/open)"); | |
| } | |
| Serial.println("--- END DIRECT TEST ---"); | |
| Serial.println("If all show HIGH, short A0 to GND and send 'T' again.\n"); | |
| } | |
| bool isMuxAddressPin(uint8_t pin) { | |
| // Skip address pins for BOTH devices to avoid conflicts | |
| return (pin >= 2 && pin <= 5) || (pin >= 29 && pin <= 32); | |
| } | |
| void initializePins() { | |
| // Mux address pins | |
| pinMode(S0, OUTPUT); | |
| pinMode(S1, OUTPUT); | |
| pinMode(S2, OUTPUT); | |
| pinMode(S3, OUTPUT); | |
| setMuxChannel(0); | |
| // SIG pins | |
| for (uint8_t m = 0; m < numMux; m++) { | |
| pinMode(sigPins[m], INPUT_PULLUP); | |
| } | |
| // Standard pins as INPUT_PULLUP (skip mux address pins for both devices) | |
| for (uint8_t i = 2; i < 54; i++) { | |
| if (isMuxAddressPin(i)) continue; | |
| pinMode(i, INPUT_PULLUP); | |
| lastPinState[i] = digitalRead(i); | |
| } | |
| // Initialize mux states | |
| delay(10); | |
| for (uint8_t ch = 0; ch < 16; ch++) { | |
| setMuxChannel(ch); | |
| for (uint8_t m = 0; m < numMux; m++) { | |
| (void)digitalRead(sigPins[m]); | |
| lastMuxState[m][ch] = digitalRead(sigPins[m]); | |
| } | |
| } | |
| } | |
| void setup() { | |
| Serial.begin(115200); | |
| Serial.println("\n--- PMDG 737 PIN/MUX TARAYICI ---"); | |
| Serial.println(" 1 = Device 1 (S0-S3 on pins 2,3,4,5)"); | |
| Serial.println(" 2 = Device 2 (S0-S3 on pins 29,30,31,32)"); | |
| Serial.println(" T = Direct A0-A7 test (bypass mux)"); | |
| Serial.println("Send '1', '2', or 'T' via Serial.\n"); | |
| // Use default device | |
| selectDevice(activeDevice); | |
| initializePins(); | |
| Serial.println("Baslangic durumu okundu. Hazir."); | |
| Serial.println("Lutfen bir switch'i hareket ettirin...\n"); | |
| } | |
| void loop() { | |
| // Check for device switch command via Serial | |
| if (Serial.available() > 0) { | |
| char c = Serial.read(); | |
| if (c == '1' && activeDevice != 1) { | |
| selectDevice(1); | |
| initializePins(); | |
| Serial.println("Switched to Device 1. Ready.\n"); | |
| } else if (c == '2' && activeDevice != 2) { | |
| selectDevice(2); | |
| initializePins(); | |
| Serial.println("Switched to Device 2. Ready.\n"); | |
| } else if (c == 'T' || c == 't') { | |
| directPinTest(); | |
| } | |
| } | |
| // DEBUG: Periodically dump all mux states to verify wiring | |
| if (DEBUG_INTERVAL_MS > 0 && (millis() - lastDebugPrint >= DEBUG_INTERVAL_MS)) { | |
| lastDebugPrint = millis(); | |
| Serial.println("\n--- DEBUG: MUX STATE DUMP ---"); | |
| for (uint8_t m = 0; m < numMux; m++) { | |
| Serial.print("Mux "); | |
| Serial.print(m); | |
| Serial.print(" (A"); | |
| Serial.print(sigPins[m] - A0); | |
| Serial.print("): "); | |
| for (uint8_t ch = 0; ch < 16; ch++) { | |
| setMuxChannel(ch); | |
| delayMicroseconds(50); | |
| bool state = digitalRead(sigPins[m]); | |
| Serial.print(state == LOW ? "1" : "0"); // 1 = ON (LOW), 0 = OFF (HIGH) | |
| } | |
| Serial.println(); | |
| } | |
| Serial.println("(1=LOW/ON, 0=HIGH/OFF)"); | |
| Serial.println("--- END DEBUG ---\n"); | |
| } | |
| // 1) Scan standard pins | |
| for (uint8_t i = 2; i < 54; i++) { | |
| if (isMuxAddressPin(i)) continue; | |
| bool currentState = digitalRead(i); | |
| if (currentState != lastPinState[i]) { | |
| Serial.print("STANDART PIN DEGISTI -> Pin: "); | |
| Serial.print(i); | |
| Serial.println(currentState == LOW ? " (BASILDI / ON)" : " (BIRAKILDI / OFF)"); | |
| lastPinState[i] = currentState; | |
| delay(30); | |
| } | |
| } | |
| // 2) Scan mux channels | |
| for (uint8_t ch = 0; ch < 16; ch++) { | |
| setMuxChannel(ch); | |
| for (uint8_t m = 0; m < numMux; m++) { | |
| (void)digitalRead(sigPins[m]); // optional glitch flush | |
| bool currentState = digitalRead(sigPins[m]); | |
| if (currentState != lastMuxState[m][ch]) { | |
| int totalID = (m * 16) + ch; | |
| Serial.print("MUX TESPIT EDILDI -> Mux No: "); | |
| Serial.print(m); | |
| Serial.print(" | Kanal: "); | |
| Serial.print(ch); | |
| Serial.print(" | ESKI LUA KODU: "); | |
| printP3(totalID); | |
| Serial.println(currentState == LOW ? " (ON)" : " (OFF)"); | |
| lastMuxState[m][ch] = currentState; | |
| delay(30); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment