Page 1 of 1

In need of assistance with a sketch -Addressable LED

In need of assistance with a sketch -Addressable LED

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
Examples: "MacOS X - Your question", "MS Word - Your Tip or Trick".

Please note that switching to another language when reading a post will not work!
Posts will not have a translated counterpart.




RSS Feed

Home Forums Hardware Arduino In need of assistance with a sketch -Addressable LED

This topic contains 11 replies, has 2 voices, and was last updated by  abi 1 month, 3 weeks ago.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • 12247

    abi
    Participant

    Hi there,

    i have a project that im busy with, im using 60 led’s. I have tweaked a few sketches and have the project working satisfactory. I need help with a few items.

    okay, so far, the animation does this:

    New kitt animation from outside to center > comes in , in white and goes back in white twice, thereafter, the entire strip turns to amber and stays that way for 25 seconds before repeating itself.

    The Application: On a subwoofer enclosure on my Mercedes Benz – It will switch ON when the trunk opens.

    Due to the fast acting animation, i would like to delay the startup of the animation by 3seconds – im assuming turn all to black

    secondly, when the strip turns to amber and stays solid, id like the first4 leds and last 4 leds to be white however it must be smooth blending – they will not move, just stay on for the 25 second time but 2 colors fused Amber to White.

    lastly, when the 25 second time is up, i would like the led’s to fade off then restart. Currently it just switches off instead of fade 

    Attachments:
    12250

    hans
    Keymaster

    Hi Abi,

    This is a nice application indeed! I like it 

    1) fast acting animation

    You could add a delay(3000) before calling NewKITT(), or … make the animation slower by modifying the “SpeedDelay” value.
    Note that Delay works with milliseconds (1 second = 1000 milliseconds).

    void loop() {
      setAll(0,0,0);
      Delay(3000);
      NewKITT(100, 150, 70, 8, 25, 2); //Second last value depicts speed of eye 
      RGBLoop();
    }

    2) I’d like the first 4 leds and last 4 leds to be white

    You could try this code (you’ll have to determine the color values to make it as smooth as you would like to see it);

    void newRGBLoop() {
      setAll(0xff,20,0); // set all to amber
      // set first 4 and last 4 to a fade white to amber (you may have to tweak the calculation)
      for(int i=0; i<4; i++) {
         setPixel(i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4)); // 235 = 255 - 20, 20 for amber (from your code)
         setPixel(NUM_LEDS-1- i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4)); 
      }
      showStrip();
      delay(25000); // wait for 25 seconds
      setAll(0,0,0); // set all LEDs to black/off
    }

    As mentioned in the code, you may have to tweak the values for the calculation. Color/light isn’t linear  so simply dividing it in equal steps may not give the wanted results. 

    You could of course do this without a loop as well;

    void newRGBLoop() {
      setAll(0xff,20,0); // set all to amber
      // set first 4 and last 4 to a fade white to amber (you may have to tweak the calculation)
      setPixel(0, 0xff, 0xff, 0xff);
      setPixel(NUM_LEDS-1, 0xff, 0xff, 0xff);
      setPixel(1, 0xff, 0xe7, 0xde);
      setPixel(NUM_LEDS-2, 0xff, 0xe7, 0xde);
      setPixel(2, 0xff, 0xac, 0x9c);
      setPixel(NUM_LEDS-3, 0xff, 0xac, 0x9c);
      setPixel(3, 0xff, 0x55, 0x42);
      setPixel(NUM_LEDS-4, 0xff, 0x55, 0x42f);
      showStrip();
      
      delay(25000); // wait for 25 seconds
      setAll(0,0,0); // set all LEDs to black/off
    }

    Note: I picked the colors with the color picker (here) based on your initial amber value (0xff,20,0).
    It may need some fine tuning.

    3. Fade out …

    For fading you can use the loop (you’ve already looked at it). The key being the starting color.

    void FadeOut(byte red, byte green, byte blue){
      float r, g, b;
      for(int k = 255; k >= 0; k=k-2) {
        r = (k/256.0)*red;
        g = (k/256.0)*green;
        b = (k/256.0)*blue;
        setAll(r,g,b);
        showStrip();
      }
    }

    and call this after the delay(25000) in the previous example. Eg.

      FadeOut(0xff,20,00);

    Granted, this kinda ruins the first 4 and the last 4 LEDs.
    To accommodate those you’ll need to expand the code a little, something like this;

    void FadeOut() {
      for(int k=255; k>=0;k=k-2) {
        for(int i=0; i<NUM_LEDS, i++) {
          leds[i].r = max(0, leds[i].r - k);   // used "max" to make sure the value doesn't go below zero
          leds[i].g = max(0, leds[i].g - k); 
          leds[i].b = max(0, leds[i].b - k);  
        } showStrip();
      }
    }

    and we should be able to call it as such:

        FadeOut();

    Note: I haven’t tested any of these examples – I do not have my Arduino stuff handy at the moment.

    Hope this helps … 

    12252

    abi
    Participant

    Hi Hans,

    I’m Truly grateful for your assistance. I will try it this evening and get back to you.

    You guys Rock! Keep up the excellent work and thank you for all your help.

    Cheers! 

    12254

    hans
    Keymaster

    You’re welcome 

    I’m curious though what the end product will look like – I really like the idea. Maybe I’ll do something like that for my car 

    12256

    abi
    Participant

    hi Hans,

    thanks again, i will send a video once done. I haven’t had a chance to add your code yest. I’ll do  it over the weekend. I will message you though.

    Thanks!

    12259

    abi
    Participant

    Hi Hans, im busing adding the stuff you gave me. Theres an error coming up  when trying to add a 3 second delay on startup.

    “Delay  was not declared in this scope”

    12261

    abi
    Participant

    Hi Hans, i have another error on the second bit of code

    Attachments:
    12264

    hans
    Keymaster

    Looks like I made a type – add a “)” (without the double quotes) before the “;” in the code of the highlighted line.

    So the following line:

         setPixel(NUM_LEDS-1- i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4)); 

    becomes:

         setPixel(NUM_LEDS-1- i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4))); // <- add ( here 

    As for the error “delay not in scope”; it may be that the change above fixes that. If not then please post the full error message (should tell you the affected line number).

    12270

    abi
    Participant

    Hi Hans, I managed  to create the initial delay. Thereafter, i tried to add the code for the first 4 and last 4 leds to turn white. That didnt work however, after deleting that bit, my code was somehow altered. Error: setAll was not in the scope. I’ve attached it

    Attachments:
    12273

    abi
    Participant

    Hi Hans, here is the entire sketch. Please see what you can do for me.
    i have the delay. It works.
    Just need, first 4 and last 4 to stay white (but slightly blended to white from amber)
    thereafter, when the  25 sec timer is  up, they should all fade off, the start sequence again.

    thanks!

    • This reply was modified 2 months ago by  abi.
    • This reply was modified 2 months ago by  abi.
    Attachments:
    12279

    hans
    Keymaster

    I have not yet been able to test the code, but maybe we should walk through the error messages, so you’d be able to debug.
    So loaded your code into my Arduino IDE and did a test compile (menu: Sketch -> Verify/Compile) which resulted in these error messages:

    Arduino: 1.8.7 (Mac OS X), Board: "Arduino/Genuino Uno"
    In file included from /Users/hans/Documents/Arduino/sketch_jul18a/sketch_jul18a.ino:1:0:
    /Users/hans/Documents/Arduino/libraries/FastLED/FastLED.h:14:21: note: #pragma message: FastLED version 3.002.001
     # pragma message "FastLED version 3.002.001"
                         ^
    /Users/hans/Documents/Arduino/sketch_jul18a/sketch_jul18a.ino: In function 'void newRGBLoop()':
    sketch_jul18a:12:19: error: 'setAll' was not declared in this scope
       setAll(0xff,20,0); // set all to amber
                       ^
    sketch_jul18a:16:59: error: expected ')' before ';' token
          setPixel(i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4); // 235 = 255 - 20, 20 for amber (from your code)
                                                               ^
    sketch_jul18a:16:59: error: expected ')' before ';' token
    sketch_jul18a:17:72: error: 'setPixel' was not declared in this scope
          setPixel(NUM_LEDS-1- i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4))); 
                                                                            ^
    sketch_jul18a:19:13: error: 'showStrip' was not declared in this scope
       showStrip();
                 ^
    /Users/hans/Documents/Arduino/sketch_jul18a/sketch_jul18a.ino: At global scope:
    sketch_jul18a:26:7: error: expected declaration before '}' token
           }
           ^
    exit status 1
    'setAll' was not declared in this scope
    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

    So let’s go through these messages;

    1) ” # pragma message “FastLED version 3.002.001”

    This is just a message that we can ignore in this case.

    2) “setAll(0xff,20,0); // set all to amber” (sketch_jul18a:12:19: error: ‘setAll’ was not declared in this scope)

    You’ll see that in the file “sketch_jul18a”, line 12, at the 19th character of that line, setAll appears not defined.
    Let’s go through some more message to see if there is a connection with the other messages. After all “setAll()” is defined at the end of the sketch, so other typos/errors may cause this.

    3) “setPixel(i, 0xff, 255 – (i*(235/4)), 255 – (i*(255/4);” (sketch_jul18a:16:59: error: expected ‘)’ before ‘;’ token)

    As before, line 16 character 59; we’re missing a ‘)’ before the ‘;’ character.

    Easy fix, change line 16 to:

    setPixel(i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4)));

    We are actually missing 2 ‘)’ … you can see this in the IDE. When setting your text cursor at a ‘)’, the editor will highlight the ‘(‘ that goes with it.
    Keep this one in mind; forgetting a ‘)’ or a ‘;’ or ‘}’ are quite common.

    So, recompile/verify, and we will see; this did not fix the other problems, so let’s continue.

    4) “showStrip();” (expected declaration before ‘}’ token)

    This may be the culprit … 

    If we look at the code, you’ll see there is a boo-boo there:

    void setup()
    { delay(2000);
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    }
    void newRGBLoop() {
      setAll(0xff,20,0); // set all to amber
      // set first 4 and last 4 to a fade white to amber (you may have to tweak the calculation)
      for(int i=0; i<4; i++) {
         setPixel(i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4))); // 235 = 255 - 20, 20 for amber (from your code)
         setPixel(NUM_LEDS-1- i, 0xff, 255 - (i*(235/4)), 255 - (i*(255/4))); 
      }
      showStrip();
      
      delay(25000); // wait for 25 seconds
      setAll(0,0,0); // set all LEDs to black/off
    }
           
            
          }
          showStrip();
          delay(25); //time of strip to stay on
        }
        
        // Fade OUT
        for(int k = 255; k >= 0; k--) { 
          switch(j) { 
            case 0: setAll(0xff,25,0); break;
            
            
          }
          showStrip();
          delay(25); //time for strip to stay on
        }
      }
    }

    The bold section has no place to below to … I assume you pasted some of the code in, but forgot to adapt or delete some of the old code.

    Unfortunately, this doesn’t resolve things yet.

    5) undefined reference to `loop’

    After removing the bold code in the step 4, we get a new error message, and this one is straight forward: the linker (ld) cannot find the “loop()” function in your code!

    /var/folders/xb/gtxmm97s5ml1fmn0j78cck5h0000gn/T//cc0oU02c.ltrans0.ltrans.o: In function `main':
    /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/main.cpp:46: undefined reference to `loop'
    collect2: error: ld returned 1 exit status
    exit status 1

    Renaming “newRGBLoop” to “loop” fixes the issue.

    Now, since I cannot test this, I’m not sure if this is what you were looking for.
    However; lesson learned; be careful when pasting code, to not make a mess of things.
    And with this walk through you may become more comfortable trying to read and debug error messages.

    12285

    abi
    Participant

    Hi Hans, Once again, thanks a million for all your efforts. I cannot tell you how pleased i am getting assistance in this regard.

    i havent tried the changes as yet since im pushing to get the install complete..i have the running in white and settling at amber. Once im complete with the install, i can play with the  programming.

    will chat soon! Take care

Viewing 12 posts - 1 through 12 (of 12 total)



You must be logged in to reply to this topic.