Arduino: LED Flashing & Serial Monitor Status

Synopsis: The goal is to have three tables. Each table has its own section on how the LED’s should flash. The tables Main tables are predicated on the Potentiometer (POT) values, connected to analog(0). The LED flashes in table 2 and 3 use the POT values to dictate the flashing rate.

In addition to controlling the LED rates, we now want the values to be printed out via Serial Monitor. Below are the instructions and the code that Jenat A. Awad and I have written.

Note. More descriptive comments were added for clarification and good documentation practice.

Screen Shot 2015-10-18 at 4.07.55 PM

Screen Shot 2015-10-18 at 4.08.11 PM

Screen Shot 2015-10-18 at 4.08.22 PM

Here is the code:sk1

********************** Below is the code ********************

/*        Global Variables Defined        */

/**********************************************************************/
/**********************************************************************/
/*************************                  ***************************/
/*************************Group 1 Project 2 ***************************/
/*************************                  ***************************/
/**********************************************************************/
/**********************************************************************/

// Daniel D. Wilcox & Jenat A. Awad
// http://www.bucketofmass.com


// POT is connected to Analog Pin A0.
int POT = A0;
int val = 0; // value read from pot
int val2 = 0; // value read from pot / 2


/* three LED colored Green, Red, Yellow */
// output LED
int ledPin1 = 11; // led1
int ledPin2 = 12; // led2
int ledPin3 = 13; // led3

/* three DIP switches, debounching effect was tested on these switches  *  no coding was required to compensate, debounching is non-existant. */
// Input DIP Switchs
int SW1 = 8; // switch 1
int SW2 = 9; // switch 2
int SW3 = 10; // switch 3

/*Initially Set LED  to off state*/
// Sets SW States
int SwitchState1 = LOW;
int SwitchState2 = LOW;
int SwitchState3 = LOW;

/* Constants designated for each LED that holds the current stateeither off (LOW) or on (High). This is important since an IF-Statementcheckes the current state on each LED before choosing the next action.*/
int SwitchState11 = LOW;
int SwitchState22 = LOW;
int SwitchState33 = LOW;

/* a variable that holds the previous length of time the state of the LED *  was changed to. This is compaired to the currentmillis() variable, and then updated *  to equal the current millis if the length of time exceeds these defined previousMillis().  */
unsigned long previousMillis1 = 0; 
unsigned long previousMillis11 = 0; 
unsigned long previousMillis2 = 0;
unsigned long previousMillis4 = 0;
unsigned long previousMillis8 = 0;


/* For each length of time the LED state is stated it is to be devided by 2. *  for example if we want 1-second for the led to flash, we defined it to be  *  on for half a second, and the off for half a second. */
 
// interval for 1 Second
const long interval1s = 500;

// interval for 2 Seconds
const long interval2s = 1000;

// interval for 4 Seconds
const long interval4s = 2000;

// Interval for 8 Seconds
const long interval8s = 4000;

// serial moniter counter 
 int i=0;

void setup() {
// pinMode specified for each LED 
pinMode(ledPin1, OUTPUT); 
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);

//************** setting up  serial moniter  ***********
Serial.begin(9600);

// pinMode specified for each DIP Switch

pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
pinMode(SW3, INPUT);
}

void loop() {

/* For table 2 and 3 we require the input POT to dictate the  *  number of flashes, predicated on its valued-input. interger val2 *  is set to be devided by 2. */
 
int val  = analogRead(POT);
int val2 = (analogRead(POT))/2;

 // LED state must be set within this loop.
SwitchState1 = digitalRead(SW1);
SwitchState2 = digitalRead(SW2);
SwitchState3 = digitalRead(SW3);



/*************************  1st Table  ********************************/    

/* If POT value is less then 256 then Table 1, defined here, is executed. Pot *  value ranges in total from 0 - 1024.  */
if( val < 255 ) {
  
  
  // All closed are set to low. LED 3,2,1 should all be set to flash every 1 second.
        if (SwitchState3 == LOW && SwitchState2 == LOW && SwitchState1 == LOW){

unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >= interval1s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                SwitchState22 = LOW;
                      }}
         digitalWrite(ledPin2, SwitchState22);
         
 unsigned long currentMillis11 = millis();
  if (currentMillis11 - previousMillis11 >= interval1s)
      { // Saves time of LED Blink
        previousMillis11 = currentMillis11;

                      if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else  {
                  SwitchState33 = LOW;
                      } }digitalWrite(ledPin3, SwitchState33);
     
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= interval1s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     
                     // LED-1 2s YELLOW
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                SwitchState11 = LOW;
 }}
 digitalWrite(ledPin1, SwitchState11);

 /*----------------------  Serial Moniter  ----------------------*/                               

i++; // serial moniter counter - increments
if (i == 4000) {
Serial.print("OFF"   " "  "OFF" " "    "OFF"  "  "    "1F/S" " "    "1F/S" " "    "1F/S" "  ");
Serial.println(val);
  i=0;  
  }}
        
/* LED switches are set so that both LED 3 and 2 flash every 1 second, led 1 is to flash every 2 seconds */
    else if (SwitchState3 == LOW && SwitchState2 == LOW && SwitchState1 == HIGH)
 { // record timer 1s
  
 unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >= interval1s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                SwitchState22 = LOW;
                      }}
         digitalWrite(ledPin2, SwitchState22);

 unsigned long currentMillis11 = millis();
  if (currentMillis11 - previousMillis11 >= interval1s)
      { // Saves time of LED Blink
        previousMillis11 = currentMillis11;

                       //LED-3 1s GREEN
                      if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else  {
                  SwitchState33 = LOW;
                      } }digitalWrite(ledPin3, SwitchState33);

  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= interval2s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                SwitchState11 = LOW;
 }}digitalWrite(ledPin1, SwitchState11);

 
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("ON"   " "  "OFF" " "    "OFF"  "  "    "1F/S" " "    "1F/S" " "    "1F/2S" "  ");
Serial.println(val);  
i = 0; 
  }

 }  
 
 /* LED switches are set so flashing is configured so that: LED1  2s  ;  LED2  4s;  LED3  1s  */
        else if (SwitchState3 == LOW && SwitchState2 == HIGH && SwitchState1 == LOW)
 { 
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >=interval1s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);
    
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=interval2s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     
                     // LED-1 2s YELLOW
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      }} digitalWrite(ledPin1, SwitchState11);

  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >=interval4s)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }} digitalWrite(ledPin2, SwitchState22);        
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("OFF"   " "  "ON" " "    "OFF"  "  "    "1F/S" " "    "1F/4S" " "    "1F/2S" "  "   );
Serial.println(val);
  i = 0;  
  }} 

 /* LED switches are set so flashing is configured so that: LED1  2s  ;  LED2  4s;  LED3  8s  */            
          else if (SwitchState3 == LOW && SwitchState2 == HIGH && SwitchState1 == HIGH)
 {
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=interval2s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
       }}digitalWrite(ledPin1, SwitchState11);
       
  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >=interval4s)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }}digitalWrite(ledPin2, SwitchState22);

  unsigned long currentMillis8 = millis();
  if (currentMillis8 - previousMillis8 >=interval8s)
      { // Saves time of LED Blink
        previousMillis8 = currentMillis8;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                SwitchState33 = LOW;
                      }}digitalWrite(ledPin3, SwitchState33);


                      
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("ON"   " "  "ON" " "    "OFF"  "  "    "1F/8S" " "    "1F/4S" " "    "1F/2S" "  "   );
Serial.println(val);
  i = 0;  
  }}

 /* LED switches are set so flashing is configured so that: LED1  1s  ;  LED2  2s;  LED3  4s  */
        else if (SwitchState3 == HIGH && SwitchState2 == LOW && SwitchState1 == LOW)
 {

  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >= interval4s)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
                      }}digitalWrite(ledPin3, SwitchState33);

  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=interval2s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
       }}digitalWrite(ledPin2, SwitchState22);

  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >=interval1s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState11 == LOW) {
            SwitchState11 = HIGH;
                      } else {
               SwitchState11 = LOW;
                      }}digitalWrite(ledPin1, SwitchState11);



 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("OFF"   " "  "OFF" " "    "ON"  "  "    "1F/4S" " "    "1F/2S" " "    "1F/1S" "  "   );
Serial.println(val);
  i = 0;  
  }
                      } // end of else if 

 /* LED switches are set so flashing is configured so that: LED1  1s  ;  LED2  2s;  LED3  2s  */
        else if (SwitchState3 == HIGH && SwitchState2 == LOW && SwitchState1 == HIGH)
 {

  
                      unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >= interval2s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                SwitchState22 = LOW;
                      }}
         digitalWrite(ledPin2, SwitchState22);

 unsigned long currentMillis11 = millis();
  if (currentMillis11 - previousMillis11 >= interval2s)
      { // Saves time of LED Blink
        previousMillis11 = currentMillis11;

                      if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else  {
                  SwitchState33 = LOW;
                      } }digitalWrite(ledPin3, SwitchState33);
     
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= interval1s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                SwitchState11 = LOW;
 }}
 digitalWrite(ledPin1, SwitchState11);


  
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("ON"   " "  "OFF" " "    "ON"  "  "    "1F/2S" " "    "1F/2S" " "    "1F/1S" "  "   );
Serial.println(val);
  i = 0;  
  }}

 
/// All LEDS are set to flash every 2 seconds
 else if (SwitchState3 == HIGH && SwitchState2 == HIGH && SwitchState1 == LOW)
 {
                      unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >= interval2s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;

                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                SwitchState22 = LOW;
                      }}
         digitalWrite(ledPin2, SwitchState22);

 unsigned long currentMillis11 = millis();
  if (currentMillis11 - previousMillis11 >= interval2s)
      { // Saves time of LED Blink
        previousMillis11 = currentMillis11;

                      if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else  {
                  SwitchState33 = LOW;
                      } }digitalWrite(ledPin3, SwitchState33);
                      
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= interval2s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                SwitchState11 = LOW;
 }}
 digitalWrite(ledPin1, SwitchState11);

       
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("OFF"   " "  "ON" " "    "ON"  "  "    "1F/2S" " "    "1F/2S" " "    "1F/2S" "  "   );
Serial.println(val);
  i=0;  
  }

} // end of else if 

 /* LED switches are set so flashing is configured so that: LED1  2s  ;  LED2  4s;  LED3  2s   this is the last column for table 1*/
 else {
  unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=interval2s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
               SwitchState33 = LOW;
                      }digitalWrite(ledPin3, SwitchState33);
                      
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                SwitchState11 = LOW;
       }digitalWrite(ledPin1, SwitchState11);
                      }
  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >=interval4s)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }}digitalWrite(ledPin2, SwitchState22);

                      
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("ON"   " "  "ON" " "    "ON"  "  "    "1F/2S" " "    "1F/4S" " "    "1F/2S" "  "   );
Serial.println(val);
  i = 0;  
  }}} //Thats a Wrap! */

//************************* END 1st Table  ********************************/ 







//*************************  2nd Table    ********************************/    

/*  With table 2 we used only two switches. However the third led timing (in seconds) has been *  replaced by the POT value.  *  val =  same value of POT for timing interval *  val2 = half the value of POT for the timing interval, so the flashes should occur more frequently *  POT value ranges from 0 - 1024. */

 // If POT value is greater then 255 but less 512, TABLE 2 is executed. 
 else if ( val > 255  && val < 512 ) {   // if loop 1


/* Both switches closed. we expect LED 2 and LED 3 to flash at 1-seconds, and LED 1 repeats: Turn on state, Delay for POT value (in milliseconds), Turn off state, Delay for POT value (in milliseconds)*/

 if (SwitchState1 == LOW && SwitchState2 == LOW)
 {  
   // record timer 1s
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >=interval1s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);
    
     
     unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=interval1s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     // LED-1 2s YELLOW
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }digitalWrite(ledPin1, SwitchState22);} 


                      // record timer 4s
  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >=val)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     // LED-3 Ns RED
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      }
                      } digitalWrite(ledPin2, SwitchState11);
                      
 /*----------------------  Serial Moniter  ----------------------*/                                                    
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("OFF"   " "  "OFF" " "    "---"  "  " "1F/");
// LED 1 must display the POT value, as it uses this as its delay for flashing on and off
Serial.print(val);
Serial.print("s ");
Serial.print(   "1F/4S"   " "   "1F/2S" "  "   );
Serial.println(val);
  i = 0;                      
 }
 } // sub main if loop



                     
/* Switch 2 is closed, switch 1 is open. LED 2  flashes every 2-seconds, and LED 3 to flash at 1-seconds, and LED 1 repeats: Turn on state, Delay for POT value (in milliseconds), Turn off state, Delay for POT value (in milliseconds)*/

else if (SwitchState1 == HIGH && SwitchState2 == LOW)    // closed is low and open is high
 { 
   // record timer 1s
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >=interval1s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);
    
     
     unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=interval2s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     // LED-1 2s YELLOW
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }digitalWrite(ledPin1, SwitchState22);} 

  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >= val)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     // LED-3 Ns RED
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      }
                      } digitalWrite(ledPin2, SwitchState11);
                      
 /*----------------------  Serial Moniter  ----------------------*/                                                     
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("ON"   " "  "OFF" " "    "---"  "  " "1F/");
// LED 1 must display the POT value, as it uses this as its delay for flashing on and off
Serial.print(val);
Serial.print("s ");
Serial.print(   "1F/4S"   " "   "1F/2S" "  "   );
Serial.println(val);
  i = 0;                      
}} // sub main if loop

/* Switch 1 is closed, switch 2 is open. LED 2 flashes every 1-seconds, and LED 3 to flash at 2-seconds, and LED 1 repeats: Turn on state, Delay for POT value (in milliseconds), Turn off state, Delay for POT value (in milliseconds)*/

else if (SwitchState1 == LOW  && SwitchState2 == HIGH)
 { 
   // record timer 2s
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >= interval2s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);
    
     unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= interval1s)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     // LED-1 2s YELLOW
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }}digitalWrite(ledPin1, SwitchState22); 

  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >= val)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     // LED-3 Ns RED
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      }
                      } digitalWrite(ledPin2, SwitchState11);
                      
 /*----------------------  Serial Moniter  ----------------------*/                                                    
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("OFF"   " "  "ON" " "    "---"  "  " "1F/");
// LED 1 must display the POT value, as it uses this as its delay for flashing on and off
Serial.print(val);
Serial.print("s ");
Serial.print(   "1F/4S"   " "   "1F/2S" "  "   );
Serial.println(val);
  i = 0;                      
}} // sub main if loop
                      
                      
/* Switch 1 & 2 are open. LED 2 flashes every 2-seconds, and LED 3 to flash at 4-seconds, and LED 1 repeats: Turn on state, Delay for POT value (in milliseconds), Turn off state, Delay for POT value (in milliseconds)*/

else if(SwitchState1 == HIGH  && SwitchState2 == HIGH )
 { 
   // record timer 1s
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >=interval4s)// yellow 4s
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);
    
     unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=interval2s) // red 2s
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     // LED-1 2s YELLOW
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }}digitalWrite(ledPin1, SwitchState22); 

  unsigned long currentMillis4 = millis();
  if ( currentMillis4 - previousMillis4 >= val )
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      
                      }}digitalWrite(ledPin2, SwitchState11);
 /*----------------------  Serial Moniter  ----------------------*/                                                    
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("ON"   " "  "ON" " "    "---"  "  " "1F/");
// LED 1 must display the POT value, as it uses this as its delay for flashing on and off
Serial.print(val);
Serial.print("s ");
Serial.print(   "1F/4S"   " "   "1F/2S" "  "   );
Serial.println(val);
  i = 0;                      
}}// sub main if loop
                      

}// end of this table if statement 



/***********************  3rd Table  ********************************/    

/* Pot value is greater then 512. We did not require to define this here, sincethis is al else statement. */

else {   // if loop 1

/* Switch 1 & 2 are closed. LED 3 flashes every 1-seconds.  *  LED 1 Repeats: *  Turn on state, Delay for POT value (in milliseconds), Turn off state, Delay for POT value (in milliseconds) *  LED 2 Repeats: *  Turn on state, Delay for POT-Value/2 (in Millisecond), Turn off State, Delay for Pot-Value/2 (in Millisecond)*/

 if (SwitchState1 == LOW && SwitchState2 == LOW)
 {     // 2
   // record timer 1s
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >=interval1s) // YELLOW
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);

     unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=val2)   // GREEN 
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     // LED-1 2s YELLOW
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }}digitalWrite(ledPin1, SwitchState22);
                      
  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >=val)  // RED
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      }
                      } digitalWrite(ledPin2, SwitchState11);

 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("OFF"   " "  "OFF" " "    "---"  "  " "1F/");
// LED 1 must display the POT value, as it uses this as its delay for flashing on and off
Serial.print(val);
Serial.print("s ");
// LED 2 must display the POT/2 value, as it uses this as the delay for flashing on and off
Serial.print("1F/");
Serial.print(val2);
Serial.print("s ");
Serial.print( " "   "1F/1S" "  "   );
Serial.println(val);
  i = 0;                     
 }
 
 } // sub main if loop
                      
/* Switch 1 is open, switch 2 is closed. LED 3 flashes every 2-seconds.  *  LED 1 Repeats: *  Turn on state, Delay for POT value (in milliseconds), Turn off state, Delay for POT value (in milliseconds) *  LED 2 Repeats: *  Turn on state, Delay for POT-Value/2 (in Millisecond), Turn off State, Delay for Pot-Value/2 (in Millisecond)*/

else if (SwitchState1 == HIGH && SwitchState2 == LOW)    // closed is low and open is high
 { 
   // record timer 1s
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >=interval2s)   // yellow
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);

     unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=val2)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     // LED-1 2s YELLOW
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }digitalWrite(ledPin1, SwitchState22);} 

  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >=val)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     // LED-3 Ns RED
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      }
                      } digitalWrite(ledPin2, SwitchState11);
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("ON"   " "  "OFF" " "    "---"  "  " "1F/");
// LED 1 must display the POT value, as it uses this as its delay for flashing on and off
Serial.print(val);
Serial.print("s ");
// LED 2 must display the POT/2 value, as it uses this as the delay for flashing on and off
Serial.print("1F/");
Serial.print(val2);
Serial.print("s ");
Serial.print( " "   "1F/2S" "  "   );
Serial.println(val);
  i = 0;                      
 }} // sub main if loop

/* Switch 1 is closed, Switch 2 is open. LED 3 flashes every 4-seconds.  *  LED 1 Repeats: *  Turn on state, Delay for POT value (in milliseconds), Turn off state, Delay for POT value (in milliseconds) *  LED 2 Repeats: *  Turn on state, Delay for POT-Value/2 (in Millisecond), Turn off State, Delay for Pot-Value/2 (in Millisecond)*/

else if (SwitchState1 == LOW  && SwitchState2 == HIGH)
 { 
   // record timer 2s
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >= interval4s)
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);
    
     unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >= val2)
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     // LED-1 2s YELLOW
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }}digitalWrite(ledPin1, SwitchState22); 

  unsigned long currentMillis4 = millis();
  if (currentMillis4 - previousMillis4 >= val)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      }
                      } digitalWrite(ledPin2, SwitchState11);
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("OFF"   " "  "ON" " "    "---"  "  " "1F/");
// LED 1 must display the POT value, as it uses this as its delay for flashing on and off
Serial.print(val);
Serial.print("s ");
// LED 2 must display the POT/2 value, as it uses this as the delay for flashing on and off
Serial.print("1F/");
Serial.print(val2);
Serial.print("s ");
Serial.print( " "   "1F/4S" "  "   );
Serial.println(val);
  i = 0;                      
 }} // sub main if loop
                          
/* Switch 1 & 2 are open. LED 3 flashes every 8-seconds.  *  LED 1 Repeats: *  Turn on state, Delay for POT value (in milliseconds), Turn off state, Delay for POT value (in milliseconds) *  LED 2 Repeats: *  Turn on state, Delay for POT-Value/2 (in Millisecond), Turn off State, Delay for Pot-Value/2 (in Millisecond)*/

else if (SwitchState1 == HIGH  && SwitchState2 == HIGH )
 { 
   // record timer 1s
  unsigned long currentMillis1 = millis();
  if (currentMillis1 - previousMillis1 >=interval8s)// yellow 4s
      { // Saves time of LED Blink
        previousMillis1 = currentMillis1;
                     
                    
                     if (SwitchState33 == LOW) {
              SwitchState33 = HIGH;
                      } else {
                 SwitchState33 = LOW;
    }} digitalWrite(ledPin3, SwitchState33);
    
     unsigned long currentMillis2 = millis();
  if (currentMillis2 - previousMillis2 >=val2) 
      { // Saves time of LED Blink
        previousMillis2 = currentMillis2;
                     // LED-1 2s YELLOW
                     if (SwitchState22 == LOW) {
              SwitchState22 = HIGH;
                      } else {
                 SwitchState22 = LOW;
                      }}digitalWrite(ledPin1, SwitchState22); 

  unsigned long currentMillis4 = millis();
  if ( currentMillis4 - previousMillis4 >= val)
      { // Saves time of LED Blink
        previousMillis4 = currentMillis4;
                     
                     // LED-3 Ns RED
                     if (SwitchState11 == LOW) {
              SwitchState11 = HIGH;
                      } else {
                 SwitchState11 = LOW;
                      
                     } } digitalWrite(ledPin2, SwitchState11);
                     
 /*----------------------  Serial Moniter  ----------------------*/                               
i++; // serial moniter counter - increments
 if (i == 4000) {
Serial.print("ON"   " "  "ON" " "    "---"  "  " "1F/");
// LED 1 must display the POT value, as it uses this as its delay for flashing on and off
Serial.print(val);
Serial.print("s ");
// LED 2 must display the POT/2 value, as it uses this as the delay for flashing on and off
Serial.print("1F/");
Serial.print(val2);
Serial.print("s ");
Serial.print( " "   "1F/8S" "  "   );
Serial.println(val);
  i = 0;                      
}// sub main if loop
}}}// END Of Void loop()

Leave a Comment

Your email address will not be published. Required fields are marked *