two signals through chainable leds

Hi there,

I have an ultrasonic sensor and 7 chainable leds connected to my grove board.

I want to measure people walking past the ultrasonic sensor and then give a light signal through the leds, one after the other.
I succeeded making this work.

My question is how can I make it work that if the leds are still on from 1 person passing by, but another person passing by that the leds also start showing the leds lighting up for that person.
So two signals through the leds at the same time.

I now tried to do that with a loop inside the void loop, but now the ultrasonic sensor stops measuring when the leds are lightning up.

Hope you can help! Thanks in advance!

#include <SoftwareSerial.h>
#include <Wire.h>
#include <rgb_lcd.h>
#include <Ultrasonic.h>

//#include "Ultrasonic.h"

#include <ChainableLED.h>

#define LED_PIN 3 
#define NUM_LEDS 7
ChainableLED leds(2, 3, NUM_LEDS);//defines the pin used on arduino.

Ultrasonic ultrasonic(4);

int distance;
int maximumRange = 150; // Maximum range needed
int minimumRange = 10; // Minimum range needed
int color = 0;
long RangeInCentimeters;
float hue = 1.0;

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
}

// Dit is de nieuwe functie die je boven je 'void loop()' zet.
void lightItUp() {
  // Zet hier je code in die je LEDs aan zet.
  for(byte i=0;i<NUM_LEDS;i++){
         leds.setColorRGB(i, 0, 0, 255);
          delay(600);
  }
  
  // Zet hier je code in die je LEDs uit zet.
//   delay(200);
        for(byte i=0;i<NUM_LEDS;i++){
        leds.setColorRGB(i, 0, 0, 0);
          delay(10);
  }
}

// Dit is de normale 'void loop()' functie die altijd in je code staat.
void loop() {

  RangeInCentimeters = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
  Serial.print(RangeInCentimeters);
  Serial.println(" cm");
  
if (RangeInCentimeters >= maximumRange || RangeInCentimeters <= minimumRange) {
       leds.setColorRGB(1, 0, 0, 0);
       }
  
  else {
       //Dit is de functie die je aanroept. Dit is 'void lightItUp()'.
    lightItUp();
  
  }
}

Hi. Please correct your post above. Use code tags, not quotation tags.

Also please post a link to the library you are using for the leds, it is not one I have seen before. Most people use the AdaFruit Neopixel or FastLED libraries. Does it not have any ".show()" function? How does it know when all required updates have been made before it sends data to the leds?

The answer to your question is the most commonly asked question from newbies on this forum: "how do I get my Arduino to do 2 things at once?". Check this post.

Thank you!

I do not know the answer to that question...

I'm going to check the post out, thanks!

delftsestudent:
I do not know the answer to that question...

I had a quick look at the library code. Each time a single led's colour is changed, the data for the entire string is transmitted. This is very inefficient. However, your strip/string is only 7 leds, so the inefficiency does not matter. If your strip was 700 leds...

What you should learn from reading that post is that delay() is the problem. But you cannot simply remove the delay() calls from your code, it will not work without them. You need to re-write your code so that it does not need the delay() calls. Instead, it will use millis() to time the animations on the strip, and in between those times, it can perform other functions such as checking the sensor.