Arduino: Simultaneously Flashing Delta-Rate LEDs

Synopsis: So the goal (see picture) is to have three dip switches connected to Pins 8,9,10 and two also have three LEDs connected to Pins 11,12,13. Depending on the switch position (High vs. Low) the LED’s are supposed to flash at different rates. The catch is that, they must all flash at different rates.

 

Instructions:

Arduino Task: Create three simultaneous flashing LEDs as different switches are turned on (All three switches combination).

Arduino Task: Create three simultaneous flashing LEDs as different switches are turned on (All three switches combination).

/*        Global Variables Defined        */

/* Daniel D. Wilcox : Jenat A. Awad*/

/* Designated output and input pins  */

// output LED
int ledPin1 = 11; // led1
int ledPin2 = 12; // led2
int ledPin3 = 13; // led3

// Input Switch
int SW1 = 8; // switch 1
int SW2 = 9; // switch 2
int SW3 = 10; // switch 3

// Sets SW States
int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
// LED Timer Holder
int ledState11 = LOW;
int ledState22 = LOW;
int ledState33 = LOW;

/* Starts timer and defined times */
unsigned long previousMillis1 = 0;
unsigned long previousMillis11 = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis4 = 0;
unsigned long previousMillis8 = 0;

// 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;

void setup() {
 // put your setup code here, to run once:

// pinMode for each LED
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);

// pinMode for each Switch

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

void loop() {

ledState1 = digitalRead(SW1);
ledState2 = digitalRead(SW2);
ledState3 = digitalRead(SW3);

   
    if (ledState1 == LOW && ledState2 == LOW && ledState3 == LOW)
{ digitalWrite(ledPin1, HIGH);
 digitalWrite(ledPin2, HIGH);
 digitalWrite(ledPin3, HIGH);
 delay(500);
 digitalWrite(ledPin1, LOW);
 digitalWrite(ledPin2, LOW);
 digitalWrite(ledPin3, LOW);
 delay(500);
}

if (ledState1 == LOW && ledState2 == LOW && ledState3 == HIGH)
{ // record timer 1s
unsigned long currentMillis1 = millis();
 if (currentMillis1 previousMillis1 >= interval1s)
     { // Saves time of LED Blink
       previousMillis1 = currentMillis1;
                    
                    // LED-2 1s RED
                    if (ledState22 == LOW) {
             ledState22 = HIGH;
                     } else {
               ledState22 = LOW;
                     }}
        digitalWrite(ledPin2, ledState22);

                      // record timer 1s
unsigned long currentMillis11 = millis();
 if (currentMillis11 previousMillis11 >= interval1s)
     { // Saves time of LED Blink
       previousMillis11 = currentMillis11;

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

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

else if (ledState1 == LOW && ledState2 == HIGH && ledState3 == LOW)
{
  // record timer 1s
 unsigned long currentMillis1 = millis();
 if (currentMillis1 previousMillis1 >=interval1s)
     { // Saves time of LED Blink
       previousMillis1 = currentMillis1;
                    
                    // LED-3 1s GREEN
                    if (ledState33 == LOW) {
             ledState33 = HIGH;
                     } else {
                ledState33 = LOW;
   }} digitalWrite(ledPin3, ledState33);
   
     // record timer 2s
 unsigned long currentMillis2 = millis();
 if (currentMillis2 previousMillis2 >=interval2s)
     { // Saves time of LED Blink
       previousMillis2 = currentMillis2;
                    
                    // LED-1 2s YELLOW
                    if (ledState11 == LOW) {
             ledState11 = HIGH;
                     } else {
                ledState11 = LOW;
                     }} digitalWrite(ledPin1, ledState11);

                     // record timer 4s
 unsigned long currentMillis4 = millis();
 if (currentMillis4 previousMillis4 >=interval4s)
     { // Saves time of LED Blink
       previousMillis4 = currentMillis4;
                    
                    // LED-2 4s RED
                    if (ledState22 == LOW) {
             ledState22 = HIGH;
                     } else {
                ledState22 = LOW;
                     }} digitalWrite(ledPin2, ledState22);
                     
                     }
                 
else if (ledState1 == LOW && ledState2 == HIGH && ledState3 == HIGH)
{
 // record timer 2s
 unsigned long currentMillis2 = millis();
 if (currentMillis2 previousMillis2 >=interval2s)
     { // Saves time of LED Blink
       previousMillis2 = currentMillis2;
                    
                    // LED-1 2s YELLOW
                    if (ledState11 == LOW) {
             ledState11 = HIGH;
                     } else {
                ledState11 = LOW;
      }}digitalWrite(ledPin1, ledState11);

                     // record timer 4s
 unsigned long currentMillis4 = millis();
 if (currentMillis4 previousMillis4 >=interval4s)
     { // Saves time of LED Blink
       previousMillis4 = currentMillis4;
                    
                    // LED-2 4s RED
                    if (ledState22 == LOW) {
             ledState22 = HIGH;
                     } else {
                ledState22 = LOW;
                     }}digitalWrite(ledPin2, ledState22);

                     // record timer 8s
 unsigned long currentMillis8 = millis();
 if (currentMillis8 previousMillis8 >=interval8s)
     { // Saves time of LED Blink
       previousMillis8 = currentMillis8;
                    
                    // LED-3 8s GREEN
                    if (ledState33 == LOW) {
             ledState33 = HIGH;
                     } else {
               ledState33 = LOW;
                     }}digitalWrite(ledPin3, ledState33);
                     }

else if (ledState1 == HIGH && ledState2 == LOW && ledState3 == LOW)
{

 // record timer 4s
 unsigned long currentMillis4 = millis();
 if (currentMillis4 previousMillis4 >= interval4s)
     { // Saves time of LED Blink
       previousMillis4 = currentMillis4;
                    
                    // LED-3 4s GREEN
                    if (ledState33 == LOW) {
             ledState33 = HIGH;
                     } else {
                ledState33 = LOW;
                     }}digitalWrite(ledPin3, ledState33);

// record timer 2s
 unsigned long currentMillis2 = millis();
 if (currentMillis2 previousMillis2 >=interval2s)
     { // Saves time of LED Blink
       previousMillis2 = currentMillis2;
                    
                    // LED-2 2s RED
                    if (ledState22 == LOW) {
             ledState22 = HIGH;
                     } else {
                ledState22 = LOW;
      }}digitalWrite(ledPin2, ledState22);

                     // record timer 1s
 unsigned long currentMillis1 = millis();
 if (currentMillis1 previousMillis1 >=interval1s)
     { // Saves time of LED Blink
       previousMillis1 = currentMillis1;
                    
                    // LED-1 1s YELLOW
                    if (ledState11 == LOW) {
           ledState11 = HIGH;
                     } else {
              ledState11 = LOW;
                     }}digitalWrite(ledPin1, ledState11);

                     }

     
else if (ledState1 == HIGH && ledState2 == LOW && ledState3 == HIGH)
{
 // record timer 2s
 unsigned long currentMillis2 = millis();
 if (currentMillis2 previousMillis2 >=interval2s)
     { // Saves time of LED Blink
       previousMillis2 = currentMillis2;
                    
                    // LED-3 2s Green
                    if (ledState33 == LOW) {
             ledState33 = HIGH;
                     } else {
                ledState33 = LOW;
                     }
      digitalWrite(ledPin3, ledState33);
                    
                    // LED-2 2s 2nd-led RED
                    if (ledState22 == LOW) {
             ledState22 = HIGH;
                     } else {
                ledState22 = LOW;
      }}digitalWrite(ledPin2, ledState22);
      

      // record timer 1s
 unsigned long currentMillis1 = millis();
 if (currentMillis1 previousMillis1 >=interval1s)
     { // Saves time of LED Blink
       previousMillis1 = currentMillis1;
                    
                    // LED-1 1s YELLOW
                    if (ledState11 == LOW) {
             ledState11 = HIGH;
                     } else {
                ledState11 = LOW;
                     }}digitalWrite(ledPin1, ledState11);
                     }
                  
else if (ledState1 == HIGH && ledState2 == HIGH && ledState3 == LOW)
{
// Flashing every 2 s
digitalWrite(ledPin1, HIGH);
 digitalWrite(ledPin2, HIGH);
 digitalWrite(ledPin3, HIGH);
 delay(1000);
 digitalWrite(ledPin1, LOW);
 digitalWrite(ledPin2, LOW);
 digitalWrite(ledPin3, LOW);
 delay(1000);

}

else if (ledState1 == HIGH && ledState2 == HIGH && ledState3 == HIGH)
{
 unsigned long currentMillis2 = millis();
 if (currentMillis2 previousMillis2 >=interval2s)
     { // Saves time of LED Blink
       previousMillis2 = currentMillis2;
                    
                    // LED-3 2s GREEN
                    if (ledState33 == LOW) {
             ledState33 = HIGH;
                     } else {
              ledState33 = LOW;
                     }digitalWrite(ledPin3, ledState33);
     
                    //LED-1 2s YELLOW
                    if (ledState11 == LOW) {
             ledState11 = HIGH;
                     } else {
               ledState11 = LOW;
      }digitalWrite(ledPin1, ledState11);

     }
                     
// record timer 4s
 unsigned long currentMillis4 = millis();
 if (currentMillis4 previousMillis4 >=interval4s)
     { // Saves time of LED Blink
       previousMillis4 = currentMillis4;
                    
                    // LED-2 4s RED
                    if (ledState22 == LOW) {
             ledState22 = HIGH;
                     } else {
                ledState22 = LOW;
                     }digitalWrite(ledPin2, ledState22);
     }}} //Thats a Wrap! */

1 Comment

  1. Delph Riddler

    I like the comments u placed in the code, it helped me copy and try it for myself. thanks.

    DR

    Reply

Leave a Comment

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