Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Can someone explain this code to me please?
QtWS25 Last Chance

Can someone explain this code to me please?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
14 Posts 4 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    UnknownCOder
    wrote on last edited by VRonin
    #1

    (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.

    JKSHJ 1 Reply Last reply
    0
    • U UnknownCOder

      (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.

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi @UnknownCOder, please describe where you got the code from and tell us what you think the code is doing.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      U 1 Reply Last reply
      3
      • JKSHJ JKSH

        Hi @UnknownCOder, please describe where you got the code from and tell us what you think the code is doing.

        U Offline
        U Offline
        UnknownCOder
        wrote on last edited by
        #3

        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.

        JKSHJ 1 Reply Last reply
        0
        • U UnknownCOder

          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.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @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:

          1. What model are the strips?
          2. What model is the sound module?
          3. What is your goal for learning this YouTube tutorial?
          4. Where is this code meant to be run?
          5. What programming experience do you have (if any)?
          6. What brought you to this Qt Forum?

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          2
          • U Offline
            U Offline
            UnknownCOder
            wrote on last edited by
            #5
            1. 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)

            2. 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

            3. 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.

            4. It is meant to be run on arduino.

            5. Not much, I have just recently gotten into coding and want to learn more.

            6. This Qt forum seemed the most applicable to my question :). -

            JKSHJ 1 Reply Last reply
            0
            • U UnknownCOder
              1. 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)

              2. 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

              3. 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.

              4. It is meant to be run on arduino.

              5. Not much, I have just recently gotten into coding and want to learn more.

              6. This Qt forum seemed the most applicable to my question :). -

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              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.
              If x 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:

              1. Copy the RGB value from LED #9 into LED #10
              2. Copy the RGB value from LED #8 into LED #9
              3. Copy the RGB value from LED #7 into LED #8
              4. Copy the RGB value from LED #6 into LED #7
              5. 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.

              1. 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.

              1. 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)

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              jsulmJ U aha_1980A 3 Replies Last reply
              6
              • JKSHJ JKSH

                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.
                If x 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:

                1. Copy the RGB value from LED #9 into LED #10
                2. Copy the RGB value from LED #8 into LED #9
                3. Copy the RGB value from LED #7 into LED #8
                4. Copy the RGB value from LED #6 into LED #7
                5. 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.

                1. 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.

                1. 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)

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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?)

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                JKSHJ U 2 Replies Last reply
                2
                • JKSHJ JKSH

                  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.
                  If x 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:

                  1. Copy the RGB value from LED #9 into LED #10
                  2. Copy the RGB value from LED #8 into LED #9
                  3. Copy the RGB value from LED #7 into LED #8
                  4. Copy the RGB value from LED #6 into LED #7
                  5. 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.

                  1. 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.

                  1. 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)

                  U Offline
                  U Offline
                  UnknownCOder
                  wrote on last edited by
                  #8

                  @JKSH Thank you very much! This helps me a lot !!! I appreciate your efforts. Thank you again!

                  1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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?)

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    @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!

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    jsulmJ 1 Reply Last reply
                    1
                    • JKSHJ JKSH

                      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.
                      If x 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:

                      1. Copy the RGB value from LED #9 into LED #10
                      2. Copy the RGB value from LED #8 into LED #9
                      3. Copy the RGB value from LED #7 into LED #8
                      4. Copy the RGB value from LED #6 into LED #7
                      5. 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.

                      1. 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.

                      1. 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)

                      aha_1980A Offline
                      aha_1980A Offline
                      aha_1980
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @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

                      Qt has to stay free or it will die.

                      JKSHJ 1 Reply Last reply
                      2
                      • JKSHJ JKSH

                        @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!

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @JKSH I wasn't criticizing you even if I replied to you :-) Just wanted to point out something looking fishy.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2
                        • aha_1980A aha_1980

                          @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

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by
                          #12

                          @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)

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          2
                          • jsulmJ jsulm

                            @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?)

                            U Offline
                            U Offline
                            UnknownCOder
                            wrote on last edited by
                            #13

                            @jsulm Sorry due to space, I decided not to paste the whole code, num_led is = 60 in the real code.

                            jsulmJ 1 Reply Last reply
                            0
                            • U UnknownCOder

                              @jsulm Sorry due to space, I decided not to paste the whole code, num_led is = 60 in the real code.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @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!

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved