Several Things at once problem

Hello everyone. I have a problem that is making my head spin. I am trying to fade several LEDs at different intervals using Millis. I read a lot of tutorials and tried a lot of samples. I feel I am understanding it pretty good now for one LED but adding others is giving me difficulties. . So I got this example here, in hopes of figuring it out. Problem is I tried it and the sample doesn’t work. Tested my connections with another sketch. Copied it again in case I missed something. Does anyone know why this sketch is not working for me? Thanks for any help. I need to add it complies OK

[code]
// SeveralThingsAtTheSameTime.ino

// An expansion of the BlinkWithoutDelay concept to illustrate how a script
//  can appear to do several things at the same time

// this sketch does the following
//    it blinks the onboard LED (as in the blinkWithoutDelay sketch)
//    it blinks two external LEDs (LedA and LedB) that are connected to pins 12 and 11.
//    it turns another Led (buttonLed connected to pin 10) on or off whenever a button
//       connected to pin 7 is pressed
//    it sweeps a servo (connected to pin 5) back and forth at different speeds


//  One leg of each LED should be connected to the relevant pin and the other leg should be connected to a
//   resistor of 470 ohms or more and the other end of the resistor to the Arduino GND. 
//   If the LED doesn't light its probably connected the wrong way round.

//  On my Uno and Mega the "button" is just a piece of wire inserted into pin 7. 
//   Touching the end of the wire with a moist finger is sufficient to cause the switching action
//   Of course a proper press-on-release-off button switch could also be used!

//  The Arduino is not capable of supplying enough 5v power to operate a servo
//    The servo should have it's own power supply and the power supply Ground should
//      be connected to the Arduino Ground.

// The sketch is written to illustrate a few different programming features.
//    The use of many functions with short pieces of code. 
//       Short pieces of code are much easier to follow and debug
//    The use of variables to record the state of something (e.g. onBoardLedState) as a means to
//       enable the different functions to determine what to do.
//    The use of millis() to manage the timing of activities
//    The definition of all numbers used by the program at the top of the sketch where 
//       they can easily be found if they need to be changed


// --------CONSTANTS (won't change)---------------


const int led_A_Pin = 9;
const int led_B_Pin = 10;

const int led_A_Interval = 2000;
const int led_B_Interval = 2000;

const int blinkDuration = 5000; // number of millisecs that Led's are on - all three leds use this
const int blinkDurationB = 200;

//------------ VARIABLES (will change)---------------------

byte led_A_State = LOW;           //   LOW = off
byte led_B_State = LOW;

                           //    will be changed to negative value for movement in the other direction

unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()

unsigned long previousLed_A_Millis = 0;
unsigned long previousLed_B_Millis = 0;


//========================================

void setup() {

  Serial.begin(9600);
  Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running
  

  pinMode(led_A_Pin, OUTPUT);
  pinMode(led_B_Pin, OUTPUT);


}

//========================================

void loop() {

      // Notice that none of the action happens in loop() apart from reading millis()
      //   it just calls the functions that have the action code

  currentMillis = millis();   // capture the latest value of millis()
                              //   this is equivalent to noting the time from a clock
                              //   use the same time for all LED flashes to keep them synchronized
  

  updateLed_A_State();
  updateLed_B_State();
 
}

//========================================



void updateLed_A_State() {

  if (led_A_State == LOW) {
    if (currentMillis - previousLed_A_Millis >= led_A_Interval) {
       led_A_State = HIGH;
       previousLed_A_Millis += led_A_Interval;
    }
  }
  else {
    if (currentMillis - previousLed_A_Millis >= blinkDuration) {
       led_A_State = LOW;
       previousLed_A_Millis += blinkDuration;
    } 
  }    
}

//========================================

void updateLed_B_State() {

  if (led_B_State == LOW) {
    if (currentMillis - previousLed_B_Millis >= led_B_Interval) {
       led_B_State = HIGH;
       previousLed_B_Millis += led_B_Interval;
    }
  }
  else {
    if (currentMillis - previousLed_B_Millis >= blinkDuration) {
       led_B_State = LOW;
       previousLed_B_Millis += blinkDuration;
    }
  }    
}

//========================================

void switchLeds() {
      // this is the code that actually switches the LEDs on and off


  digitalWrite(led_A_Pin, led_A_State);
  digitalWrite(led_B_Pin, led_B_State);

}

//========================================




//========================================END

[/code]

You never call switchLeds().

Okay, thanks but
I added switchLeds(); after updateLed_B_State();
Both LEDS stayed on and did not blink

I removed the complete section. Back to nothing works/

Never put a timing test inside a conditional statement like you have here:

void updateLed_A_State() {

  if (led_A_State == LOW) {
    if (currentMillis - previousLed_A_Millis >= led_A_Interval) {

If the condition remains in the false state, the desired time will pass without detection. It should be something like (you'll have to code it more specifically)...

void updateLed_A_State() {

  if (currentMillis - previousLed_A_Millis >= led_A_Interval) {
    if (led_A_State == LOW) {

In 99.999% of cases, millis() tests must be performed EVERY time through loop().

Now I'm good and confused. Robin2 wrote this code.

Here's a different take that uses different math for the time calculations. It gives different blink rates for three LEDs( your 'A' and 'B' and the LED on the board.)

#define LED_ON      true        //a way to track the state of each LED internally
#define LED_OFF     false

const uint8_t led_A_Pin = 9;
const uint8_t led_B_Pin = 10;
const uint8_t pinLED = LED_BUILTIN;

//create a structure to make managing the LEDs easier
typedef struct
{
    uint8_t     pin;            //which pin is the LED connected to
    bool        state;          //the state of the LED (on or off)
    uint32_t    timeLED;        //the time of the last toggle
    uint32_t    timeDelay;      //the time delay to the next toggle
    uint32_t    timeDelayOn;    //the delay value used for the 'on' time
    uint32_t    timeDelayOff;   //the delay value used for the 'off' time
    
}sLEDControl_t;

//because the structure is "typedef" it means we can use it as a variable
//in this case, an array of size two, one element for each LED
sLEDControl_t
    LEDControl[2] = 
    {
        {
            .pin = led_A_Pin,           //assign the first element to LED 'A'
            .state = LED_OFF,           //it's off now
            .timeLED = 0ul,             //intialize to zero
            .timeDelay = 0ul,           //same
            .timeDelayOn = 1000ul,      //this is how long the LED will be on in mS
            .timeDelayOff = 2000ul      //and this is how long it will be off in mS
            
        },
        {
            .pin = led_B_Pin,           //repeat for the 2nd or 'B' LED
            .state = LED_OFF,
            .timeLED = 0ul,
            .timeDelay = 0ul,
            .timeDelayOn = 20ul,       //make it blink differently for contrast
            .timeDelayOff = 500ul
            
        }
    };


void setup() 
{
    Serial.begin(9600);
    Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running

    pinMode( pinLED, OUTPUT );
    pinMode( led_A_Pin, OUTPUT );
    pinMode( led_B_Pin, OUTPUT );
    
}//setup

void loop() 
{
    UpdateLEDs(); 
    UpdateBuiltInLED();
    
}//loop

void UpdateLEDs( void )
{
    static uint8_t
        index = 0;
    uint32_t
        timeNow = millis();

    //index tracks which LED we look at each pass
    //when '0' we're doing element 0 (or LED 'A')
    //when '1' we're doing element 1 (or LED 'B')
    //check the time elapsed against the current delay value
    if( (timeNow - LEDControl[index].timeLED) >= LEDControl[index].timeDelay )
    {
        //save the time of this toggle for timing the next period
        LEDControl[index].timeLED = timeNow;
        
        //if the time has expired we need to toggle the state
        //if the LED is on now
        if( LEDControl[index].state == LED_ON )
        {
            //set it to off
            LEDControl[index].state = LED_OFF;
            //and set the next delay to the 'off' value
            LEDControl[index].timeDelay = LEDControl[index].timeDelayOff;
            
        }//if
        else
        {
            //LED is off now; turn it on
            LEDControl[index].state = LED_ON;
            //and set the next delay to the 'on' value
            LEDControl[index].timeDelay = LEDControl[index].timeDelayOn;
            
        }//else

        //write the updated state to the LED (actually turns the LED on or off)
        digitalWrite( LEDControl[index].pin, LEDControl[index].state );
        
    }//if

    //we do one LED per pass. Before we leave, change the index to the "other"
    //LED so when we come back again we look at the other LED
    index = (index + 1) & 0x01;
    
}//UpdateLEDs

void UpdateBuiltInLED( void )
{
    static bool
        state = LED_OFF;
    static uint32_t
        timeBuiltIn;
    uint32_t
        timeNow = millis();

    //uses exactly the same principle as above but without the structure
    //just acts on a single LED - the one built into the board
    //the LED toggles every 125mS
    if( (timeNow - timeBuiltIn) >= 125ul )
    {
        //every 125mS, save the time
        timeBuiltIn = timeNow;
        //toggle the LED state (here I use an exclusive-OR or XOR operator to flip the value of state
        state ^= LED_ON;
        //and write it to the LED pin
        digitalWrite( pinLED, state );
        
    }//if
    
}//UpdateBuiltInLED

You did not use the correct B variables in the B led timing section. When I add switchLeds() to loop() and correct the failure to use B variables, both leds blink as desired.

const int led_A_Pin = 9;
const int led_B_Pin = 10;

const int led_A_Interval = 2000;
const int led_B_Interval = 2000;

const int blinkDuration = 5000; // number of millisecs that Led's are on - all three leds use this
const int blinkDurationB = 200;

//------------ VARIABLES (will change)---------------------

byte led_A_State = LOW;           //   LOW = off
byte led_B_State = LOW;

                           //    will be changed to negative value for movement in the other direction

unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()

unsigned long previousLed_A_Millis = 0;
unsigned long previousLed_B_Millis = 0;


//========================================

void setup() {

  Serial.begin(9600);
  Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running
 

  pinMode(led_A_Pin, OUTPUT);
  pinMode(led_B_Pin, OUTPUT);


}

//========================================

void loop() {

      // Notice that none of the action happens in loop() apart from reading millis()
      //   it just calls the functions that have the action code

  currentMillis = millis();   // capture the latest value of millis()
                              //   this is equivalent to noting the time from a clock
                              //   use the same time for all LED flashes to keep them synchronized
 

  updateLed_A_State();
  updateLed_B_State();
  switchLeds();
 
}

//========================================



void updateLed_A_State() {

  if (led_A_State == LOW) {
    if (currentMillis - previousLed_A_Millis >= led_A_Interval) {
       led_A_State = HIGH;
       previousLed_A_Millis += led_A_Interval;
    }
  }
  else {
    if (currentMillis - previousLed_A_Millis >= blinkDuration) {
       led_A_State = LOW;
       previousLed_A_Millis += blinkDuration;
    }
  }   
}

//========================================

void updateLed_B_State() {

  if (led_B_State == LOW) {
    if (currentMillis - previousLed_B_Millis >= led_B_Interval) {
       led_B_State = HIGH;
       previousLed_B_Millis += led_B_Interval;
    }
  }
  else {
    if (currentMillis - previousLed_B_Millis >= blinkDurationB) {
       led_B_State = LOW;
       previousLed_B_Millis += blinkDurationB;
    }
  }   
}

//========================================

void switchLeds() {
      // this is the code that actually switches the LEDs on and off


  digitalWrite(led_A_Pin, led_A_State);
  digitalWrite(led_B_Pin, led_B_State);

}

//========================================

//========================================END

Blackfin:
Here's a different take that uses different math for the time calculations. It gives different blink rates for three LEDs( your 'A' and 'B' and the LED on the board.)

#define LED_ON      true        //a way to track the state of each LED internally

#define LED_OFF     false

const uint8_t led_A_Pin = 9;
const uint8_t led_B_Pin = 10;
const uint8_t pinLED = LED_BUILTIN;

//create a structure to make managing the LEDs easier
typedef struct
{
   uint8_t     pin;            //which pin is the LED connected to
   bool        state;          //the state of the LED (on or off)
   uint32_t    timeLED;        //the time of the last toggle
   uint32_t    timeDelay;      //the time delay to the next toggle
   uint32_t    timeDelayOn;    //the delay value used for the 'on' time
   uint32_t    timeDelayOff;   //the delay value used for the 'off' time
   
}sLEDControl_t;

//because the structure is "typedef" it means we can use it as a variable
//in this case, an array of size two, one element for each LED
sLEDControl_t
   LEDControl[2] =
   {
       {
           .pin = led_A_Pin,           //assign the first element to LED 'A'
           .state = LED_OFF,           //it's off now
           .timeLED = 0ul,             //intialize to zero
           .timeDelay = 0ul,           //same
           .timeDelayOn = 1000ul,      //this is how long the LED will be on in mS
           .timeDelayOff = 2000ul      //and this is how long it will be off in mS
           
       },
       {
           .pin = led_B_Pin,           //repeat for the 2nd or 'B' LED
           .state = LED_OFF,
           .timeLED = 0ul,
           .timeDelay = 0ul,
           .timeDelayOn = 20ul,       //make it blink differently for contrast
           .timeDelayOff = 500ul
           
       }
   };

void setup()
{
   Serial.begin(9600);
   Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running

pinMode( pinLED, OUTPUT );
   pinMode( led_A_Pin, OUTPUT );
   pinMode( led_B_Pin, OUTPUT );
   
}//setup

void loop()
{
   UpdateLEDs();
   UpdateBuiltInLED();
   
}//loop

void UpdateLEDs( void )
{
   static uint8_t
       index = 0;
   uint32_t
       timeNow = millis();

//index tracks which LED we look at each pass
   //when '0' we're doing element 0 (or LED 'A')
   //when '1' we're doing element 1 (or LED 'B')
   //check the time elapsed against the current delay value
   if( (timeNow - LEDControl[index].timeLED) >= LEDControl[index].timeDelay )
   {
       //save the time of this toggle for timing the next period
       LEDControl[index].timeLED = timeNow;
       
       //if the time has expired we need to toggle the state
       //if the LED is on now
       if( LEDControl[index].state == LED_ON )
       {
           //set it to off
           LEDControl[index].state = LED_OFF;
           //and set the next delay to the 'off' value
           LEDControl[index].timeDelay = LEDControl[index].timeDelayOff;
           
       }//if
       else
       {
           //LED is off now; turn it on
           LEDControl[index].state = LED_ON;
           //and set the next delay to the 'on' value
           LEDControl[index].timeDelay = LEDControl[index].timeDelayOn;
           
       }//else

//write the updated state to the LED (actually turns the LED on or off)
       digitalWrite( LEDControl[index].pin, LEDControl[index].state );
       
   }//if

//we do one LED per pass. Before we leave, change the index to the "other"
   //LED so when we come back again we look at the other LED
   index = (index + 1) & 0x01;
   
}//UpdateLEDs

void UpdateBuiltInLED( void )
{
   static bool
       state = LED_OFF;
   static uint32_t
       timeBuiltIn;
   uint32_t
       timeNow = millis();

//uses exactly the same principle as above but without the structure
   //just acts on a single LED - the one built into the board
   //the LED toggles every 125mS
   if( (timeNow - timeBuiltIn) >= 125ul )
   {
       //every 125mS, save the time
       timeBuiltIn = timeNow;
       //toggle the LED state (here I use an exclusive-OR or XOR operator to flip the value of state
       state ^= LED_ON;
       //and write it to the LED pin
       digitalWrite( pinLED, state );
       
   }//if
   
}//UpdateBuiltInLED

Thanks, This is way above my level. Gonna take a lot of study just to understand.

Polliwog:
Thanks, This is way above my level. Gonna take a lot of study just to understand.

It's really not. Just follow it the way you followed the original example.

If you look at the code that toggles the built-in LED it'll look very familar. Now look at the more complex code taking care of the two other LEDs: You'll see similarities to the built-in LED code, just different ways of addressing the various variables.

Polliwog:
Problem is I tried it and the sample doesn’t work.

The words “doesn’t work” provide no useful information from which to help you. Please describe in as much detail as possible what actually happens when you run the program and what you expect it to do that is different.

…R

Robin2:
The words "doesn't work" provide no useful information from which to help you. Please describe in as much detail as possible what actually happens when you run the program and what you expect it to do that is different.

...R

Sorry Robin. I meant the LEDs don't light at all. Please, not to worry, I will figure things out before I get too old.

Polliwog:
Sorry Robin. I meant the LEDs don't light at all. Please, not to worry, I will figure things out before I get too old.

Why don't you start with the code from my Tutorial with absolutely no changes. If that works (and it should) then start making changes.

...R