HCWS2812 Library For Serial RGB LEDs

Useful guides, libraries, and example sketches to support our Arduino based products.
Post Reply
admin
Site Admin
Posts: 865
Joined: Sun Aug 05, 2012 4:02 pm

HCWS2812 Library For Serial RGB LEDs

Post by admin » Fri Mar 27, 2015 11:18 am

Image


Image

This Arduino library is designed to work with WS2812 based serial RGB LED's such as our 8x serial RGB LED strip (HCMODU0075) or our Flexable RGB LED strips.

Additionally this library includes the ability to create full colour message displays by arranging the LEDs into multiple columns of 8 and with used of the built-in print functions you can easily display or scroll alphanumeric text.

The library currently supports the following development boards:

ATMega328 Arduinos such as the Uno, Nano, 5V Pro Mini
ATMega25060 based Arduinos such as the Mega
ATmega32U4 based Arduinos such as the Leonardo and 5V pro mini.
ESP8266 development boards such as the Lolin D1 min & Hobby Components ESP8266 Development Board


You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/
or similarly for Linux.


Change log:

11/03/15 version 0.1: Original version
13/04/15 version 0.2: Updated timings to work with 1.x.x versions of Arduino IDE
30/11/16 version 0.3: Updated to support ESP8266
Text font moved to program memory to save ram.
14/07/23 version 1.0: Updated to allow user to specify the output pin
22/02/24 version 2.0: Implemented output code into in-line assembly for 8bit AVR
microcontrollers for better timing.
Removed option for setting output pin via library constructor
as 8bit AVRs are not fast enough to support - Uncomment
#define DOUT_PIN line HCMAX7219.h file and set pin there.


A few considerations when using this library

1) The library defaults to using Arduino pin 3 on AVR based boards and D4 on ESP8266 Lolin boards. If you need to change this open the HCWS2812.h file in a text editor (do not use Windows text pad), uncomment the #define DOUT_PIN 2 line in the user configuration section and then change the number to the pin you require.


2) Before using the library to save memory and processor cycles, you should edit the following lines in the HCWS2812.h header file found within the library folder:

#define NUMBEROFLEDS 200

3) By default this is set to 200. Change this to match the number of LED's you intend to drive. If you are a going using the print functions to drive an message display then you should make sure this number is a multiple of 8 otherwise your sketch will not work.

4) If you just want to drive a strip or LED's and don't need the print functions you can save some program memeort by commenting out the following line in the HCWS2812.h header file:

#define DOTMATRIX

Change to this:

//#define DOTMATRIX


5) Timing constraints for writing to the LEDs is very strict and so to ensure correct timing interrupts are disabled whilst the LEDs are being written to. Therefor anything relaying on interrupts will not work correctly whilst the Refresh function is executed.


Using the library

To use the library just include the HCWS2812.h header file and create an instance of the library. E.g:

Code: Select all

#include <HCWS2812.h>
HCWS2812 HCWS2812;

The library will create an array called RGBBuffer[RED/GREEN/BLUE][INTENSITY] which stores the brightness settings (0 to 255) for each red, green, and blue element of each LED. Issuing a refresh command will write the contents of this buffer to the LEDs. So for example if you wish to set the first LED in the chain to red you would do the following:

Code: Select all

RGBBuffer[RED][0] = 255; // Sets first LEDs red element to maximum brightness (255)
RGBBuffer[GREEN][0] = 0; // Sets first LEDs green element to minimum brightness (0)
RGBBuffer[BLUE][0] = 0; // Sets first LEDs blue element to minimum brightness (0)
To set the thirteenth LED to white you would do the following:

Code: Select all

RGBBuffer[RED][12] = 255; // Sets first LEDs red element to maximum brightness (255)
RGBBuffer[GREEN][12] = 255; // Sets first LEDs green element to maximum brightness (255)
RGBBuffer[BLUE][12] = 255; // Sets first LEDs blue element to maximum brightness (255)


The following functions are available with this library:

Code: Select all

HCWS2812.SetBG(R, G, B);
Sets the default background colour for the LEDs. This will be the colour the LEDs will default to when the RGBBuffer is cleared. Where:

R sets the intensity of the RED element. Valid values are between 0 (off) and 255 (maximum).
G sets the intensity of the GREEN element. Valid values are between 0 (off) and 255 (maximum).
B sets the intensity of the BLUE element. Valid values are between 0 (off) and 255 (maximum).

Code: Select all

HCWS2812.ClearBuffer(void);
Clears the contents of the RGBBuffer with the background colour set by SetBG();

Code: Select all

HCWS2812.Refresh();
Updates the LEDs by writing the contents of the RGBBuffer to them. Note that interrupts are disabled whilst this command is executed.



Additional commands for creating a message display:

Code: Select all

HCWS2812.SetFontFG(R, G, B);
Sets the foreground colour of text written to the RGB buffer via the print command. Where:

R sets the intensity of the RED element. Valid values are between 0 (off) and 255 (maximum).
G sets the intensity of the GREEN element. Valid values are between 0 (off) and 255 (maximum).
B sets the intensity of the BLUE element. Valid values are between 0 (off) and 255 (maximum).

Code: Select all

HCWS2812.SetFontBG(R, G, B);
Sets the background colour of text written to the RGBBuffer via the print command. Where:

R sets the intensity of the RED element. Valid values are between 0 (off) and 255 (maximum).
G sets the intensity of the GREEN element. Valid values are between 0 (off) and 255 (maximum).
B sets the intensity of the BLUE element. Valid values are between 0 (off) and 255 (maximum).

Code: Select all

HCWS2812.print("TEXT STRING", Offset)
Writes a string of text to the RGBBuffer at position starting at Offset. A value of 1 for offset starts the text at column 1, 2 will start the text from column 2 etc. Offset numbers beyond the maximum column number can be set to allow for text to be positioned beyond the ends of the display.

Code: Select all

HCWS2812.print(Value, Offset)
Writes a positive or negative integer to the display. If negative a '-' sign will be appended to the beginning of the number. See above of description of Offset.


HCWS2812.print(Value, Decimal_Position, Offset)
Writes a positive or negative integer to the display. If negative a '-' sign will be appended to the beginning of the number.
Decimal_Position allows the option to specify the position of a decimal point.
See above of description of Offset.




Image
  1. /* FILE:    HCWS2812_Cylon_Example
  2.    DATE:    26/03/15
  3.    VERSION: 0.1
  4.    AUTHOR:  Andrew Davies
  5.  
  6. 11/03/15 version 0.1: Original version
  7.  
  8. This is an example of how to use the HCMAX7219 library to control one or more
  9. RGB LEDS. The example will set each LED to a random colour.
  10.  
  11. To use this example connect one or more LEDs in series (Dout --> Din) and connect
  12. the first LED's Din pin to digital 2 (D4 on ESP8266 Lolin based boards) of your Arduino.
  13.  
  14. By default the library is set to control 100 LEDs. You can change this by editing the
  15. following line in the MCMAX7219.h header file:
  16.  
  17. #define NUMBEROFLEDS 200 <--- Change this number to match the number of LEDS connected
  18.  
  19. You can download the library from the software section of our support forum here:
  20. http://forum.hobbycomponents.com/viewforum.php?f=58
  21.  
  22. Or from Github here:
  23. https://github.com/HobbyComponents/HCWS2812
  24.  
  25.  
  26. You may copy, alter and reuse this code in any way you like, but please leave
  27. reference to HobbyComponents.com in your comments if you redistribute this code.
  28. This software may not be used directly for the purpose of selling products that
  29. directly compete with Hobby Components Ltd's own range of products.
  30.  
  31. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  32. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  33. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  34. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  35. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  36. REASON WHATSOEVER.
  37. */
  38.  
  39. /* Include the HCWS2812 library */
  40. #include "HCWS2812.h"
  41.  
  42. /* Create an instance of the library*/
  43. HCWS2812 HCWS2812;
  44.  
  45. void setup()
  46. {
  47.   /* Set the R,G,B background colours to zero */
  48.   HCWS2812.SetBG(0, 0, 0);
  49.   /* Clear the output buffer */
  50.   HCWS2812.ClearBuffer();
  51. }
  52.  
  53.  
  54. /* Main program */
  55. void loop()
  56. {
  57.   int index;
  58.  
  59.   /* Step forward through each LED */
  60.   for(index = 0; index < NUMBEROFLEDS; index++)
  61.   {  
  62.     HCWS2812.ClearBuffer();
  63.     RGBBuffer[RED][index] = 255;
  64.     HCWS2812.Refresh();
  65.     delay(100);
  66.   }
  67.  
  68.   /* Step backward through each LED */
  69.   for(index = NUMBEROFLEDS; index; index--)
  70.   {  
  71.     HCWS2812.ClearBuffer();
  72.     RGBBuffer[RED][index - 1] = 255;
  73.     HCWS2812.Refresh();
  74.     delay(100);
  75.   }
  76. }

  1. /* FILE:    HCWS2812_Random_Example
  2.    DATE:    26/03/15
  3.    VERSION: 0.1
  4.    AUTHOR:  Andrew Davies
  5.  
  6. 11/03/15 version 0.1: Original version
  7.  
  8. This is an example of how to use the HCMAX7219 library to control one or more
  9. RGB LEDS. The example will set each LED to a random colour.
  10.  
  11. To use this example connect one or more LEDs in series (Dout --> Din) and connect
  12. the first LED's Din pin to digital 2 (D4 on ESP8266 Lolin based boards) of your Arduino.
  13.  
  14. By default the library is set to control 100 LEDs. You can change this by editing the
  15. following line in the MCMAX7219.h header file:
  16.  
  17. #define NUMBEROFLEDS 200 <--- Change this number to match the number of LEDS connected
  18.  
  19. You can download the library from the software section of our support forum here:
  20. http://forum.hobbycomponents.com/viewforum.php?f=58
  21.  
  22. Or from Github here:
  23. https://github.com/HobbyComponents/HCWS2812
  24.  
  25. You may copy, alter and reuse this code in any way you like, but please leave
  26. reference to HobbyComponents.com in your comments if you redistribute this code.
  27. This software may not be used directly for the purpose of selling products that
  28. directly compete with Hobby Components Ltd's own range of products.
  29.  
  30. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  31. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  32. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  33. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  34. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  35. REASON WHATSOEVER.
  36. */
  37.  
  38. /* Include the HCWS2812 library */
  39. #include "HCWS2812.h"
  40.  
  41. /* Create an instance of the library*/
  42. HCWS2812 HCWS2812;
  43.  
  44. void setup()
  45. {
  46.   /* Set the R,G,B background colours to zero */
  47.   HCWS2812.SetBG(0, 0, 0);
  48.   /* Clear the output buffer */
  49.   HCWS2812.ClearBuffer();
  50. }
  51.  
  52.  
  53. void loop()
  54. {
  55.   int index;
  56.  
  57.   /* Fill the output buffer with random colours */
  58.   for(index = 0; index < NUMBEROFLEDS; index++)
  59.   {  
  60.     RGBBuffer[RED][index] = random(0,255);
  61.     RGBBuffer[GREEN][index] = random(0,255);
  62.     RGBBuffer[BLUE][index] = random(0,255);
  63.   }
  64.  
  65.   /* Send the output buffer to the LEDs */
  66.   HCWS2812.Refresh();
  67.  
  68.   /* Wait a moment before doing it again */
  69.   delay(100);
  70. }
  1. /* FILE:    HCWS2812_Message_Display_Example
  2.    DATE:    26/03/15
  3.    VERSION: 0.1
  4.    AUTHOR:  Andrew Davies
  5.  
  6. 11/03/15 version 0.1: Original version
  7.  
  8. This is an example of how to use the HCMAX7219 library to create a message display.
  9. To use this example you will need to connect the LEDs in series (Dout --> Din) and
  10. arrange them in columns of 8 to create your LED array. Connect the Din of the first
  11. LED to digital pin  of your Arduino.
  12.  
  13. You can create as many columns of 8 LEDs as you like but you will need to edit the
  14. following line in the HCWS2812.h header file:
  15.  
  16. #define NUMBEROFLEDS 200 <--- Change this number to match the number of LEDS connected
  17.  
  18. This value must be an multiple of 8 otherwise the print functions will not work
  19.  
  20. You can download the library from the software section of our support forum here:
  21. http://forum.hobbycomponents.com/viewforum.php?f=58
  22.  
  23. Or from Github here:
  24. https://github.com/HobbyComponents/HCWS2812
  25.  
  26.  
  27.  
  28. You may copy, alter and reuse this code in any way you like, but please leave
  29. reference to HobbyComponents.com in your comments if you redistribute this code.
  30. This software may not be used directly for the purpose of selling products that
  31. directly compete with Hobby Components Ltd's own range of products.
  32.  
  33. THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
  34. EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
  35. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
  36. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
  37. INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
  38. REASON WHATSOEVER.
  39. */
  40.  
  41. /* Include the HCWS2812 library */
  42. #include "HCWS2812.h"
  43.  
  44. /* Create an instance of the library*/
  45. HCWS2812 HCWS2812;
  46.  
  47. void setup()
  48. {
  49.   Serial.begin(9600);
  50.  
  51.   /* Set the fonts R G B colour to white */
  52.   HCWS2812.SetFontFG(100, 100, 100);
  53.   /* Set the fonts R G B background colour to light red */
  54.   HCWS2812.SetBG(20, 0, 0);
  55. }
  56.  
  57. /* Main program */
  58. void loop()
  59. {
  60.   byte index;
  61.  
  62.   /* Scroll some text with random background colours */
  63.   for(index = 0; index < 160; index++)
  64.   {
  65.     /* Clear the output buffer */
  66.     HCWS2812.ClearBuffer();
  67.     /* Change the fonts background colour every 16 steps */
  68.     if (index % 16 == 0)
  69.       HCWS2812.SetFontBG(random(0,20), random(0,20), random(0,20));
  70.    
  71.     /* Print some text to the output buffer */
  72.     HCWS2812.print("HOBBY COMPONENTS", index);
  73.    
  74.     /* Send the contents of the output buffer to the LEDs */
  75.     HCWS2812.Refresh();
  76.    
  77.     /* Wait a little before moving the text to the next position */
  78.     delay(80);  
  79.   }    
  80.    
  81.   HCWS2812.ClearBuffer();  
  82.   /* Set the fonts background colour to light green */
  83.   HCWS2812.SetFontBG(0, 20, 0);
  84.  
  85.   /* Scroll an integer number */
  86.   for(index = 0; index < 64; index++)
  87.   {
  88.     HCWS2812.ClearBuffer();
  89.     HCWS2812.print(-1234, index);
  90.     HCWS2812.Refresh();
  91.     delay(80);  
  92.   }
  93.  
  94.  
  95.   HCWS2812.ClearBuffer();  
  96.   /* Set the fonts background colour to light green */
  97.   HCWS2812.SetFontBG(0, 20, 0);
  98.  
  99.   /* Scroll an integer number with a decimal point
  100.      (notice a leading zero is added) */
  101.   for(index = 0; index < 80; index++)
  102.   {
  103.     HCWS2812.ClearBuffer();
  104.     HCWS2812.print(-1234, 4, index);
  105.     HCWS2812.Refresh();
  106.     delay(80);  
  107.   }
  108. }




Image

HCWS2812 latest version (V2.0):
HCWS2812_V2_0_0.zip

HCWS2812 version (V1.0):
HCWS2812_V1_0.zip

Older versions:
HCWS2812.zip



Libraries, example code, and diagrams are provided as an additional free service by Hobby Components and are not sold as part of this product. We do no provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.
You do not have the required permissions to view the files attached to this post.

luke1969
Posts: 5
Joined: Fri Nov 10, 2017 7:34 pm

Re: HCWS2812 Library For Serial RGB LEDs

Post by luke1969 » Sat Nov 11, 2017 12:55 pm

I have made a POV with 8 RGB LEDs.
This a great library and works well most of the time.
However, occasionally when using the print function to print an integer value, it crashes. I've checked and double checked my hardware and code, tried adding delays but still it shuts down.
If anybody has experienced the same thing and found a solution, please help. Thanks.
Luke1969morgan (Instagram)

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCWS2812 Library For Serial RGB LEDs

Post by andrew » Mon Nov 13, 2017 10:56 am

Can you provide an example that causes the crash?
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

luke1969
Posts: 5
Joined: Fri Nov 10, 2017 7:34 pm

Re: HCWS2812 Library For Serial RGB LEDs

Post by luke1969 » Tue Nov 14, 2017 10:39 pm

8 rgb LED POV.
I have pasted a copy of my code below. As you can see i have 4 modes which can be toggled using a button switch on pin 15. All modes work fine except, mode 1, which is a simple counting display. It works for about 20 seconds counting nicely, and then all leds turn off and it's frozen. I have to disconnect the power supply and scratch my head.

Code: Select all

#include "HCWS2812.h"


HCWS2812 HCWS2812;
int r,g,b; int x=0; int z=0; int o=0;
int mode =0; int button;
int latch=0;
int ani=0;
int counter =0;

byte R[]{255,255,0,0,0,255,255};
byte G[]{0,255,255,255,0,0,130};
byte B[]{0,0,0,255,255,255,0};

byte index;


byte invader[]={

  0,255,255,255,255,0,0,0,
  0,0,255,255,255,255,0,255,
  255,255,255,0,255,255,255,0,
  0,0,255,255,255,255,0,0,
   0,0,255,255,255,255,0,0,
   255,255,255,0,255,255,255,0,
   0,0,255,255,255,255,0,255,
   0,255,255,255,255,0,0,0,

};  

 byte invader2[]={

  0,0,0,255,255,255,255,0,
  0,0,255,255,255,255,0,0,
  255,255,255,0,255,255,255,255,
  0,0,255,255,255,255,0,0,
  0,0,255,255,255,255,0,0,
   255,255,255,0,255,255,255,255,
   0,0,255,255,255,255,0,0,
   0,0,0,255,255,255,255,0,
   
 
};

    byte invader3[]={
  
  0,255,255,255,255,0,0,0,
  0,0,255,255,255,255,0,255,
  255,255,255,0,255,255,255,0,
  0,0,255,255,255,255,0,0,
   0,0,255,255,255,255,0,0,
  255,255,255,0,255,255,255,0,
  0,0,255,255,255,255,0,255,
  0,255,255,255,255,0,0,0
};   
 byte invader4[]={
  
  0,0,0,255,255,0,0,255,
  0,0,255,255,255,0,255,0,
  0,255,255,0,255,255,0,255,
  255,255,255,255,255,0,255,0,
  255,255,255,255,255,0,255,0,
  0,255,255,0,255,255,0,255,
  0,0,255,255,255,0,255,0,
  0,0,0,255,255,0,0,255,
};   

void setup() {
 
  //Serial.begin(9600); 
 pinMode(9,OUTPUT);
 pinMode(15,INPUT_PULLUP);
  
  HCWS2812.SetBG(0, 255, 0);
}


 
void loop() {

  button = digitalRead(15);
  if(button==LOW){mode++;latch=0;
  HCWS2812.ClearBuffer();
   HCWS2812.Refresh();
  delay(500);
    if(mode==5){mode=0;}

     }

     
  //*************************MODE 0 : Shuts off LEDS
     
   if(mode==0){
    if(latch==0){
      for(index = 0; index < 8; index++){
    HCWS2812.SetBG(0,0,0);
    HCWS2812.SetFontBG(0, 0, 0);
    HCWS2812.SetFontFG(0, 0, 0);
    RGBBuffer[RED][index]=0;
    RGBBuffer[GREEN][index]=0;
    RGBBuffer[BLUE][index]=0;
    HCWS2812.ClearBuffer();
    HCWS2812.Refresh();
    latch=1;  
     }
   }
 }
  //***************************************COUNT

  if(mode==1){
  x++;
  if(x==50){x=0; counter++;}
  if(counter==100){counter=0;}
  HCWS2812.SetBG(0, 0, 2);
  HCWS2812.SetFontBG(0, 0, 2);
  HCWS2812.SetFontFG(150, 50, 0);
 
  for(index = 0; index <16; index++){
  
    HCWS2812.print(counter,index);
  
  
    HCWS2812.Refresh();
    delayMicroseconds(350); 
     
  }
  
  HCWS2812.ClearBuffer();
  HCWS2812.Refresh();
  delay(40);
  }
  
   //****************MODE 2 displays ARDUINO in colour arrays

    if(mode==2){
      HCWS2812.SetFontBG(0, 0, 2);HCWS2812.SetBG(0,0,2);
 HCWS2812.SetFontFG(200, 200, 0);
  for(index = 0; index <56; index++){
  
    HCWS2812.print("Arduino", index);
    HCWS2812.SetFontFG(R[index/8],G[index/8],B[index/8]);
    HCWS2812.Refresh();
     delayMicroseconds(100);   
      } 
      HCWS2812.ClearBuffer();
    HCWS2812.Refresh();
      delay(35);
     }
     
    //******************MODE 3 : space invaders animation

     if(mode==3){  HCWS2812.SetBG(1, 0, 1);
if(ani==6){ani=0;}
  if(ani<3){ vade0();}
   if(ani>=3){vade1();}
      
     }
     
     //******************MODE 4: prints LUKE

     if(mode==4){
   
  HCWS2812.SetBG(0, 0, 2);
 HCWS2812.SetFontBG(0, 0, 2);
 HCWS2812.SetFontFG(150, 50, 0);
 
  for(int test = 0; test <32; test++){
  
    HCWS2812.print("LUKE",test);
   
    HCWS2812.Refresh();
    delayMicroseconds(300); 
     
  }
 
   
  HCWS2812.ClearBuffer();
  HCWS2812.Refresh();
  delay(70);
  }

  //************************************
 
     
   }

   void vade0(){
     for(int i=0;i<8;i++){
  
   
  RGBBuffer[GREEN][i]=invader[z]; 
  z++;
 }
  HCWS2812.Refresh();
  delayMicroseconds(250);
  if(z==64){z=0;ani++;
     
 HCWS2812.ClearBuffer();
   HCWS2812.Refresh();
   delay(10);}
    
   }

   void vade1(){
    for(int i=0;i<8;i++){

   
  RGBBuffer[GREEN][i]=invader2[o]; 
o++;
 }
  HCWS2812.Refresh();
  delayMicroseconds(250);
 if(o==64){o=0;ani++;
 HCWS2812.ClearBuffer();
   HCWS2812.Refresh();
   delay(10);}
     
   }

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCWS2812 Library For Serial RGB LEDs

Post by andrew » Wed Nov 15, 2017 4:08 pm

I've taken a look at your code and I can't seem to replicate a crash. There are however a couple of things I've noticed about your example..

For mode 1 the delay within the for loop is very low (350us per step). Did you mean to use delay() instead of delayMicroseconds()? The short delay will cause the integer you wish to display to whiz past at a speed so fast you won't be able to see it. Is this intentional? The second issue is that because so little time is spent in the mode 1 section it will probably have enough time to execute the code and loop back to the top of your main loop (~46ms) before you have had chance to release the mode button causing it to skip to the next mode (mode 2).

You may want to take a look at these two things first as they don't look right to me. If you still think your code is crashing try isolating each section. I.e. commenting out the other modes and any other bits of code not relevant to mode 1. Do this a bit at a time to try and isolate exactly where the problem is.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

luke1969
Posts: 5
Joined: Fri Nov 10, 2017 7:34 pm

Re: HCWS2812 Library For Serial RGB LEDs

Post by luke1969 » Wed Nov 15, 2017 11:28 pm

Thanks for the quick response. I think I have a power supply issue. When I upload my sketch via the arduino uno to my home made board, power is supplied by the usb cable and there are no problems. ( I just can't see it as it is a POV spinner. As you have pointed out 350 microseconds is too fast, when stationary, but when spinning (overlapping itself) produces a nice animation.) When power is supplied by a lithium battery, it shuts down, but only in mode 1. Bizzar !!! I will replace the battery, check my soldering and modify the sketch.
The HCWS2812 library is great and really works well. Thank you for providing it and support.

luke1969
Posts: 5
Joined: Fri Nov 10, 2017 7:34 pm

Re: HCWS2812 Library For Serial RGB LEDs

Post by luke1969 » Wed Nov 15, 2017 11:41 pm

Here are some images of my project, including mode 1 just before it shuts off.
You do not have the required permissions to view the files attached to this post.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCWS2812 Library For Serial RGB LEDs

Post by andrew » Thu Nov 16, 2017 3:22 pm

Ah the very short time delay makes sense now! The LEDs can use a lot of power so maybe in mode one it happens to use slightly more power than the other modes. Maybe your battery pack doesn't have the capacity or can't react quickly enough to the fast changing loads from your LEDs. If you have thin wires from your battery that could also cause a problem.

Your project looks great btw, it's the first time I've seen the library used in this way.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

luke1969
Posts: 5
Joined: Fri Nov 10, 2017 7:34 pm

Re: HCWS2812 Library For Serial RGB LEDs

Post by luke1969 » Thu Nov 16, 2017 9:23 pm

Problem solved !! and it's thanks to you. I made this circuit very quickly with minimal components. As it worked fine for all other modes I thought it was a bug in the code somewhere, but when you said " Maybe your battery pack doesn't have the capacity ", I immediately knew the circuit requires a capacitor. (How stupid of me.) Thanks again for your help. I can now make a POV clock.

Zo0x
Posts: 1
Joined: Thu Aug 09, 2018 12:02 am

Re: HCWS2812 Library For Serial RGB LEDs

Post by Zo0x » Thu Aug 09, 2018 8:49 pm

Hi,

I am trying to use this library with a small board based on the ATmega32U4 and have had great success with standard pro micro's (leonardo) but when I use this smaller board (DFRobot Beetle) I am facing a big problem.

So as the library is hard coded to use pin 8 I struck a problem with the smaller board as it does not have pin 8 available, but it does have pin 9!
To address this issue I opted to modify this section in the library:

Code: Select all

/* Settings for ATmega32U4 based Arduino's. (Leonardo, 5V Pro Micro, etc..) */
#elif defined (__AVR_ATmega32U4__)
#define HIGHDELAYHIGH __asm__ __volatile__ ("nop\n nop\n nop\n nop\n nop\n nop\n nop\n")
#define HIGHDELAYLOW __asm__ __volatile__ ("nop\n nop\n nop\n nop\n nop\n nop\n")
#define LOWDELAYHIGH __asm__ __volatile__ ("")
#define LOWDELAYLOW __asm__ __volatile__ ("nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n nop\n")

/* PIN 8 */
#define DOUT_PIN 0x10
#define DOUT_PORT PORTB
#define DOUT_DDR DDRB
I changed the DOUT_PIN line to the following:

Code: Select all

#define DOUT_PIN digitalPinToBitMask(9)
Which seems to have worked somewhat, as previously the LEDs I am using [45mm WS2812B 16 RGB LED Ring (HCMODU0090)] were turned off entirely but now they come on, however they are always on bright white, nothing I do manages to change the colour. I did try to work out the bitmask manually but for some reason my maths was not good enough and it wouldn't work so I opted to use the function instead.

Is there something else I need to change in the code to make this work on a different pin? Or is there something wrong with my setup?

It worked fine on my other larger pro micro board on pin 8, however that board is too large for my project so I need the smaller board.


Thanks in advanced for anybody's help.



EDIT:
I managed to solve my problem, it seems that taking a break for a day makes you think differently, for those of you who are wondering, it was my bitmask that was messing with the timings.

I changed my code to the following and it worked great!

Code: Select all

#define DOUT_PIN 0x20
For those who are wondering or wanting to change pins I have included how I worked it out below:

First, look up the board pinout, mine was: https://www.arduino.cc/en/Hacking/PinMapping32u4
Find the new pin number: i.e. Digital Pin 9 is PB5 (Port B, bit 5)
Convert the binary value to the hex representation: 0010 0000 = 0x20 (move along 6 bits from the right, we count from 0, i.e. Pin 11 is PB7/1000 0000/0x80)
Update the library to use the new pin, don't forget to update the other sections with the relevant bitmasks and ports if you intend on using other boards :)


Thanks again for a great library!

Post Reply

Return to “Arduino”