simplified rules Neopixel

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
niblon91
New user
Posts: 1
Joined: 13 Oct 2022, 02:06

simplified rules Neopixel

#1 Post by niblon91 » 13 Oct 2022, 02:27

Hi there ,
I have a staircase lighting project, with 3 LEDs per step. I'm trying to simplify the rule: in the current state, I light 1 and 3 then when they go out I light 2, when they go out I light 4 and 6 etc .... but too much line for the rules with my 48 leds

Code: Select all

on System#Boot do
  timerSet 1,10
endon
 On Rules#Timer=1 do
  neopixel,1,50,0,0
  neopixel,3,50,0,0
   delay 2000

  neopixel,2,50,0,0
  neopixel,1,0,0,0
  neopixel,3,0,0,0
   delay 2000

 neopixel,4,50,0,0
 neopixel,6,50,0,0
 neopixel,2,0,0,0
  delay 2000

 neopixel,5,50,0,0
 neopixel,4,0,0,0
 neopixel,6,0,0,0
  delay 2000

 neopixel,7,50,0,0
 neopixel,9,50,0,0
 neopixel,5,0,0,0
  delay 2000

 neopixel,8,50,0,0
 neopixel,7,0,0,0
 neopixel,9,0,0,0
  delay 2000

 neopixel,8,0,0,0
  timerSet 1,1
endon
the goal will be to start it with a detector and to stop it with another detector

Thanks in advance
Niblon91

User avatar
Ath
Normal user
Posts: 3557
Joined: 10 Jun 2018, 12:06
Location: NL

Re: simplified rules Neopixel

#2 Post by Ath » 13 Oct 2022, 09:30

So you would need something like the NeoPixelLine command that supports an interval option for on and off pixels in the range?
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8777
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: simplified rules Neopixel

#3 Post by TD-er » 13 Oct 2022, 10:25

And please don't use delay in rules.
Rather use the looptimer. It does send events with an extra eventvalue representing the loop count.
N.B. you can also set the nr of loops.

See: https://espeasy.readthedocs.io/en/lates ... oop-timers

One way of reducing the amount of code is to set the leds in variables and use the loop counter as an offset

Code: Select all

On System#Boot do    //When the ESP boots, do
  looptimerset_ms,1,2000,10  // Start loop timer 1, 2000 msec interval, 10 loops
  
  let,1,1
  let,2,3
  let,3,2
  let,5,4
  let,6,6
  let,7,5
endon

On Rules#Timer=1 do
  if [int#100] != 0
    // First turn the previous ones off
    neopixel,[int#[int#100]],0,0,0
    let,100,[int#100]-1
    neopixel,[int#[int#100]],0,0,0
  endif

  if %eventvalue2% != 10
    // On the last loop, don't turn the LEDs on
    let,100,%eventvalue2% * 2 // The start index based on the loop count
    neopixel,[int#[int#100]],50,0,0
    let,100,[int#100]+1
    neopixel,[int#[int#100]],50,0,0
  else
    let,100,0 // Set the index to 0 again
  endif
endon
[code]
Then you have to initialize the other variables at boot containing the LED indices.

N.B. this code is untested.

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: simplified rules Neopixel

#4 Post by igorka » 07 Nov 2022, 10:43

I'm starting to get acquainted with NeoPixel (WS2818B)...As always, there are more questions than answers.
Why does such a record work incorrectly?

Code: Select all

On System#Boot do
let,1,120
let,2,100
NeoPixelAllHSV,let1,let2,10
endon
I want to try such a code example as for Arduino, but I don't have enough mind yet...

Code: Select all

#include <Adafruit_NeoPixel.h> 

int pix = 30;

Adafruit_NeoPixel strip (pix, 6, NEO_GRB + NEO_KHZ800);

void setup() {
   strip.begin();                     
   strip.show();                    
   strip.setBrightness(50); 
}

void loop() {
   for (int i = 0; i <= pix; i++) {
      strip.setPixelColor(i, 0, 0, 250);
      strip.show();   
      delay(50);    
   }

   for (int b = 50; b < 250; b++) {
      for (int i = 0; i <= pix; i++) {
         strip.setPixelColor(i, 250, 250, b * i / 250);
      }
      strip.show();
      delay(50); 
   }
}

TD-er
Core team member
Posts: 8777
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: simplified rules Neopixel

#5 Post by TD-er » 07 Nov 2022, 11:23

The "let" command does set a variable.
For example "let,1,123"
This variable can then be used via [int#1] as integer or [var#1] as floating point value.
"let,2,234" will of course set variable #2.

Thus your code would then be:

Code: Select all

On System#Boot do
  let,1,120
  let,2,100
  NeoPixelAllHSV,[int#1],[int#2],10
endon
See: https://espeasy.readthedocs.io/en/lates ... -variables

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: simplified rules Neopixel

#6 Post by igorka » 07 Nov 2022, 16:54

Thanks for the answer above!
Questions again ...
How to perform integer calculations (mathematical operations with variables)?

Code: Select all

On System#Boot do
loopTimerSet,1,1
let,1,1
let,2,1
let,3,0
endon
on Rules#Timer=1 do
[int#3]=[int#1]+[int#2]
endon
And such a record does not work either...

Code: Select all

[int#1]+1
What is my mistake, please indicate.

TD-er
Core team member
Posts: 8777
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: simplified rules Neopixel

#7 Post by TD-er » 07 Nov 2022, 17:02

Code: Select all

On System#Boot do
loopTimerSet,1,1
let,1,1
let,2,1
let,3,0
endon
on Rules#Timer=1 do
let,3,[int#1]+[int#2] // perform calculations
endon

Code: Select all

let,1,[int#1]+1

User avatar
Ath
Normal user
Posts: 3557
Joined: 10 Jun 2018, 12:06
Location: NL

Re: simplified rules Neopixel

#8 Post by Ath » 07 Nov 2022, 17:04

igorka wrote: 07 Nov 2022, 16:54 What is my mistake, please indicate.
It will help you having a look at the Rules documentation, quite some time went into writing that... ;)
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: simplified rules Neopixel

#9 Post by igorka » 09 Nov 2022, 12:58

TD-er wrote: 07 Nov 2022, 17:02

Code: Select all

let,3,[int#1]+[int#2] // perform calculations
let,1,[int#1]+1
Thanks for the help!Honestly, I only noticed that you replied... Fortunately for myself, I was able to find an answer on the forum, and only then read your message. Excuse me, but I have a question again...
My goal is to try to control the LEDs by Mqtt. Somewhere I make a mistake, I can't figure out where.

Code: Select all

On Led#hu do
 logentry,"MQTT import hu: %eventvalue1%"
 NeoPixelAll,%eventvalue1%,%eventvalue2%,%eventvalue3%"
endon
tried it like this:
On Led#saturat do
 logentry,"MQTT import saturat: %eventvalue2%"
 NeoPixelAll,[int#eventvalue1],[int#eventvalue2],[int#eventvalue3]"
endon
tried it like this:
On System#Boot do
NeoPixelAll,0,0,0
let,1,0
On Led#hu do
 logentry,"MQTT import hu: [int#1]"
 NeoPixelAll,[int#1],[int#eventvalue2],[int#eventvalue3]"
endon
endon
I understand from Mqtt the digits come in string format, but you need to convert to int?

TD-er
Core team member
Posts: 8777
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: simplified rules Neopixel

#10 Post by TD-er » 09 Nov 2022, 13:11

Code: Select all

On System#Boot do
NeoPixelAll,0,0,0
let,1,0
On Led#hu do
 logentry,"MQTT import hu: [int#1]"
 NeoPixelAll,[int#1],[int#eventvalue2],[int#eventvalue3]"
endon
endon
You cannot nest the on...do...endon parts

Apart from that, you still need to wrap the eventvalue part in percent characters:

Code: Select all

NeoPixelAll,[int#1],[int#%eventvalue2%],[int#%eventvalue3%]
Also you had a closing quote on that line.

But I'm not sure what you want to achieve here.
[int#%eventvalue2%] means: refer to the variable with index given as eventvalue.
So you need to have this variable set, or else you will get strange results.

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: simplified rules Neopixel

#11 Post by igorka » 09 Nov 2022, 15:41

I'm sorry, I wrote some nonsense with a bunch of mistakes, it's a rush and a low level of knowledge in programming.Maybe delete my previous post?I created three variables to control the RGB tape, let,1; let,2; let,3.I need the Mqtt protocol to send values and save them to the variables let,1; let,2; let,3.
This is my attempt to do this (an example for the let,1 variable):

Code: Select all

On System#Boot do
NeoPixelAll,0,0,0
let,1,0
let,2,0
let,3,0
endon
On Led#hu do
 logentry,"MQTT import hu: let,3,[int#1]"
 NeoPixelAll,[int#1],[int#2],[int#3]
endon
Screenshot_1.png
Screenshot_1.png (12.11 KiB) Viewed 2731 times
Screenshot_2.png
Screenshot_2.png (4.2 KiB) Viewed 2731 times
How to transfer values from variables hu, saturat, val to variables let,1; let,2; let,3 or directly control, something like this NeoPixelAll,[int#hu],[int#saturat],[int#val]?

User avatar
Ath
Normal user
Posts: 3557
Joined: 10 Jun 2018, 12:06
Location: NL

Re: simplified rules Neopixel

#12 Post by Ath » 09 Nov 2022, 16:26

igorka wrote: 09 Nov 2022, 15:41 How to transfer values from variables hu, saturat, val to variables let,1; let,2; let,3 or directly control, something like this NeoPixelAll,[int#hu],[int#saturat],[int#val]?
You can use the values directly, every time one of the values changes:
(You will have to enable the Generate events checkbox in the MQTT Import task settings)

Code: Select all

On Led do
  logentry,"MQTT import Hue: [Led#Hu], Sat [Led#saturat], Val [Led#val]"
  NeoPixelAllHSV,[Led#Hu],[Led#saturat],[Led#val]
endon
If you also enable the checkbox Single event with all values, it could be like this:

Code: Select all

On Led#All do
  logentry,"MQTT import Hue: %eventvalue1%, Sat %eventvalue2%, Val %eventvalue3%"
  NeoPixelAllHSV,%eventvalue1%,%eventvalue2%,%eventvalue3%
endon
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: simplified rules Neopixel

#13 Post by igorka » 09 Nov 2022, 16:55

Thank you, I am grateful to you for your help and time spent on me! But unfortunately your code examples don't work either, at least for me. :(
Such a rule:

Code: Select all

On System#Boot do
NeoPixelAllHSV,0,0,0
endon
On Led do
  logentry,"MQTT import Hue: [Led#Hu], Sat [Led#saturat], Val [Led#val]"
  NeoPixelAllHSV,[Led#Hu],[Led#saturat],[Led#val]
endon
Screenshot_3.png
Screenshot_3.png (31.75 KiB) Viewed 2722 times
Last edited by igorka on 09 Nov 2022, 17:15, edited 2 times in total.

User avatar
Ath
Normal user
Posts: 3557
Joined: 10 Jun 2018, 12:06
Location: NL

Re: simplified rules Neopixel

#14 Post by Ath » 09 Nov 2022, 17:01

Ah, that plugin doesn't send default value events, only events on receiving data, so the #All event isn't available.
You'll have to use my other example in that post using "on Led do"
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: simplified rules Neopixel

#15 Post by igorka » 09 Nov 2022, 17:20

Ath wrote: 09 Nov 2022, 17:01 You'll have to use my other example in that post using "on Led do"

Code: Select all

On Led do
  logentry,"MQTT import Hue: [Led#Hu], Sat [Led#saturat], Val [Led#val]"
  NeoPixelAllHSV,[Led#Hu],[Led#saturat],[Led#val]
endon
Screenshot_1.png
Screenshot_1.png (14.4 KiB) Viewed 2714 times

Code: Select all

On Led#All do
  logentry,"MQTT import Hue: %eventvalue1%, Sat %eventvalue2%, Val %eventvalue3%"
  NeoPixelAllHSV,%eventvalue1%,%eventvalue2%,%eventvalue3%
endon
Screenshot_2.png
Screenshot_2.png (14.59 KiB) Viewed 2714 times
As you can see, the result is the same.

User avatar
Ath
Normal user
Posts: 3557
Joined: 10 Jun 2018, 12:06
Location: NL

Re: simplified rules Neopixel

#16 Post by Ath » 09 Nov 2022, 21:23

igorka wrote: 09 Nov 2022, 17:20 As you can see, the result is the same.
Does it work if you use "on Led#Hu do" ?

If so, you can use an alternative like this:
(Remove any other "on Led* do" event handlers)

Code: Select all

On updateLed do
  logentry,"MQTT import Hue: [Led#Hu], Sat [Led#saturat], Val [Led#val]"
  NeoPixelAllHSV,[Led#Hu],[Led#saturat],[Led#val]
Endon
On Led#Hu do
  AsyncEvent,updateLed
Endon
On Led#saturat do
  AsyncEvent,updateLed
Endon
On Led#val do
  AsyncEvent,updateLed
Endon
Then any updated value will be handled, and update the led-stripe
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: simplified rules Neopixel

#17 Post by igorka » 10 Nov 2022, 06:26

Ath wrote: 09 Nov 2022, 21:23
igorka wrote: 09 Nov 2022, 17:20 As you can see, the result is the same.
Does it work if you use "on Led#Hu do" ?
Hooray works!!! You're a magician.Very complex code, I would never have thought of that.
Screenshot_3.png
Screenshot_3.png (42.26 KiB) Viewed 2652 times
I looked at the NeoPixel library and there in my opinion there is a convenient option for recording color:
led.setColor(0xFF00FF);
Also, I don't fully understand the color codes if you put it this way:

Code: Select all

NeoPixelHSV,<led nr>,<hue>,<saturation>,<value>
For example, if you write the color like this, then this is as clear as possible:

Code: Select all

NeoPixel,<led nr>,<red>,<green>,<blue>
NeoPixel,<led nr>,255,0,0//red
NeoPixel,<led nr>,0,255,0//green
NeoPixel,<led nr>,0,0,255//blue
NeoPixel,<led nr>,255,255,255//White

TD-er
Core team member
Posts: 8777
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: simplified rules Neopixel

#18 Post by TD-er » 10 Nov 2022, 10:42

That's the difference between "color spaces"

RGB is basically how the LEDs are addressed. You have some amount of R, some of G and some B.
But to humans, this color space isn't very intuitive if you use values other than 0 or 255 for these.

So the HSV color space was designed to be more intuitive for how we humans like to make adjustments.

To start with the H (hue).
It is just some angle on the color wheel:
Image
For example if the light is too yellow, you turn the angle either to the green side, or the red side.

The S (saturation) is a parameter to change between "less or more color".
Full saturation will be "as colorful as can be" and 0 saturation will be like a B/W photo. Only black and white and all shades of grey inbetween.
In terms of paint, this is called a "tint". (e.g. pastel tint)

The V(value) is just the amount of light.
So 0 is with the lights turned off, 255 is at maximum brightness (given the other two values H and S)

The V can somewhat be compared to typical terms used for paint, like a "tint" (pure color paint with white added) or a "shade" (pure color paint with black added).
Changing only the V is not (supposed to) change the color, but just "more or less light".

Since there has to be some conversion using a limited nr of input and output values (integer value steps), both on the HSV values and the RGB led driving values, you can see some strange artefacts when approaching the ends of the V value.
For example a nice yellow color may turn green when the V approaches 0, simply because the amount of red needed is already at its lowest possible value.

Anyway, let's not make things more complicated. Just keep in mind that color mixing with light differs from color mixing with paint. :)

User avatar
Ath
Normal user
Posts: 3557
Joined: 10 Jun 2018, 12:06
Location: NL

Re: simplified rules Neopixel

#19 Post by Ath » 10 Nov 2022, 11:44

igorka wrote: 10 Nov 2022, 06:26 Also, I don't fully understand the color codes if you put it this way:

Code: Select all

NeoPixelHSV,<led nr>,<hue>,<saturation>,<value>
I switched to using HSV colors in my code, as your MQTT messages seem to have the HSV values, that TD-er just explained how they work, it wouldn't be correct to use the default RGB commands as then it will not match your expected colors. And the NeoPixel plugin does support HSV colors :)
/Ton (PayPal.me)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: simplified rules Neopixel

#20 Post by igorka » 10 Nov 2022, 11:56

TD-er wrote: 10 Nov 2022, 10:42 Anyway, let's not make things more complicated. Just keep in mind that color mixing with light differs from color mixing with paint. :)
Of course we won’t, because you explained everything to me very sensibly!
Thank you for your help!
Ath wrote: 10 Nov 2022, 11:44
igorka wrote: 10 Nov 2022, 06:26 Also, I don't fully understand the color codes if you put it this way:
I switched to using HSV colors in my code, as your MQTT messages seem to have the HSV values, that TD-er just explained how they work, it wouldn't be correct to use the default RGB commands as then it will not match your expected colors.
My mistake. I started experiments with HSV, and then I decided to try with RGB, but I did not change the name of the variables.
Thank you for your help!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests