| View previous topic :: View next topic |
| Author |
Message |
EDC
Joined: 26 Mar 2014 Posts: 1169

|
|
| Back to top |
|
 |
JC
Joined: 15 Dec 2007 Posts: 637 Location: Cleveland, OH

|
Posted: Wed May 14, 2025 6:45 pm Post subject: |
|
|
Hi EDC,
I agree, a short delay is needed.
My copy of the data sheet shows that a delay of >= 50 uSec is required between the new LED update commands.
This is, I think, so that the LED can recognize the start of a new command.
My simple test program below shows this.
With a delay of Waitus 50 it works fine, toggling between Red and Blue.
With a delay of 30 uSec, it fails.
In the past I spent HOURS (or was it days...), looking for a bug in a couple programs, wondering why the LEDs were not updating correctly, before I learned that the delay was required.
In many cases, one's program will have a delay for processing other code between the updates.
But a loop rapidly updating the LEDs will fail without a short (50 uSec) delay.
I have a short test program, below, to demonstrate this.
This test / demo uses only one string of WS28B12 (3-color) digital LEDs.
This program does NOT use Interrupts (which could, if used, interfere with the output string timing).
I understand that the Waitus X command is an approximation, I have not O'scope the actual delay time.
This program sets the strip of 5 LEDs to all Blue.
It then sets the strip of all 5 LEDs to all Red.
It alternates back and forth in a loop.
The human eye will see a blended color, (off white), not the actual Blue then Red then Blue, etc., because the colors change faster than the human eye can perceive them.
If they are changing, however, one will see an off-white color.
If the command is failing, one sees that the colors are all Blue, and the strip is not updating.
With delay of 50 uSec the LEDs update correctly.
With a delay of 30 uSec, the update fails.
Perhaps Mark could add a comment in the Config Rainbow command's Help with the 2026 update as a reminder to everyone.
Take Care,
JC
| Code: |
'File: WS2812B Test V3.bas
'May 14, 2025 BasCom 2.0.8.6
'Simple test program for a string of 5 WS2812B Digital LEDs.
'Nano, 16 MHz, 5V (Arduino Nano)
'Testing sequential commands, with and without a delay between them.
'Hardware:
'WS28B12 LEDs, String of 5:
'PortD.7 Dig LEDs
'With a Waitus 50 (Approx 50 uSec) delay between the commands to
'update / change the LEDs colors, RB_Fill command works.
'Without a short delay, it fails to update the LEDs.
'Can just change the 50 to 30 to see it fail, don't have to REM the Waitus line.
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
$regfile = "m328pdef.dat" 'Micro in use
$crystal = 16000000 'Micro's Clock
$hwstack = 64 'Hardware Stack
$swstack = 64 'Software Stack
$framesize = 64 'Frame Space
$LIB "RainbowBSCN.lib" 'Newer Library.
'Hardware Aliases:
DigLED Alias PortD.7 'WS28B12 LED String
'Now Configure the Port's Pins for Input or Output mode.
Config Portd.7 = Output
'Dig LED Variables:
Dim Color(3) as Byte 'Color array for digital Leds
'Hardware Startup Delay:
Waitms 100
'Config the digital LEDs, Strip of 5, on PortD.7 (1 Channel, Mode 3)
'Config parameters: Channels, 3/4 Color LEDs, String Length, Port, Pin
'The Channel Setup uses Channel = 1, as their is one channel, called Channel 0 when used.
'Channel is Base 0, so the Command uses Channel 0
'LED Number in string is Base 0. (i.e. 0 - 4 for 5 LEDs)
Config Rainbow = 1 , RGB = 3 , RB0_LEN = 5 , RB0_Port = PortD , RB0_Pin = 7
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Main:
RB_SelectChannel 0 'Specify the active channel, (The only channel in this cae)
Gosub DigTwoColors 'Alternate between two colors for full strip of LEDs
End
DigTwoColors:
'Set the entire strip to one of two colors, alternate back and forth
'A delay of 50 uSec Works. Eye sees a mix of Red and Blue colors.
'A delay of 30 uSec Fails The LEDs don't change colors.
Do
Color(1) = 0 'Green
Color(2) = 0 'Red
Color(3) = 255 'Blue
RB_Fill color()
Waitus 50 'Short Delay is REQUIRED
Color(1) = 0 'Green
Color(2) = 255 'Red
Color(3) = 0 'Blue
RB_Fill color()
Waitus 50 'Short Delay is REQUIRED
Loop
|
|
|
| Back to top |
|
 |
Robert77
Joined: 29 Sep 2016 Posts: 9

|
Posted: Fri May 16, 2025 10:16 am Post subject: |
|
|
| JC wrote: |
that the LED can recognize the start of a new command.
|
To be precise: The delay is neccessary to recognize the END of the current transmission.
After this delay the LEDs will update their colors.  |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6270 Location: Holland

|
Posted: Sat May 17, 2025 1:35 pm Post subject: |
|
|
did not knew this. always worked in my tests. but they were only basic.
i wonder what the best solution is. add it to the lib so it will wait 50 us? or that the user add it.
the downside of the user adding it is that it uses more code. but... a lot of other code can run in those 50 us.
an option would be to use a timer that would wait if needed. but i am not a fan of using hardware that can be used otherwise or by the user. _________________ Mark |
|
| Back to top |
|
 |
JC
Joined: 15 Dec 2007 Posts: 637 Location: Cleveland, OH

|
Posted: Sat May 17, 2025 6:24 pm Post subject: |
|
|
I've included a part of the data sheet below.
It shows the >= 50 uSec "Reset" delay that is required between sequential transmissions to the LEDs.
The data value is also listed in a table of timing information.
But until one actually works with the LEDs, it is very easy to overlook this!
Although the information is in the data sheet, the data sheet doesn't make a big point about it.
Most of the time one sets the LEDs and moves on with other code.
So the timing requirement is not an issue.
I had a Line-Following-Robot that had 8 IR Sensors to watch for the line on the track.
I had 8 LEDs mounted across the back of the Bot.
Each IR sensor's data was shown on its own LED.
All of the LEDs were Blue, except the ones that detected the line, they were Red.
In the photo, the IR sensors are hidden by the front of the PCB, and one can see the black line, (on a white floor), extending out the in front of the Bot.
As the Bot wandered off the line, (and had to correct its steering), the Red dot would move back and forth along the Blue line.
One could see the IR data in real time as the Bot traveled down the course, and watch it make corrections to the steering.
The way the code was structured, I was often trying to update one or two LEDs faster than allowed, (no 50 uSec delay), and it failed.
It took me forever to figure out the problem!
Now, back to on-topic:
I would suggest that you simply put a comment in the Rainbow Help.
Perhaps in several places, the Config, and every command that writes to the string.
I would prefer NOT to have a blocking delay in the library.
That time, as you already mentioned, can be used for running other code.
I would prefer that it NOT use any HW Timer/Counter.
I don't want to have to design my projects around the HW that the compiler is using.
JC
 |
|
| Back to top |
|
 |
Robert77
Joined: 29 Sep 2016 Posts: 9

|
Posted: Sun May 18, 2025 9:12 am Post subject: |
|
|
I agree with JC: Hints in the help are sufficient.
Please don't add the delay to lib and don't attach a HW-Timer to it.
I often have time-critical code or run out of timers, so changing the lib would cause more problems than benefits.  |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6270 Location: Holland

|
Posted: Tue Oct 21, 2025 9:26 am Post subject: |
|
|
I was about to update the help. But i see something odd.
The help says :
"The entire memory of the active channel is filled with the specified color.
This statement will not update the LED's. This means that you need to use RB_SEND to update the LED's. Or use RB_FILL which will update the LED's as well."
In the example the RBSEND is missing.
When i check the lib.
[RB_FILL]
;DESCRIPTION Set SRAM and Stripe
;PARAMETER RGB(R16..1
;USE X
$external RB_STRIPE
RB_FILLColors:
CLT
RJMP RB_Fill0
RB_FILL:
SET
RB_Fill0:
;RCALL Parameter3
LD R16 , X+ ; R
LD R17 , X+ ; G
LD R18 , X ; B
#IF _RB_RGBW=4
LD R19 , X ; W
#ENDIF
RB_FILL1:
RCALL RB_LoadStripe
RB_FILL2:
ST X+, R16 ;store red
ST X+, R17 ;store green
ST X+, R18 ;store blue
#IF _RB_RGBW=4
St X+, R19 ; white
#ENDIF
* SBIW R24,_RB_RGBW ; 3 or 4 done
BRNE RB_FILL2
BRTC +2
Rcall RB_Send
Ret
[END]
When RB_FILLColors is called it will use CLT to clear the T flag in SREG
And this code :
BRTC +2
Rcall RB_Send
will skip the call to rb_send when T is cleared.
So i wonder how the stripe can even show something?
RB_Fill does not set T and will update using RB_Send.
So was this actually tested with the sample above?
Can it be re-tested?
And when it updates the rgb, can you remark the line with Rcall RB_Send by placing a ; before it?
Then save, recompile, prog and test.
If the code is right it will show the same behavior with remark on without remark.
If the code is not right it will not update.
The routine works on the memory only so i do not see how it can update the rgb. _________________ Mark |
|
| Back to top |
|
 |
JC
Joined: 15 Dec 2007 Posts: 637 Location: Cleveland, OH

|
Posted: Thu Oct 23, 2025 12:09 am Post subject: |
|
|
Hi Mark,
I apologize; I have not (yet) tested the SNAP with Ver 2.0.8.7 from within Bascom.
The test below is using Ver 2.0.8.6.
I am using the SNAP programmer and the MPLab IPE to program the Arduino Nano, (M328P).
(My AVR Studio quit working with the SNAP after an update a year or two ago.)
I think you are asking whether the RB_Fill command will actually update a string of WS2812B LEDs.
Using Ver 2.0.8.6 and the RainbowBSCN.lib library, this DOES update the actual LEDs.
One does NOT need to use the RB_Send command after the RB_Fill command.
Note that the WS2812B Digial LEDs do require the 50 uS delay between sending new commands out the I/O pin
to the LED string.
The program below was tested on real hardware, today, and works correctly.
I prefer to put the delay in MY code, so I don't need it at all if I am executing other instructions between the
Digital LED update commands.
Take care,
Jay
| Code: |
'File: WS2812B Test V5.bas
'BasCom 2.0.8.6
'Testing RB_Fill Command
'Hardware:
'Arduino Nano
'Mega 328P, 16 MHz, 5V
'String of 5 WS2812B Digital LEDs.
'PortB.5 Nano's LED, (Normal LED)
'PortD.7 WS2812B Digital LEDs, String of 5
'*****
'This short program shows that the Bascom RainbowBSCN.lib's RB_Fill color()
'command WILL output the memory's data to the digital LED string.
'the RB_Send command is NOT needed.
'NOTE: The 50 uSec selay after sending data string to the Digital LED string
'IS required for the WS2812B LEDs to recognize the end of th elast command
'and hence to recognize the start of a new command.
'*****
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
$regfile = "m328pdef.dat" 'Micro in use
$crystal = 16000000 'Micro's Clock
$hwstack = 64 'Hardware Stack
$swstack = 64 'Software Stack
$framesize = 64 'Frame Space
$LIB "RainbowBSCN.lib" 'Newer Library.
'Hardware Aliases:
Led1 Alias PortB.5 'Nano's LED
DigLED Alias PortD.7 'WS2812B LED String
'Now Configure the Port's Pins for Input or Output mode.
Config PortB.5 = Output
Config Portd.7 = Output
'Variables:
Dim LpCnt as Byte 'Loop Counter
Dim Color(3) as Byte 'Color array for digital leds
'Config the digital LEDs, Strip of 5 LEDs, on PortD.7 (1 Channel, Mode 3)
'Parameters: Num of Channels, 3/4 Color LEDs, LED String Length, Port, Pin
'The Channel Setup uses Channel = 1, as their is one channel, called Channel 0 when used.
'Channel is Base 0, so the Command uses Channel 0
'LED Number in string is Base 0. (i.e. 0 - 4 for 5 LEDs)
Config Rainbow = 1 , RGB = 3 , RB0_LEN = 5 , RB0_Port = PortD , RB0_Pin = 7
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'Hardware Startup Delay:
Waitms 100
Main:
Gosub FlashLED 'Flash Normal LED on Start-Up
RB_SelectChannel 0 'Specify the active channel
Do
'Set the entire strip to one of three colors.
'Cycle through the three colors, slowly to see them,
'or rapidly to see the color White.
'All three colors blended by the human eye.
'With the Waitms 500 delays, one can see the LEDs are Red, then Green, and then Blue.
'Without the Waitms 500 delays, the LEDs change color very fast, and
'the LED strip appears to be White.
'This does show that they are updating quickly.
'The Waitus 50 delay after writing to the Digital LEDs IS required.
'******************
'This shows that RB_Fill color() DOES update the string of LEDs
'RB_Send is NOT required.
'******************
'Set LED Strip to Red:
Color(1) = 0 'Green
Color(2) = 32 'Red
Color(3) = 0 'Blue
RB_Fill color()
Waitus 50
'Waitms 500
'Set LED Strip to Green:
Color(1) = 32 'Green
Color(2) = 0 'Red
Color(3) = 0 'Blue
RB_Fill color()
Waitus 50
'Waitms 500
'Set LED Strip to Blue:
Color(1) = 0 'Green
Color(2) = 0 'Red
Color(3) = 32 'Blue
RB_Fill color()
Waitus 50
'Waitms 500
Loop
End
DigColorTest:
'Set one LED to one color, all others off.
'Example only, not used above.
'First turn Off all Dig LEDs
RB_ClearStripe
Waitus 50
'Now turn On one single Dig LED
'Make LED #1, (The 2nd one in the string), Blue.
Color(1) = 0 'Green
Color(2) = 0 'Red
Color(3) = 255 'Blue
RB_SetColor 1, Color()
RB_Send 'Update the LED display
Return
FlashLED:
'Flash the "Normal" LED on Power up
For LpCnt = 1 to 5
Set Led1
Waitms 100
ReSet Led1
Waitms 100
Next LpCnt
Return
|
|
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6270 Location: Holland

|
Posted: Fri Oct 24, 2025 8:56 pm Post subject: |
|
|
Thanks for the answer.
I does not matter if you use 2086 or 2087.
Can you edit the lib section [RB_FILL] and remark the line :
Rcall RB_Send
by placing a ; before it ?
;Rcall RB_Send
then save and recompile/reprogram.
it would be odd if it would update. _________________ Mark |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6270 Location: Holland

|
Posted: Fri Oct 24, 2025 9:02 pm Post subject: |
|
|
ignore that. i see what is wrong. i think the wrong entry point is called so it will always update.
i need to look into it. but the cause is clear now. _________________ Mark |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6270 Location: Holland

|
Posted: Sat Oct 25, 2025 2:16 pm Post subject: |
|
|
I need better glasses. I thought that rb_fillColors was used but rb_fill is used which will indeed update the RGB. _________________ Mark |
|
| Back to top |
|
 |
JC
Joined: 15 Dec 2007 Posts: 637 Location: Cleveland, OH

|
Posted: Sat Oct 25, 2025 5:42 pm Post subject: |
|
|
It is a good feeling, however, when you track down a "bug" and find the actual problem and solution.
This is true even if the "bug" is in our thinking, and not in the code itself!
I did verify, on real hardware, that:
RB_Fill array
Will update the memory and write the data out to the LEDs.
RB_FillColors array
Will update the memory, but NOT write the data out to the LEDs
RB_FillColors array
RB_Send
Will update the memory, and then write the data out to the LEDs.
When the name of the array is color() it can easily be mis-read by your brain!
If you are updating the Help for the Rainbow instructions, then perhaps it would be reasonable to add a paragraph that states that there needs to be a >= 50 uSec delay between successive writes to the WS2812B LED. This is to allow it to correctly recognize the end of one instruction and the start of the next instruction. This delay can be inserted with Waitus 50 if one has several instructions in a row that write to an LED (or LED string). The Waitus50 is not needed if other instructions are being performed that take >= 50 uSec to be executed.
Thank you, again, for your continued work on Bascom!
Take care,
Jay |
|
| Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6270 Location: Holland

|
Posted: Mon Oct 27, 2025 11:54 am Post subject: |
|
|
Yes solving puzzles gives a good feeling.
I misread the color() variable. So all is working as expected.
I have added to the help:
A small delay of 50 uS is required between updates/writing new data with successive writes. The ws2811/2812 needs a small delay so it can recognize the end of data. Other RGB chips might not require it. Just check the specs. _________________ Mark |
|
| Back to top |
|
 |
EDC
Joined: 26 Mar 2014 Posts: 1169

|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|