I created four objects, but the later three objects need to wait until the method execution of the first object is completed

I used a to create an LED object. The twinkle function exists in the object.

Twinkle has a delay function that uses millis () to judge the delayed execution without using the delay function

But I created five led objects at the same time, and the first one executed twinkle

As a result, the other four objects need to wait until the first twinkle is executed.

braked.cpp


    BrakedObj.setInit(BRAKE_LED, true);
   
    BrakedObj.twinkle(5, false);


turn.cpp

   TurnLeftDefaultObj.setInit(LEFT_TURN_LED_DEFAULT, true); 

    TurnLeftSecondObj.setInit(LEFT_TURN_LED_SECOND, true);

    TurnRightDefaultObj.setInit(RIGHT_TURN_LED_DEFAULT, true); 

    TurnRightSecondtObj.setInit(RIGHT_TURN_LED_SECOND, true);  

object.cpp 



ledobj::twinkle(int frequency, bool state)
{
    int i = 0; 
     next_duration = millis() + duration;
    openLed();

    while (i < frequency)
    {
        if (millis() > next_duration)
        {
            Serial.println("=====i=====");
            Serial.println(i);
            i++;
            next_duration = millis() + duration;
            if (ledOpen)
            {
                ledobj::closeLed();
            }
            else
            { 
                ledobj::openLed();
            }

            if (i >= frequency && !state)
            {

                closeLed();
            }
        }
    }
}


It looks like the problem is the while loop. Consider using if instead of while so that the code is not blocked in the while loop

Yes, you have created a more useful delay() function, it's still blocking the rest of your code :slight_smile:

Try something like this example : led_blink_class_example.ino - Wokwi Arduino and ESP32 Simulator

Thank you. I've solved the problem

Yes, it looks really stupid. I've solved it in a different way

For the benfit of others who may find this topic please share your solution

I canceled while, exposed a method in the object, and used the loop function to execute the exposed method. The original while was changed to if

Thanks

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.