|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Help needed with algorithm
Hello.
I am finally got to seriously play with Arduino boards and want to control bank of 24 LEDs. Idea at the moment is to create a single wave of light. I thought I was on the right track, but results are not what I expected. Here is my code snippet: Code:
float in, out;
for (in = 0; in < pi * 2; in = in + pi/500)
{
for (int i = 0; i <= 23; i = i + 1)
{
if (in > pi * 2 * i / 24 )
{
out = ((cos((in - pi) - (i*(pi*2)/24))+1)/2) *4095;
tlc.setPWM(i, out);
tlc.write();
}
}
}
Any help is appreciated |
|
#2
|
|||||
|
|||||
|
Re: Help needed with algorithm
I'm not following what you're trying to do, or how the LEDs are arranged.
Are the LEDs arrayed in a line, a circle, or something else? Which LEDs do you expect to be lit at any given phase of the circle? One thing that is unusual looking is the if block. Note that the LEDs for which Code:
in <= pi * 2 * i / 24 Edit: Also, did you really mean to have 24,000 calls to the cos() function per wave? That could take a while on an arduino. Last edited by GeeTwo : 03-03-2016 at 22:59. |
|
#3
|
||||
|
||||
|
Re: Help needed with algorithm
Quote:
Code:
in <= pi * 2 * i / 24 My brain so hurts right now thinking about it, I may put it aside for a bit in favour of linear values |
|
#4
|
|||
|
|||
|
Re: Help needed with algorithm
Print out the values of i, and out and see if they are what you expect.
Make sure the value being passed to the COS function is in the right form (degrees, radians, gradian). LED's are typically on/off. Passing continuous PWM values could have unexpected results for values not near full on or full off. |
|
#5
|
|||||
|
|||||
|
Re: Help needed with algorithm
Do you have an array that is 24 LEDs tall by some other number of LEDs wide? If so, how wide? Your current algorithm seems to be directed at filling an array 24 x 1000 LEDs. If your array is shorter (or longer) than this, you will need to change the increment of the outer loop. (Change the 500 to half of your array length.) If that doesn't do it, I can't help much more without seeing the details of the libraries and possibly hardware (LED array) you're using; a link to a page about the array would probably be enough of a lead.
|
|
#6
|
|||
|
|||
|
Re: Help needed with algorithm
If you have a single long strip of 24 individually controllable LED lights (such as this), you are probably asking about a KITT light, or a Battlestar Glactica Cylon light, or (more generically) a Larson scanner, who created the effect, you can do a search for the names and get plenty of solutions. Adafruit has a nice project that explains it. Your LEDs might have different calls to address the bulbs, but the code concept is the same. This is what it sounds like you're trying to do.
If, instead, you have an array of the LEDs, to make a sine wave, that's different. If this is your first time working with LED strips, look for examples on how to use that particular type and try them out, making changes to the examples to see how the code operates. Sometimes (most of the time) if your code "doesn't work" it is just missing one important bit, or your hardware isn't wired correctly. Code examples that work eliminates a lot of errors. |
|
#7
|
||||
|
||||
|
Re: Help needed with algorithm
Quote:
Roger, your first half of your post is most helpful. I will poke at them tonight when I get home. What I have is Adafruit 24-Channel 12-bit PWM LED Driver - SPI Interface - TLC5947 with individual 24 simple LEDs connected. Sin or Cos wave is not to display a wave on LED matrix, but rather control brightness of the LEDs of a row of LEDs and that where PWM controller interface come in play. I don't have NeoPixel strip yet. I will order NeoPixel or DotStar when I get my tax refund . Perhaps it is easier for what i am trying to do. We'll see. |
|
#8
|
|||
|
|||
|
Re: Help needed with algorithm
The older LED Strips work just as well, by the by. It's mostly density and how they are programmed. The one I pointed out just happened to be the high end $. The lower end cost ones work well with the Arduino family without added boards. With 24 you can get by with just power off the Arduino.
Just be extremely careful not to feed the LEDs with more volts than specified! |
|
#9
|
||||
|
||||
|
Re: Help needed with algorithm
Quote:
http://www.ebay.com/itm/371518334189 Hopefully they are programmed same way as Adafrut strips |
|
#10
|
|||
|
|||
|
Re: Help needed with algorithm
Quote:
Searching around for ws2812b I found Sparkfun's tutorial on their own WS2812 and their Breakout board, with of course code. Again, start with their libraries and their examples, and get the feel for the method before trying your own. |
|
#11
|
|||
|
|||
|
Re: Help needed with algorithm
In regards to the code
Code:
float in, out;
for (in = 0; in < pi * 2; in = in + pi/500)
{
for (int i = 0; i <= 23; i = i + 1)
{
if (in > pi * 2 * i / 24 )
{
out = ((cos((in - pi) - (i*(pi*2)/24))+1)/2) *4095;
tlc.setPWM(i, out);
tlc.write();
}
}
}
Running it thru a spreadsheet, that if (in > pi * 2 * i / 24 ) only is true for 1 of the 24 i loops, so re-sets the LED 1/24th of the time. Good practice is setting the LED each loop, even if it is off, because the LED will keep the previous number until changed. When it does set the LED, out is in a range from zero to the low 4000s. If these LEDs are typical, the range is zero (off) to 255 (full on). Plus if they are color, you have to set each Red-Green-Blue to a level -- typically something like tlc.setPWM( i, color( outR,outG,outB ) ); Find the command in the examples; I'm just guessing what it is. |
|
#12
|
||||
|
||||
|
Re: Help needed with algorithm
Quote:
I am getting tired of TLC5947, as it continues to produce some artifacts that are not in the code. I tried 3 simple loops, 1st 100%, 2nd 50% and 3rd 0% brightness. It all works as expected but at the end of second loop all LEDs blink. |
|
#13
|
||||
|
||||
|
Re: Help needed with algorithm
I am still waiting for ws2812b LEDs, but after a bit of a break and then practicing on direct PWM pins, I finally cleaned up logic enough for it to do almost what I want. Here is what I got so far:
PHP Code:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|