Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Rainbowleds delay

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1130

poland.gif
PostPosted: Wed May 14, 2025 12:33 pm    Post subject: Rainbowleds delay Reply with quote

Today Im working on some project where Rainbowleds are used.
I have two Rainbow_sripes in this project. One of them is ring:



After Rb_clearcolors and Rb_send I cant light first or whatever LED on the strip again without a delay.
So I must add special Waitms 1 and then everything working fine.

Maybe this will shorten develop software for someone Very Happy I dont test how exactly I must wait because here it is not strictly time-dependent application.

Code:
Sub Show_stby()
  Debug "System going sleep"
  Rb_selectchannel 1                                        'select ring
  Rb_clearcolors : Rb_send
   Waitms 1                                                 'without it LED will not be enabled in the next command
 Debug "Turning on one led only, color blue"
  Rb_setcolor 0 , Color5(1) : Rb_send                       '5=blue  RING
End Sub


(BASCOM-AVR version : 2.0.8.7 )

_________________
Check B-Flash -my MCS bootloader app for Android (updated)
Back to top
View user's profile Visit poster's website
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 626
Location: Cleveland, OH

usa.gif
PostPosted: Wed May 14, 2025 6:45 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
Robert77

Bascom Member



Joined: 29 Sep 2016
Posts: 9

germany.gif
PostPosted: Fri May 16, 2025 10:16 am    Post subject: Reply with quote

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. Wink
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6180
Location: Holland

blank.gif
PostPosted: Sat May 17, 2025 1:35 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 626
Location: Cleveland, OH

usa.gif
PostPosted: Sat May 17, 2025 6:24 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
Robert77

Bascom Member



Joined: 29 Sep 2016
Posts: 9

germany.gif
PostPosted: Sun May 18, 2025 9:12 am    Post subject: Reply with quote

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. Confused
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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