I'm trying to control LED blinking within the range of the sensor values.

Hello! :slight_smile:
With the help of many people, I was able to get this far.

The slope was obtained through the 9-axis sensor.
And when I tilted the board to the left or right, I made a code to light each LED for a momentarily.
And I want to replace LED on with LED blinking in this code.
When the board tilts, the LED blinks for a certain period of time.

The code below is for LED ON/OFF.

    if(val_x < -30 && State == 0) // right on
    {
      State = 1; 
      previousMillis = millis();
      Serial.println("Right LED ON");
      digitalWrite(right_led, HIGH);
      digitalWrite(left_led, LOW);
    }

   /* else if(val_x > 30 && State == 0) // left on(blinking)
    {
      State = 1;
      unsigned long currentMillis_blink = millis();      
      if(currentMillis_blink - previousMillis_blink  > 500)
      {
        previousMillis_blink = currentMillis_blink;
        Serial.println("Left LED ON");
        if (led_state_2 == LOW)  
          led_state_2 = HIGH;   
        else 
          led_state_2 = LOW; 
    
        digitalWrite(left_led, led_state_2);  
      }
    }*/

    else
    {
      State = 0;
    }

    if( millis() >= previousMillis + 5000 && State == 0) //LED off after 5 seconds
    {
      digitalWrite(left_led, LOW);
      digitalWrite(right_led, LOW);
      Serial.println("LED OFF");
    }

I tried to apply millis(), but the code will only become more strange.

I’d really appreciate your help.
Thank you in advance!

You need to look up state machines. You can have 0/1/2. 0 = off ,1 = on, 2 = blinking. write a function for blinking which should be relatively straight forward and if it is going to be active only for a short time then you change back to state 0.

I wrote the code like this, but it doesn’t work properly.
Can I know why?

    if(val_x < -30 && State == 0)
    {
      State = 1;
      unsigned long currentMillis_blink = millis();
      if(currentMillis_blink - previousMillis_blink  > 500)
      {
        previousMillis_blink = currentMillis_blink;
        digitalWrite(right_led, led_state_2);
        digitalWrite(left_led, LOW);
        led_state_2 = !led_state_2;
      }
    }

    else if(val_x > 30 && State == 0) // x축 기울기 left on
    {
      State = 1;
      unsigned long currentMillis_blink = millis();
      if(currentMillis_blink - previousMillis_blink  > 500)
      {
        previousMillis_blink = currentMillis_blink;
        digitalWrite(left_led, led_state_2);
        digitalWrite(right_led, LOW);
        led_state_2 = !led_state_2;
      }
    }

    else
    {
      State = 0;
    }

    if( millis() >= previousMillis + 9000 && State == 0)//9초 후 LED OFF
    {
      digitalWrite(left_led, LOW);
      digitalWrite(right_led, LOW);
      Serial.println("LED OFF");
    }

Hello! :slight_smile:
I’m using nano33ble board and using IMU, a 9-axis sensor.

I got the slope value from this IMU sensor.
And when I tilted the board to the left or right, I made a code to light each LED for a momentarily.
And I want to replace LED on with LED blinking in this code.
When the board tilts, the LED blinks for a certain period of time.

    if(val_x < -30 && State == 0) // right on
    {
      State = 1;
      previousMillis = millis();
      Serial.println("Right LED ON");
      digitalWrite(right_led, HIGH);
      digitalWrite(left_led, LOW);
    }

   /* else if(val_x > 30 && State == 0) // left on(blinking)
    {
      State = 1;
      unsigned long currentMillis_blink = millis();     
      if(currentMillis_blink - previousMillis_blink  > 500)
      {
        previousMillis_blink = currentMillis_blink;
        Serial.println("Left LED ON");
        if (led_state_2 == LOW) 
          led_state_2 = HIGH;   
        else
          led_state_2 = LOW;
   
        digitalWrite(left_led, led_state_2); 
      }
    }*/

    else
    {
      State = 0;
    }

    if( millis() >= previousMillis + 5000 && State == 0) //LED off after 5 seconds
    {
      digitalWrite(left_led, LOW);
      digitalWrite(right_led, LOW);
      Serial.println("LED OFF");
    }

I tried to apply millis(), but the code will only become more strange.

I’d really appreciate your help.
Thank you in advance!

Create a function that causes the LED to blink as you require.

Then use code something like this to call that function until the time expires

if (millis() - startTime <= duration) {
   myBlinkFunction();
}

There is example code for blinking an LED in Several Things at a Time

…R

@yscheol

TOPIC MERGED.

Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

When the board tilts, the LED blinks for a certain period of time.

You want the LED to blink faster? Or blink the same speed just for longer/shorter amount of time?

-jim lee

ballscrewbob:
@yscheol

TOPIC MERGED.

Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

I've confirmed. I'll be careful from now on.

jimLee:
You want the LED to blink faster? Or blink the same speed just for longer/shorter amount of time?

-jim lee

For example, if the board tilts to the left, the left LED (LED1) should flash at 0.5 second intervals for 5 seconds. After 5 seconds, the LED turns off. If tilted in the opposite direction (right) before 5 seconds have passed, the left LED will immediately turn off and the right LED will operate.

So, you'd probably like an object that blinks an LED 'till it hits 5 seconds (timeout) or's turned off. And doesn't block, Right?

Hmm.. constructor might be..

timeoutBlinker leftBlinker(leftPinNum,timeoutMs,blinkOnMs,blinkPeriodMs);
timeoutBlinker rightBlinker(leftPinNum,timeoutMs,blinkOnMs,blinkPeriodMs);

And each would have an on/off call..

leftBlinker.onOff(bool onOff);
rightBlinker.onOff(bool onOff);

if (leaningRight) {
rightBlinker.onOff(true);
leftBlinker.onOff(false);
else if (leaningLeft) {
rightBlinker.onOff(false);
leftBlinker.onOff(true);
}

And that would about do it, right? If you had a timeoutBlinker class..

-jim lee

jimLee:
If you had a timeoutBlinker class..

Hello, Jim Lee. Thank you for your reply.

Could you tell me what TimeoutBlinker class is?

Doesn't exists yet. But I'm thinking it would be pretty easy to write. Are you up for loading a library form the library manager?

-jim lee

Here you go..

class timeoutBlinker :  public blinker,
                        public timeObj {
   public:
                  timeoutBlinker(int pin,float timeOutMs,float blinkOnMs,float periodMs);
         virtual  ~timeoutBlinker(void);

         virtual  void  setOnOff(bool onOff);  
         virtual  void  idle(void);        
};


timeoutBlinker::timeoutBlinker(int pin,float timeOutMs,float blinkOnMs,float periodMs)
   : blinker(pin,blinkOnMs,periodMs),
   timeObj(timeOutMs) {  }

   
timeoutBlinker::~timeoutBlinker(void) {  }

void timeoutBlinker::setOnOff(bool onOff) {

   if (onOff) start();
   blinker::setOnOff(onOff);
}

void  timeoutBlinker::idle(void) {

   if (blinking()) {
      if (ding()) {
         setOnOff(false);
      }
   }
   blinker::idle();    
}

Above is your class code.

Your part in this is to create the two global blinkers left and right like above.

And in loop() add the call.

idle(); // This runs everything in the background

You will need LC_baseTools from the library manager to compile what I gave you.

So, like above, after computing our your lean, right or left..

if (beginLeaningRight) {
rightBlinker. setOnOff(true);
leftBlinker. setOnOff(false);
else if (beginLeaningLeft) {
rightBlinker. setOnOff(false);
leftBlinker. setOnOff(true);
}

Now, you call this when you BEGIN to lean. You will need to NOT call it over-and-over while leaning, or it'll run-and-run. Call it once, note that it was called, then call it when you lean the other way.

Oh NEVER call delay() or the this won't work.. (And the world will end.)

This seem doable?

-jim lee

Thank you very much for the details!
I will refer to it and revise it.

Well, don't mess with the class. It's been tested and it works.

-jim lee