Hi everyone! I'm after some help with my WS2811 lights.

Hi everyone!

I’m after some help with my WS2811 lights. I’m fairly new to arduino, and I’m trying to write a sketch using an ultrasonic sensor, servo motor and a string of leds, where approaching closer to the object the sensor is mounted on causes the lights to change colour / do different things. My current plan is to have the distance sensor mounted on a servo which is rotating and acting as a sort of radar, so that a wide field of angles of approach towards the object are covered (I could totally be going about this the wrong way so if you have other suggestions about how to do this or a more suitable sensor that would be awesome). I’ve tried a PIR sensor but it’s really either on or off and it has a weird cool-down period between each trigger which makes it unsuitable for my project.

The problem so far with using this radar method is that since the sensor will sweep past people approaching the object, using a simple if statement to trigger some lights (basically, if distance < 40, {fill_solid red}) will mean lights only stay red for the fraction of a second that the sensor hits a person. However, what I want is the distance sensor to act as though it’s continually tracking the person, even though it’s sweeping like a radar. My solution is to have the lighting state that’s being triggered to run for a second or so, by which time the sensor can sweep past again and register any change in a persons position and therefore if the lighting state should stay the same or change.

In practice, I tried using a for loop so that when a person is detected, it runs a lighting state for a certain amount of loops which sort of works, but the problem is that the arduino is of course no longer receiving distance signals from the sensor while it’s running the for loop.

I’ve been trying instead to use the millis function but i’m not really getting how I would use this to achieve what I’m after, despite my best (but probably misguided) efforts. Any help or suggestions would really be appreciated!!

ps servo is run on a separate arduino to avoid interrupt issues.

here is my code using a for loop:

and here is code which i cant seem to make work using the millis function:

Are you wanting the color of the whole strip to represent the closest object? If so, seems like you need to sweep the full range/revolution, gathering a distance value every x degrees, and then update the LED display once per rotation.

Or are you wanting each pixel to represent a certain degree of sweep? If so, then you could read and update as you rotate, or read all the values for a full revolution and update the display once per rotation.

The first one pretty much - I want the colour or animation displayed to be based on the the distance to the closest object. Would it be possible to collect maybe the lowest value read on a sweep and use that value to determine what pattern/colour is displayed? And then update once per rotation? but also the servo is running off a different arduino, not sure how that would affect things. ps thanks so much for your help, super appreciated!

How about having the controller running the servo ping the LED/distance sensor controller once per revolution? That way it knows when to “reset” for a new revolution.

For each revolution use millis or EVERY_N to take a distance reading every so often. Store the value. When you take the next reading compare to the previous reading and either keep the existing value or replace it (if something closer was detected). Anytime the revolution “reset” ping is received update the pixels and reset the stored distance value.

This sounds like it would work! And i understand what you’re saying - only problem is i don’t know how exactly to implement that (as i mentioned I’m pretty new to Fastled/arduino) Here’s what i’ve got so far, but based on the serial monitor my lightingVal is stuck at 0s. I’m focusing only on getting the closest value so far and havent yet started with the servo.

thanks again for your help :slight_smile:

Two possible things to look at:
Line 59, I think you’d want to swap what’s before/after the = sign.
Line 51, try setting the time to something larger for now like 5000 milliseconds. 50 is so fast that things are constantly being reset to oldvalue (which is zero).

thanks so much, this fixed it! I had to do the same swap for line 53 as well. I’ll give integrating the servo a go - thanks again!

Hi again - made some more progress but predictably am stuck again. So I’ve successfully got my servo and distance sensor attached. I also downloaded a stats library and used it to calculate the minimum value of incoming distances. The x value is the angle that’s being written to the servo from the other arduino - at the beginning of each ‘sweep’ the minimum value is collected and sent to the leds, and at the end of a sweep the data is reset so a new minimum can be collected on the next sweep. The system is working, but I can’t get the lights I want to actually display an animation (eg the simple rainbow in my code based on the demoreel example), they can only do static lights. I think it might be a problem with using the ‘if’ statement because it only executes the code once (if i understand correctly). But if i use a while statement the animation works but code gets stuck in a loop and can no longer detect new minimum values. Any suggestions would be appreciated!

@Antony_Y Try moving line 86, int gHue; to near the top of the program with the other variables. As is, ghue gets reset to zero every time through that section.

this did the trick! (of course, very stupid error) Thanks so much for your help :slight_smile:

@marmil Hey marc, back again with this program. So everything is working fine, now the only problem is that when the sensor actually starts registering an object, the lights start to glitch. So for example, during the sweep, when the sensor sweeps over an object, the moment when an object is being detected the lights start to lag for some reason. I’ve noticed also that when the sensor hits something the TX light on the arduino will start to flicker as opposed to being lit up almost continuously, which i guess means that somehow the flow of data is being interrupted, and therefore the FastLED show command isnt being triggered? I’ve included a vid which sort of shows that when the sensor points at me (holding the camera), the animation stops. The animation in the vid is a simple beatsin8 which is controlling the r value for a fill_solid string. I can’t figure out why this is happening though. Thanks again in advance, im probably going to have to credit you by the time im done with this haha.

Code:

Video:
missing/deleted image from Google+

It might be because you’re using WS2811 pixels (3 wire chipset).

Also, have you tried using a simple fill instead of beatsin8 to make sure that isn’t somehow causing what you’re seeing?

Solid fills work fine, problems only start to happen when i have animations that are active or move.

Do you think this is where a teensy might solve the problem?

Teensy is definitely faster but I have no idea.