// For later const int W_H = 1; const int W_L = 0; const int M_O = 1; const int M_I = 0; // MAX PINS const int MAX_PINS = 12; // Hold pin info struct { int m; int w; int number; } pins[12]; // Characters for pin commands char pin_number = 'x'; char pin_action = 'x'; char pin_chars[MAX_PINS] = {'2','3','4','5','6','7','8','9','A','B','C','D'}; void setup() { // Initialize pins and struct for (int pc=0; pc < MAX_PINS; pc++) { int temp_pin = pc + 2; pinMode(temp_pin, INPUT); pins[pc].m = M_I; pins[pc].w = W_L; pins[pc].number = temp_pin; } // Start serial Serial.begin(9600); while(!Serial) { ; } } void loop() { // Check for incoming bits that are looking for the controller if (Serial.available() > 0) { char c = Serial.read(); if (pin_number == 'x') { pin_number = c; } else if (pin_action == 'x') { pin_action = c; } } // If you have enough, go if (pin_number != 'x' && pin_action != 'x') { goCommand(); } // Pause delay(100); } int findPin(int pin) { for(int cnt=0; cnt < MAX_PINS; cnt++) { if (pin == pins[cnt].number) { return cnt; } } return 99; } int findPinFromChar() { for (int cnt = 0; cnt < MAX_PINS; cnt++) { if (pin_chars[cnt] == pin_number) { return cnt; } } return 99; } void switchMode(int pin) { int c = findPin(pin); if (c >= MAX_PINS) { return; } if (pins[c].m == M_O) { pinMode(pins[c].number, INPUT); pins[c].m = M_I; } else { pinMode(pins[c].number, OUTPUT); pins[c].m = M_O; } } void switchWrite(int pin) { int c = findPin(pin); if (c >= MAX_PINS) { return; } if (pins[c].m == M_I) { return; } if (pins[c].w == W_H) { digitalWrite(pins[c].number, LOW); pins[c].w = W_L; } else { digitalWrite(pins[c].number, HIGH); pins[c].w = W_H; } } void feedBack() { int p = findPinFromChar(); if (p == 99) return; int pmw = pins[p].m + (pins[p].w * 2); Serial.print(pin_chars[p]); Serial.print(pmw, DEC); } void resetChars() { pin_number = 'x'; pin_action = 'x'; } void goCommand() { if (pin_action != 'M' && pin_action != 'W') { resetChars(); return; } int p = findPinFromChar(); if (p >= MAX_PINS) { resetChars(); return; } if (pin_action == 'M') { switchMode(pins[p].number); } if (pin_action == 'W') { switchWrite(pins[p].number); } feedBack(); resetChars(); }