|
| 1 | +//****************************************************** |
| 2 | +// LED Panel and Presentation OUTPUT |
| 3 | +const int LED1 = 13; |
| 4 | +const int LED2 = 12; |
| 5 | +const int LED3 = 11; |
| 6 | +const int LED4 = 10; |
| 7 | +const int LED5 = 9; |
| 8 | +const int LED6 = 8; |
| 9 | +const int LED7 = 7; |
| 10 | +const int LED8 = 6; |
| 11 | +const int LED9 = 5; |
| 12 | +const int LED_P = 4; |
| 13 | + |
| 14 | +// Buttons (D. INPUT) |
| 15 | +const int BN1 = 3; |
| 16 | +const int BN2 = 2; |
| 17 | +// Buttons (A. INPUT) - Analog pins are INPUT by default |
| 18 | +#define BN3 A2 |
| 19 | +#define BN4 A3 |
| 20 | +#define BN5 A4 |
| 21 | + |
| 22 | +// Diode Inputs |
| 23 | +#define D1 A0 |
| 24 | +#define D2 A1 |
| 25 | +//****************************************************** |
| 26 | +// Variables to keep track |
| 27 | +/* Order of variables - 1:ALL ON - 2:AUTO OFF - 3:AUTO - 4:Custom |
| 28 | + Presentation is separate. |
| 29 | + states are switched by using different buttons. presentation is turned on and off |
| 30 | +by toggling same button. its also connected to other states.*/ |
| 31 | +int state_of4 = 2; // All off initially |
| 32 | +byte presentationState = 0; // Presentation off initially |
| 33 | +bool auto_transition = false; // transition to auto mode |
| 34 | +int previous_state = 2; |
| 35 | + |
| 36 | +//****************************************************** |
| 37 | +// Serial Comm variables and functions------------------------------- |
| 38 | +String COMstring; // String to store input from Serial Port |
| 39 | +String COMsequence; // String to store input sequence from Serial Port |
| 40 | +int sequence[9] = {1, 0, 1, |
| 41 | + 0, 1, 0, |
| 42 | + 1, 0, 1}; |
| 43 | + |
| 44 | +#define STRING2SEQUENCE for(int i=0; i<9; i++) {sequence[i] = (COMsequence.charAt(i)=='1') ? 1 : 0;} |
| 45 | +#define SWITCHBYSEQUENCE(seq, num) for(int i=0; i<num; i++) {(seq[i]==1) ? digitalWrite(getLedPin(i), HIGH) : digitalWrite(getLedPin(i), LOW);} |
| 46 | + |
| 47 | +// Sequences---------------------------------------------------------- |
| 48 | +// All LEDs and presentation on seqeunce |
| 49 | +int all_on[10] = {1, 1, 1, |
| 50 | + 1, 1, 1, |
| 51 | + 1, 1, 1, |
| 52 | + 1}; |
| 53 | +// All LEDs and presentation off seqeunce |
| 54 | +int all_off[10] = {0,}; |
| 55 | +// Automatic Diode input sequences (2-inputs -> 9-outputs) |
| 56 | +int auto_1[9] = {0, 0, 0, /* D1=1 D2=1*/ |
| 57 | + 0, 0, 0, |
| 58 | + 1, 1, 1}; |
| 59 | +int auto_2[9] = {0, 0, 1, /* D1=1 D2=0*/ |
| 60 | + 0, 0, 1, |
| 61 | + 1, 1, 1}; |
| 62 | +int auto_3[9] = {1, 0, 0, /* D1=0 D2=1*/ |
| 63 | + 1, 0, 0, |
| 64 | + 1, 1, 1}; |
| 65 | +#define auto_4 all_on // D1=0 D2=0 |
| 66 | + |
| 67 | +const int getLedPin(int index) // For turning on LEDs based on sequence |
| 68 | +{ |
| 69 | + switch(index) |
| 70 | + { |
| 71 | + case 0: |
| 72 | + return LED1; |
| 73 | + case 1: |
| 74 | + return LED2; |
| 75 | + case 2: |
| 76 | + return LED3; |
| 77 | + case 3: |
| 78 | + return LED4; |
| 79 | + case 4: |
| 80 | + return LED5; |
| 81 | + case 5: |
| 82 | + return LED6; |
| 83 | + case 6: |
| 84 | + return LED7; |
| 85 | + case 7: |
| 86 | + return LED8; |
| 87 | + case 8: |
| 88 | + return LED9; |
| 89 | + case 9: |
| 90 | + return LED_P; |
| 91 | + default: |
| 92 | + return -1; // error |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Function to toggle LED presentation with software debouncing |
| 97 | +void togglePresentation() { |
| 98 | + static unsigned long lastDebounceTime = 0; |
| 99 | + unsigned long debounceDelay = 1000; // Adjust debounce delay as needed |
| 100 | + unsigned long currentTime = millis(); |
| 101 | + |
| 102 | + if (currentTime - lastDebounceTime >= debounceDelay) { |
| 103 | + // Toggle presentation state |
| 104 | + presentationState = !presentationState; |
| 105 | + digitalWrite(LED_P, presentationState); |
| 106 | + (presentationState) ? Serial.println("*P1\r\n") : Serial.println("*P0\r\n"); |
| 107 | + lastDebounceTime = currentTime; |
| 108 | + } |
| 109 | +} |
| 110 | +//****************************************************** |
| 111 | + |
| 112 | +// Setup Pins |
| 113 | +#define PIN_OUT(num) pinMode(LED##num, OUTPUT); |
| 114 | +#define PIN_IN(num) pinMode(BN##num, INPUT); |
| 115 | + |
| 116 | +//****************************************************** |
| 117 | +void setup() |
| 118 | +{ |
| 119 | + PIN_OUT(1) PIN_OUT(2) PIN_OUT(3) |
| 120 | + PIN_OUT(4) PIN_OUT(5) PIN_OUT(6) |
| 121 | + PIN_OUT(7) PIN_OUT(8) PIN_OUT(9) |
| 122 | + pinMode(LED_P, OUTPUT); |
| 123 | + PIN_IN(1) PIN_IN(2) |
| 124 | + // Start Serial Communication |
| 125 | + Serial.begin(9600); |
| 126 | +} |
| 127 | +void loop() |
| 128 | +{ |
| 129 | + // Get Serial Port Input from Desktop CLI------------------------------------ |
| 130 | + if(Serial.available()) |
| 131 | + { |
| 132 | + COMstring = Serial.readString(); |
| 133 | + if(COMstring.charAt(0)=='#' && COMstring.charAt(1)=='C') |
| 134 | + { |
| 135 | + // to get only sequence from '#C111000111\r\n' format and convert to int array |
| 136 | + COMsequence = COMstring.substring(2,11); |
| 137 | + STRING2SEQUENCE |
| 138 | + state_of4 = 4; // Set state to Custom mode |
| 139 | + Serial.println("*M4\r\n"); |
| 140 | + } |
| 141 | + if(COMstring.charAt(0)=='#' && COMstring.charAt(1)=='S') |
| 142 | + { |
| 143 | + Serial.println("*M2\r\n"); |
| 144 | + delay(2000); |
| 145 | + Serial.println("*P0\r\n"); |
| 146 | + } |
| 147 | + } |
| 148 | + // Read button and sensor inputs and assign states |
| 149 | + if(digitalRead(BN1)) // 1: Turn all ON |
| 150 | + { |
| 151 | + state_of4 = 1; |
| 152 | + } |
| 153 | + else if(digitalRead(BN2)) // 2: Turn all OFF |
| 154 | + { |
| 155 | + state_of4 = 2; |
| 156 | + } |
| 157 | + else if(digitalRead(BN3)) // 3: AUTO mode |
| 158 | + { |
| 159 | + state_of4 = 3; |
| 160 | + auto_transition = true; |
| 161 | + } |
| 162 | + else if(analogRead(BN5)) // 4: Custom mode |
| 163 | + { |
| 164 | + state_of4 = 4; |
| 165 | + } |
| 166 | + if(analogRead(BN4)) // Toggle Presentation--------- |
| 167 | + { |
| 168 | + /*presentationState = !presentationState; |
| 169 | + digitalWrite(LED_P, presentationState); |
| 170 | + (presentationState) ? Serial.println("#P1\r\n") : Serial.println("#P0\r\n");*/ |
| 171 | + togglePresentation(); |
| 172 | + delay(200); |
| 173 | + } |
| 174 | + |
| 175 | + // Do processes as per states |
| 176 | + switch (state_of4) |
| 177 | + { |
| 178 | + case 1: |
| 179 | + if(previous_state == 1) break; |
| 180 | + SWITCHBYSEQUENCE(all_on, 10); |
| 181 | + presentationState = 1; |
| 182 | + Serial.println("*M1\r\n"); |
| 183 | + state_of4 = 0; // Reset state |
| 184 | + previous_state = 1; |
| 185 | + break; |
| 186 | + case 2: |
| 187 | + if(previous_state == 2) break; |
| 188 | + SWITCHBYSEQUENCE(all_off, 10); |
| 189 | + presentationState = 0; |
| 190 | + Serial.println("*M2\r\n"); |
| 191 | + state_of4 = 0; |
| 192 | + previous_state = 2; |
| 193 | + break; |
| 194 | + case 3: |
| 195 | + if(digitalRead(D1) && digitalRead(D2)) |
| 196 | + { |
| 197 | + SWITCHBYSEQUENCE(auto_1, 9); |
| 198 | + } |
| 199 | + else if(digitalRead(D1) && !digitalRead(D2)) |
| 200 | + { |
| 201 | + SWITCHBYSEQUENCE(auto_2, 9); |
| 202 | + } |
| 203 | + else if(!digitalRead(D1) && digitalRead(D2)) |
| 204 | + { |
| 205 | + SWITCHBYSEQUENCE(auto_3, 9); |
| 206 | + } |
| 207 | + else |
| 208 | + { |
| 209 | + SWITCHBYSEQUENCE(auto_4, 9); |
| 210 | + } |
| 211 | + state_of4 = 3; // Keep same state |
| 212 | + if(auto_transition==true && previous_state != 3) |
| 213 | + { |
| 214 | + Serial.println("*M3\r\n"); |
| 215 | + } |
| 216 | + auto_transition = false; // transition over |
| 217 | + previous_state = 3; |
| 218 | + break; |
| 219 | + case 4: |
| 220 | + for(int i=0; i<9; i++) |
| 221 | + { |
| 222 | + (sequence[i]==1) ? digitalWrite(getLedPin(i), HIGH) : digitalWrite(getLedPin(i), LOW); |
| 223 | + } |
| 224 | + if(previous_state != 4) |
| 225 | + { |
| 226 | + Serial.println("*M4\r\n"); |
| 227 | + } |
| 228 | + state_of4 = 0; |
| 229 | + previous_state = 4; |
| 230 | + break; |
| 231 | + default: |
| 232 | + break; |
| 233 | + } |
| 234 | +} |
| 235 | +//****************************************************** |
0 commit comments