.

Welcome to My HO Model Railroad Blog

January 16. 2019 Arduino Multasking

It took me quite awhile to come up with an Arduino Sketch that would flash multiple bulbs for my layout.  My goal was to flash my emergency vehicle revolving beacons on my layout so that they are not in sync, randomly flashing but all at the same rate of about 60 flashes per minute.  I finally conquered the Arduino multitasking by using millis instead of delay.

The Arduino is a one at a time processor, when it sees a delay all functions stop for the delay period.  The answer is to use milliseconds for delays, that way the program doesn't stop running essentially multitasking.

The video below is my Arduino test UNO with a Mel test expansion module that has three TD62304AP seven channel high current drivers so that the 20 Arduino low current outputs can drive 12 volt Grain of Wheat bulbs.

The expansion module has standard Arduino male connectors and I made a matching female plug with 20 GOW bulbs for easy testing the Arduino outputs.


Each bulb flashes at about 60 times per minute totally out of sync.

The Arduino Sketch (Program) is configured for a Nano so I used D3 to D9 for the outputs.  I'm going to be using a Nano to drive my emergency vehicle lighting.

This is a Nano with the Mel Expansion Module and the seven channel TD62304AP driver IC.


As normal I went with my own way of adding the Arduino connectors to the boards.  The UNOs use the Arduino female connector on the board and the Arduino way is to mount the male connector on the Nano.  I went with the female to match the UNOs.

I cleaned up my sketch, it should copy and paste to the Arduino IDE.  The sketch will drive LEDs as is.  I'll post my drawing of the high current TD62304AP wiring later for those that need from 6 to 24 volts at up to 500ma per output.




// This is a working seven channel nonsynchronous emergency flasher
// These variables store the flash pattern
// and the current state of the LED


int ledPin1 =  3;      // the number of the LED pin
int ledState1 = LOW;             // ledState used to set the LED
unsigned long previousMillis1 = 0;        // will store last time LED was updated
long OnTime1 = 300;           // milliseconds of on-time
long OffTime1 = 400;          // milliseconds of off-time


int ledPin2 =  4;      // the number of the LED pin
int ledState2 = LOW;             // ledState used to set the LED
unsigned long previousMillis2 = 0;        // will store last time LED was updated
long OnTime2 = 310;           // milliseconds of on-time
long OffTime2 = 410;          // milliseconds of off-time


int ledPin3 =  5;      // the number of the LED pin
int ledState3 = LOW;             // ledState used to set the LED
unsigned long previousMillis3 = 0;        // will store last time LED was updated
long OnTime3 = 315;           // milliseconds of on-time
long OffTime3 = 380;          // milliseconds of off-time


int ledPin4 =  6;      // the number of the LED pin
int ledState4 = LOW;             // ledState used to set the LED
unsigned long previousMillis4 = 0;        // will store last time LED was updated
long OnTime4 = 335;           // milliseconds of on-time
long OffTime4 = 390;          // milliseconds of off-time


int ledPin5 =  7;      // the number of the LED pin
int ledState5 = LOW;             // ledState used to set the LED
unsigned long previousMillis5 = 0;        // will store last time LED was updated
long OnTime5 = 305;           // milliseconds of on-time
long OffTime5 = 395;          // milliseconds of off-time


int ledPin6 =  8;      // the number of the LED pin
int ledState6 = LOW;             // ledState used to set the LED
unsigned long previousMillis6 = 0;        // will store last time LED was updated
long OnTime6 = 325;           // milliseconds of on-time
long OffTime6 = 405;          // milliseconds of off-time


int ledPin7 =  9;      // the number of the LED pin
int ledState7 = LOW;             // ledState used to set the LED
unsigned long previousMillis7 = 0;        // will store last time LED was updated
long OnTime7 = 325;           // milliseconds of on-time
long OffTime7 = 405;          // milliseconds of off-time


void setup()
{
  // set the digital pin as output:
  pinMode(ledPin1, OUTPUT);     
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);     
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
}


void loop()
{
  // check to see if it's time to change the state of the LED
  unsigned long currentMillis = millis();

  if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    ledState1 = LOW;  // Turn it off
    previousMillis1 = currentMillis;  // Remember the time
    digitalWrite(ledPin1, ledState1);  // Update the actual LED
  }
  else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
  {
    ledState1 = HIGH;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    digitalWrite(ledPin1, ledState1);    // Update the actual LED
  }
    if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
  {
    ledState2 = LOW;  // Turn it off
    previousMillis2 = currentMillis;  // Remember the time
    digitalWrite(ledPin2, ledState2);  // Update the actual LED
  }
  else if ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
  {
    ledState2 = HIGH;  // turn it on
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(ledPin2, ledState2);   // Update the actual LED
  }
    if((ledState3 == HIGH) && (currentMillis - previousMillis3 >= OnTime3))
  {
    ledState3 = LOW;  // Turn it off
    previousMillis3 = currentMillis;  // Remember the time
    digitalWrite(ledPin3, ledState3);  // Update the actual LED
  }
  else if ((ledState3 == LOW) && (currentMillis - previousMillis3 >= OffTime3))
  {
    ledState3 = HIGH;  // turn it on
    previousMillis3 = currentMillis;   // Remember the time
    digitalWrite(ledPin3, ledState3);   // Update the actual LED
  }
   if((ledState4 == HIGH) && (currentMillis - previousMillis4 >= OnTime4))
  {
    ledState4 = LOW;  // Turn it off
    previousMillis4 = currentMillis;  // Remember the time
    digitalWrite(ledPin4, ledState4);  // Update the actual LED
  }
  else if ((ledState4 == LOW) && (currentMillis - previousMillis4 >= OffTime4))
  {
    ledState4 = HIGH;  // turn it on
    previousMillis4 = currentMillis;   // Remember the time
    digitalWrite(ledPin4, ledState4);   // Update the actual LED
   }
    if((ledState5 == HIGH) && (currentMillis - previousMillis5 >= OnTime5))
  {
    ledState5 = LOW;  // Turn it off
    previousMillis5 = currentMillis;  // Remember the time
    digitalWrite(ledPin5, ledState5);  // Update the actual LED
  }
  else if ((ledState5 == LOW) && (currentMillis - previousMillis5 >= OffTime5))
  {    ledState5 = HIGH;  // turn it on
    previousMillis5 = currentMillis;   // Remember the time
    digitalWrite(ledPin5, ledState5);   // Update the actual LED
   }
  if((ledState6 == HIGH) && (currentMillis - previousMillis6 >= OnTime6))
  {
    ledState6 = LOW;  // Turn it off
    previousMillis6 = currentMillis;  // Remember the time
    digitalWrite(ledPin6, ledState6);  // Update the actual LED
  }
  else if ((ledState6 == LOW) && (currentMillis - previousMillis6 >= OffTime6))
  {    ledState6 = HIGH;  // turn it on
    previousMillis6 = currentMillis;   // Remember the time
    digitalWrite(ledPin6, ledState6);   // Update the actual LED
  }
  if((ledState7 == HIGH) && (currentMillis - previousMillis7 >= OnTime7))
  {
    ledState7 = LOW;  // Turn it off
    previousMillis7 = currentMillis;  // Remember the time
    digitalWrite(ledPin7, ledState7);  // Update the actual LED
  }
  else if ((ledState7 == LOW) && (currentMillis - previousMillis7 >= OffTime7))
  {    ledState7 = HIGH;  // turn it on
    previousMillis7 = currentMillis;   // Remember the time
    digitalWrite(ledPin7, ledState7);   // Update the actual LED
  }
}



 


 

   

No comments: