Arduino-Blinkenlight icon indicating copy to clipboard operation
Arduino-Blinkenlight copied to clipboard

Add a Bool to return a value to pattern blink

Open Rimbaldo opened this issue 2 years ago • 5 comments

There could be a function like this:

bool pattern(int num, SpeedSetting speed, bool repeat = false)

Then the blink code would play only once, and some action by another function could be taken as soon as the blink is over. Like this:

If I put my hand over a distance sensor, it would blink the number of millimeters that the hand is over the sensor. But then some function could only allow another "distance measure by the sensor" when the number of mms blinked is over. So the bool argument would indicate the end of the blink pattern (for a repeat = false)

Thanks!

Rimbaldo avatar Dec 15 '22 13:12 Rimbaldo

Good idea! This could also be solved by a function which returns the current state, something like an enum BLINKING, IDLE, ON, OFF...

Your example would become something like this:

int distance_mm = read_distance();
if light.state == IDLE {
    light.pattern(distance_mm);
}

Let me know what you think about this.

tfeldmann avatar Dec 22 '22 14:12 tfeldmann

Great! Will you update the library then? :)

Rimbaldo avatar Dec 27 '22 01:12 Rimbaldo

Yep, first thing in the new year ;)

tfeldmann avatar Dec 28 '22 17:12 tfeldmann

Hi! Any news?? 😊😊😊

Rimbaldo avatar Jan 05 '23 11:01 Rimbaldo

Hey, yes I found that you can already do this with the existing code:

#include <Blinkenlight.h>

Blinkenlight led(13);
int count = 1;

void setup()
{
  led.setSpeed(200);
}

void loop()
{
  if (!led.isOn()) {
    count++;
    if (count >= 5) {
      count = 1;
    }
    led.pattern(count, false);
  }
  led.update();
}

This will blink 1, 2, 3, 4, 1, 2, 3, ... I just tested this and it seems to work. The API could be clearer, though.

For your usecase you just have to replace count with your distance measurement and maybe adjust the timing to have a longer pause between the patterns.

tfeldmann avatar Jan 05 '23 12:01 tfeldmann