Tuesday, December 1, 2015
Lua and Neopixels
I had to learn several programming languages in a relative short time. I learned to program in Python, C++, Mit App inventor, and I learned some HTML. And now I had to learn LUA.
LUA is one of the programming languages used in the ESP8266 boards. It is the standard language for the NodeMCU boards.
So as I had to learn so many languages in a short time I struggled a bit with it.
The Problem:
I wanted to control a bunch of Neopixels (WS2812 leds) with my ESP-8266-01. Luckily enough there is a driver incorporated in the Lua language. The drivers description is as follows:
ws2812.writergb()
Description
Send the RGB Data in 8bits to WS2812
Syntax
ws2812.writergb(pin, string.char(R1,G1,B1(,R2,G2,B2...)) )
Parameters
pin = Supported all the PINs(0,1,2...)
R1 = The first WS2812 though the line's Red Channel's Parameters(0-255)
G1 = The first WS2812 though the line's Green Channel's Parameters(0-255)
B1 = The first WS2812 though the line's Blue Channel's Parameters(0-255)
... You can connect a lot of WS2812...
R2,G2,B2 is the next WS2812's Red, Green and Blue Channel's Parameters
Looks easy enough. So what I did is I send a snippet to the ESP8266-01 looking like this:
ws2812.writergb(3, string.char(120, 25, 80):rep(7))
And all 7 of my Neopixels displayed a nice white/purplisch color.
Note that the first I/O port of the ESP8266-01 being I/O 0 is attached to pin 3......
But that is not what I wanted. I wanted to adress all Neopixels seperately. I wanted each WS2812 to have a different colour.
That made things a bit more complicated.
The syntax for adressing all 7 pixels is:
ws2812.writergb(3,R1,G1,B1,R2,G2,B2,R3,G3,B3,R4,G4,B4,R5,G5,B5,R6,G6,B6,R7,G7,B7)
This looks easy and that is right. However programming it is a bit more complicated.
Solution.
Here is my solution.
I start with defining the colors.
Note that the higher the values, the brighter the Neopixels will be. You can make as many different color combinations as you want.
I made an Array which is as large as the number of Neopixels I had attached. In this case 7 pieces. And then I filled the array with the RGB values: first 3 with blue (b), the next one red (r) and the last 3 with green (g).
And the actual line to set the pixels is:
ws2812.writergb(3,n[1]..n[2]..n[3]..n[4]..n[5]..n[6]..n[7])
So this makes it very easy to adress the individual Neopixels which makes it easy to control them in various projects.
Till next time.
Have fun
Luc Volders
LUA is one of the programming languages used in the ESP8266 boards. It is the standard language for the NodeMCU boards.
So as I had to learn so many languages in a short time I struggled a bit with it.
The Problem:
I wanted to control a bunch of Neopixels (WS2812 leds) with my ESP-8266-01. Luckily enough there is a driver incorporated in the Lua language. The drivers description is as follows:
ws2812.writergb()
Description
Send the RGB Data in 8bits to WS2812
Syntax
ws2812.writergb(pin, string.char(R1,G1,B1(,R2,G2,B2...)) )
Parameters
pin = Supported all the PINs(0,1,2...)
R1 = The first WS2812 though the line's Red Channel's Parameters(0-255)
G1 = The first WS2812 though the line's Green Channel's Parameters(0-255)
B1 = The first WS2812 though the line's Blue Channel's Parameters(0-255)
... You can connect a lot of WS2812...
R2,G2,B2 is the next WS2812's Red, Green and Blue Channel's Parameters
Looks easy enough. So what I did is I send a snippet to the ESP8266-01 looking like this:
ws2812.writergb(3, string.char(120, 25, 80):rep(7))
And all 7 of my Neopixels displayed a nice white/purplisch color.
Note that the first I/O port of the ESP8266-01 being I/O 0 is attached to pin 3......
But that is not what I wanted. I wanted to adress all Neopixels seperately. I wanted each WS2812 to have a different colour.
That made things a bit more complicated.
The syntax for adressing all 7 pixels is:
ws2812.writergb(3,R1,G1,B1,R2,G2,B2,R3,G3,B3,R4,G4,B4,R5,G5,B5,R6,G6,B6,R7,G7,B7)
This looks easy and that is right. However programming it is a bit more complicated.
Solution.
Here is my solution.
I start with defining the colors.
Note that the higher the values, the brighter the Neopixels will be. You can make as many different color combinations as you want.
I made an Array which is as large as the number of Neopixels I had attached. In this case 7 pieces. And then I filled the array with the RGB values: first 3 with blue (b), the next one red (r) and the last 3 with green (g).
And the actual line to set the pixels is:
ws2812.writergb(3,n[1]..n[2]..n[3]..n[4]..n[5]..n[6]..n[7])
So this makes it very easy to adress the individual Neopixels which makes it easy to control them in various projects.
Till next time.
Have fun
Luc Volders