Can someone explain this code to me please?
-
(Under Void Loop):
int x = analogRead(0); x = x * sensitivity; if (x < 71) { leds[(num_leds/2)] = CRGB(255, 0, 0); } else if (x > 71 && x <= 142) { leds[(num_leds/2)] = CRGB(255, 154, 0); and: FastLED.show(); delay(10); for (int z = num_leds; z > (num_leds/2); z--) { leds[z] = leds[z - 1]; } for (int z = 0; z < (num_leds/2); z++) { leds[z] = leds[z + 1];
Thank you very much, all help is appreciated! Im just very confused.
-
(Under Void Loop):
int x = analogRead(0); x = x * sensitivity; if (x < 71) { leds[(num_leds/2)] = CRGB(255, 0, 0); } else if (x > 71 && x <= 142) { leds[(num_leds/2)] = CRGB(255, 154, 0); and: FastLED.show(); delay(10); for (int z = num_leds; z > (num_leds/2); z--) { leds[z] = leds[z - 1]; } for (int z = 0; z < (num_leds/2); z++) { leds[z] = leds[z + 1];
Thank you very much, all help is appreciated! Im just very confused.
Hi @UnknownCOder, please describe where you got the code from and tell us what you think the code is doing.
-
Hi @UnknownCOder, please describe where you got the code from and tell us what you think the code is doing.
Hi @JKSH , I got this code from a youtube tutorial about some LED Strips that can be controlled by a sound module. I believe the first code is relative to the RGB numbers to code the LED strips for certain colors. However I have no idea what the second code does. Any help would be greatly appreciated.
-
Hi @JKSH , I got this code from a youtube tutorial about some LED Strips that can be controlled by a sound module. I believe the first code is relative to the RGB numbers to code the LED strips for certain colors. However I have no idea what the second code does. Any help would be greatly appreciated.
@UnknownCOder said in Can someone explain this code to me please?:
I got this code from a youtube tutorial about some LED Strips that can be controlled by a sound module. I believe the first code is relative to the RGB numbers to code the LED strips for certain colors. However I have no idea what the second code does. Any help would be greatly appreciated.
I see. For us to help you effectively, please let us know:
- What model are the strips?
- What model is the sound module?
- What is your goal for learning this YouTube tutorial?
- Where is this code meant to be run?
- What programming experience do you have (if any)?
- What brought you to this Qt Forum?
-
-
The model of the LED strips are WS2811 : (https://www.amazon.com/BTF-LIGHTING-300LEDs-Addressable-Flexible-Non-waterproof/dp/B01CP5PQU6/ref=sr_1_1_sspa?crid=2ODXCF8G72Q34&keywords=ws2811%2Bled%2Bstrip&qid=1550778317&s=home-garden&sprefix=WS2811%2Bled%2Bs%2Clawngarden%2C131&sr=1-1-spons&th=1)
-
This is the sound module: https://www.amazon.com/gp/product/B01B26UBYA/ref=as_li_tl?ie=UTF8&tag=grensom-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01B26UBYA&linkId=16c244419c943cc9b65e63269863519e
-
The goal is to make the led strip correspond to the sound module, i'm sure the code works however I just don't understand what it does.
-
It is meant to be run on arduino.
-
Not much, I have just recently gotten into coding and want to learn more.
-
This Qt forum seemed the most applicable to my question :). -
-
-
-
The model of the LED strips are WS2811 : (https://www.amazon.com/BTF-LIGHTING-300LEDs-Addressable-Flexible-Non-waterproof/dp/B01CP5PQU6/ref=sr_1_1_sspa?crid=2ODXCF8G72Q34&keywords=ws2811%2Bled%2Bstrip&qid=1550778317&s=home-garden&sprefix=WS2811%2Bled%2Bs%2Clawngarden%2C131&sr=1-1-spons&th=1)
-
This is the sound module: https://www.amazon.com/gp/product/B01B26UBYA/ref=as_li_tl?ie=UTF8&tag=grensom-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B01B26UBYA&linkId=16c244419c943cc9b65e63269863519e
-
The goal is to make the led strip correspond to the sound module, i'm sure the code works however I just don't understand what it does.
-
It is meant to be run on arduino.
-
Not much, I have just recently gotten into coding and want to learn more.
-
This Qt forum seemed the most applicable to my question :). -
Thanks for the info. Let's get started.
I believe the first code is relative to the RGB numbers to code the LED strips for certain colors.
Yes, you're right.
Both code snippets involve updating RGB values.
int x = analogRead(0); x = x * sensitivity;
This takes the value of your analog input pin, stores it in the variable
x
, then multiplies it by a sensitivity value.if (x < 71) { leds[(num_leds/2)] = CRGB(255, 0, 0); } else if (x > 71 && x <= 142) { leds[(num_leds/2)] = CRGB(255, 154, 0);
If
x
is less than 71, the middle LED's colour gets set to RGB(255, 0, 0) which I think is red.
Ifx
is between 71 and 142, the middle LED's colour gets set to RGB(255,154, 0) which I think is orange.The code is incomplete. You should find the rest of it which applies to the cases where
x > 142
.FastLED.show(); delay(10);
You'll need to read the documentation to find out exactly what
FastLED.show()
does. But I'm guessing that it outputs the RBB values to to the physical LEDs.After that, there's a small delay (10 ms?)
for (int z = num_leds; z > (num_leds/2); z--) { leds[z] = leds[z - 1]; }
This part is hard to describe in words. To understand it, make sure you learn about for-loops and arrays.
But anyway, this code loops across your LED array. It and copies the values from one LED to its neighbour.
If
num_leds == 10
, this code will loop in the following order from LED #10 to LED #6:- Copy the RGB value from LED #9 into LED #10
- Copy the RGB value from LED #8 into LED #9
- Copy the RGB value from LED #7 into LED #8
- Copy the RGB value from LED #6 into LED #7
- Copy the RGB value from LED #5 into LED #6
for (int z = 0; z < (num_leds/2); z++) { leds[z] = leds[z + 1];
This is similar to the previous part, but it loops from LED #0 to LED #4 (assuming
num_leds == 10
)Again, your code is incomplete -- the closing curly braces (
}
) are missing.- Not much, I have just recently gotten into coding and want to learn more.
Welcome aboard! I'm quite confident that you'll have fun and interesting days ahead.
- This Qt forum seemed the most applicable to my question :)
Since you want to write code that runs on an Arduino, the Arduino Forum (https://forum.arduino.cc/ ) would be an even better choice.
AFAIK, Qt cannot run on Arduinos (although Qt can be used to make a GUI that runs on your PC to control your Arduino)
-
-
Thanks for the info. Let's get started.
I believe the first code is relative to the RGB numbers to code the LED strips for certain colors.
Yes, you're right.
Both code snippets involve updating RGB values.
int x = analogRead(0); x = x * sensitivity;
This takes the value of your analog input pin, stores it in the variable
x
, then multiplies it by a sensitivity value.if (x < 71) { leds[(num_leds/2)] = CRGB(255, 0, 0); } else if (x > 71 && x <= 142) { leds[(num_leds/2)] = CRGB(255, 154, 0);
If
x
is less than 71, the middle LED's colour gets set to RGB(255, 0, 0) which I think is red.
Ifx
is between 71 and 142, the middle LED's colour gets set to RGB(255,154, 0) which I think is orange.The code is incomplete. You should find the rest of it which applies to the cases where
x > 142
.FastLED.show(); delay(10);
You'll need to read the documentation to find out exactly what
FastLED.show()
does. But I'm guessing that it outputs the RBB values to to the physical LEDs.After that, there's a small delay (10 ms?)
for (int z = num_leds; z > (num_leds/2); z--) { leds[z] = leds[z - 1]; }
This part is hard to describe in words. To understand it, make sure you learn about for-loops and arrays.
But anyway, this code loops across your LED array. It and copies the values from one LED to its neighbour.
If
num_leds == 10
, this code will loop in the following order from LED #10 to LED #6:- Copy the RGB value from LED #9 into LED #10
- Copy the RGB value from LED #8 into LED #9
- Copy the RGB value from LED #7 into LED #8
- Copy the RGB value from LED #6 into LED #7
- Copy the RGB value from LED #5 into LED #6
for (int z = 0; z < (num_leds/2); z++) { leds[z] = leds[z + 1];
This is similar to the previous part, but it loops from LED #0 to LED #4 (assuming
num_leds == 10
)Again, your code is incomplete -- the closing curly braces (
}
) are missing.- Not much, I have just recently gotten into coding and want to learn more.
Welcome aboard! I'm quite confident that you'll have fun and interesting days ahead.
- This Qt forum seemed the most applicable to my question :)
Since you want to write code that runs on an Arduino, the Arduino Forum (https://forum.arduino.cc/ ) would be an even better choice.
AFAIK, Qt cannot run on Arduinos (although Qt can be used to make a GUI that runs on your PC to control your Arduino)
@JKSH said in Can someone explain this code to me please?:
for (int z = num_leds; z > (num_leds/2); z--) {
leds[z] = leds[z - 1];
}I would expect this code to crash because out of bounds access. But it is not clear how num_leds is initialized (numbers of LEDs - 1?)
-
Thanks for the info. Let's get started.
I believe the first code is relative to the RGB numbers to code the LED strips for certain colors.
Yes, you're right.
Both code snippets involve updating RGB values.
int x = analogRead(0); x = x * sensitivity;
This takes the value of your analog input pin, stores it in the variable
x
, then multiplies it by a sensitivity value.if (x < 71) { leds[(num_leds/2)] = CRGB(255, 0, 0); } else if (x > 71 && x <= 142) { leds[(num_leds/2)] = CRGB(255, 154, 0);
If
x
is less than 71, the middle LED's colour gets set to RGB(255, 0, 0) which I think is red.
Ifx
is between 71 and 142, the middle LED's colour gets set to RGB(255,154, 0) which I think is orange.The code is incomplete. You should find the rest of it which applies to the cases where
x > 142
.FastLED.show(); delay(10);
You'll need to read the documentation to find out exactly what
FastLED.show()
does. But I'm guessing that it outputs the RBB values to to the physical LEDs.After that, there's a small delay (10 ms?)
for (int z = num_leds; z > (num_leds/2); z--) { leds[z] = leds[z - 1]; }
This part is hard to describe in words. To understand it, make sure you learn about for-loops and arrays.
But anyway, this code loops across your LED array. It and copies the values from one LED to its neighbour.
If
num_leds == 10
, this code will loop in the following order from LED #10 to LED #6:- Copy the RGB value from LED #9 into LED #10
- Copy the RGB value from LED #8 into LED #9
- Copy the RGB value from LED #7 into LED #8
- Copy the RGB value from LED #6 into LED #7
- Copy the RGB value from LED #5 into LED #6
for (int z = 0; z < (num_leds/2); z++) { leds[z] = leds[z + 1];
This is similar to the previous part, but it loops from LED #0 to LED #4 (assuming
num_leds == 10
)Again, your code is incomplete -- the closing curly braces (
}
) are missing.- Not much, I have just recently gotten into coding and want to learn more.
Welcome aboard! I'm quite confident that you'll have fun and interesting days ahead.
- This Qt forum seemed the most applicable to my question :)
Since you want to write code that runs on an Arduino, the Arduino Forum (https://forum.arduino.cc/ ) would be an even better choice.
AFAIK, Qt cannot run on Arduinos (although Qt can be used to make a GUI that runs on your PC to control your Arduino)
@JKSH Thank you very much! This helps me a lot !!! I appreciate your efforts. Thank you again!
-
@JKSH said in Can someone explain this code to me please?:
for (int z = num_leds; z > (num_leds/2); z--) {
leds[z] = leds[z - 1];
}I would expect this code to crash because out of bounds access. But it is not clear how num_leds is initialized (numbers of LEDs - 1?)
@jsulm said in Can someone explain this code to me please?:
I would expect this code to crash because out of bounds access. But it is not clear how num_leds is initialized (numbers of LEDs - 1?)
That's why I said "if
num_leds == 10
" rather than "if you have 10 LEDs" ;-)OP is trying to learn how to read code that's copied from an existing tutorial. I'm not touching on debugging/troubleshooting or code correctness just yet.
@UnknownCOder said in Can someone explain this code to me please?:
@JKSH Thank you very much! This helps me a lot !!! I appreciate your efforts. Thank you again!
You're most welcome. All the best with your learning!
-
Thanks for the info. Let's get started.
I believe the first code is relative to the RGB numbers to code the LED strips for certain colors.
Yes, you're right.
Both code snippets involve updating RGB values.
int x = analogRead(0); x = x * sensitivity;
This takes the value of your analog input pin, stores it in the variable
x
, then multiplies it by a sensitivity value.if (x < 71) { leds[(num_leds/2)] = CRGB(255, 0, 0); } else if (x > 71 && x <= 142) { leds[(num_leds/2)] = CRGB(255, 154, 0);
If
x
is less than 71, the middle LED's colour gets set to RGB(255, 0, 0) which I think is red.
Ifx
is between 71 and 142, the middle LED's colour gets set to RGB(255,154, 0) which I think is orange.The code is incomplete. You should find the rest of it which applies to the cases where
x > 142
.FastLED.show(); delay(10);
You'll need to read the documentation to find out exactly what
FastLED.show()
does. But I'm guessing that it outputs the RBB values to to the physical LEDs.After that, there's a small delay (10 ms?)
for (int z = num_leds; z > (num_leds/2); z--) { leds[z] = leds[z - 1]; }
This part is hard to describe in words. To understand it, make sure you learn about for-loops and arrays.
But anyway, this code loops across your LED array. It and copies the values from one LED to its neighbour.
If
num_leds == 10
, this code will loop in the following order from LED #10 to LED #6:- Copy the RGB value from LED #9 into LED #10
- Copy the RGB value from LED #8 into LED #9
- Copy the RGB value from LED #7 into LED #8
- Copy the RGB value from LED #6 into LED #7
- Copy the RGB value from LED #5 into LED #6
for (int z = 0; z < (num_leds/2); z++) { leds[z] = leds[z + 1];
This is similar to the previous part, but it loops from LED #0 to LED #4 (assuming
num_leds == 10
)Again, your code is incomplete -- the closing curly braces (
}
) are missing.- Not much, I have just recently gotten into coding and want to learn more.
Welcome aboard! I'm quite confident that you'll have fun and interesting days ahead.
- This Qt forum seemed the most applicable to my question :)
Since you want to write code that runs on an Arduino, the Arduino Forum (https://forum.arduino.cc/ ) would be an even better choice.
AFAIK, Qt cannot run on Arduinos (although Qt can be used to make a GUI that runs on your PC to control your Arduino)
@JKSH said in Can someone explain this code to me please?:
The code is incomplete. You should find the rest of it which applies to the cases where x > 142.
Not only that, the case where
x == 71
is also unhandled ;)Regards
-
@jsulm said in Can someone explain this code to me please?:
I would expect this code to crash because out of bounds access. But it is not clear how num_leds is initialized (numbers of LEDs - 1?)
That's why I said "if
num_leds == 10
" rather than "if you have 10 LEDs" ;-)OP is trying to learn how to read code that's copied from an existing tutorial. I'm not touching on debugging/troubleshooting or code correctness just yet.
@UnknownCOder said in Can someone explain this code to me please?:
@JKSH Thank you very much! This helps me a lot !!! I appreciate your efforts. Thank you again!
You're most welcome. All the best with your learning!
-
@JKSH said in Can someone explain this code to me please?:
The code is incomplete. You should find the rest of it which applies to the cases where x > 142.
Not only that, the case where
x == 71
is also unhandled ;)Regards
@aha_1980 said in Can someone explain this code to me please?:
Not only that, the case where
x == 71
is also unhandled ;)D'oh! Missed that one.
@jsulm said in Can someone explain this code to me please?:
I wasn't criticizing you even if I replied to you :-) Just wanted to point out something looking fishy.
All good! I agree that it looks fishy. It's up to the tutorial author to fix it (along with the case of
x == 71
) -
@JKSH said in Can someone explain this code to me please?:
for (int z = num_leds; z > (num_leds/2); z--) {
leds[z] = leds[z - 1];
}I would expect this code to crash because out of bounds access. But it is not clear how num_leds is initialized (numbers of LEDs - 1?)
@jsulm Sorry due to space, I decided not to paste the whole code, num_led is = 60 in the real code.
-
@jsulm Sorry due to space, I decided not to paste the whole code, num_led is = 60 in the real code.
@UnknownCOder But how many LEDs?
If leds contains 60 elements then leds[z] will crash if z is 60 as the biggest valid index is 59!