Page 1 of 1

Running light with push button

Running light with push button

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 Running light with push button

This topic contains 16 replies, has 2 voices, and was last updated by  hans 2 months ago.

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • 12296

    robiv8
    Participant

    Hallo,

    can anybody help me with this code.

    I would like to have a build-up running light, the built-up about 0.5sec stands and then completely off without effect.

    So a kind of turn signal.

    But this only wen i push a button.

    Am an absolute newbie and can not continue.

    Thank you

    #include <FastLED.h>
    #define NUM_LEDS 46 
    #define DATA_PIN 2
    int buttonPin = 4; // the number of the pushbutton pin
    CRGB leds[NUM_LEDS];
    void setup() { 
      LEDS.addLeds<WS2812,DATA_PIN,GRB>(leds,NUM_LEDS);
      LEDS.setBrightness(100);
      pinMode(buttonPin, INPUT);
    }
    void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
    void loop() {
      int buttonState = digitalRead(buttonPin);
      static uint8_t hue = 0;
      if(buttonState == HIGH)
      for(int i = 0; i < NUM_LEDS; i++)
      {
         leds[i] = CRGB(255, 102, 0);
        FastLED.show(); 
        fadeall();
         delay(10);
      }
      for(int i = 0; i < NUM_LEDS; i++)
      {
        leds[i] = CRGB(0, 0, 0);
        FastLED.show(); 
        fadeall();
          delay(10);
      }
      if(buttonState == LOW)
      {
        for(int i = 0; i < NUM_LEDS; i++)
        leds[i] = CRGB(0, 0, 0);
        FastLED.show(); 
        }
    }
    12298

    hans
    Keymaster

    Hi Robiv8,

    is this related to this comment post?

    If not, what is the exact effect you’re looking for?

    I did go through your code, and noticed a few things. Your code could compile just fine, but it wouldn’t do what you’d expect it to do.
    Granted, I didn’t look at what the code exactly does, I’d need to understand better the effect you’re looking for.
    You’re missing a few accolades ( { and } ). See also “If then loops“, but I’m sure you already get the concept. 

    As you can see below; organizing your code makes it more readable as well. 

    #include <FastLED.h>
    
    #define NUM_LEDS 46 
    #define DATA_PIN 2
    int buttonPin = 4; // the number of the pushbutton pin
    CRGB leds[NUM_LEDS];
    void setup() { 
      LEDS.addLeds<WS2812,DATA_PIN,GRB>(leds,NUM_LEDS);
      LEDS.setBrightness(100);
      pinMode(buttonPin, INPUT);
    }
    void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
    void loop() {
      int buttonState = digitalRead(buttonPin);
      // static uint8_t hue = 0; // <-- does not seem to be used anywhere
      if(buttonState == HIGH) { // <-- missing "{" here
      for(int i = 0; i < NUM_LEDS; i++)
      {
         leds[i] = CRGB(255, 102, 0);
        FastLED.show(); 
        fadeall();
          delay(10);
      }
      for(int i = 0; i < NUM_LEDS; i++)
      {
        leds[i] = CRGB(0, 0, 0);
        FastLED.show(); 
        fadeall();
          delay(10);
      } } // <-- missing "{" here
      else // <-- you can use "else" here (since a button is either LOW or HIGH ;-)
      {
        for(int i = 0; i < NUM_LEDS; i++) { // <-- missing "{" here
        leds[i] = CRGB(0, 0, 0); } // <-- missing "{" here
        FastLED.show(); 
      }
    }
    12300

    robiv8
    Participant

    Hallo Hans,

    I would like to rebuild my Vespa

    see video from min. 01:33 – 01:40

    YouTube Video TurnLight

    12302

    hans
    Keymaster

    I’ll go dig up my Arduino, power supply and LED strips. This effect shouldn’t be too hard. 

    12304

    hans
    Keymaster

    Alrighty, you may like this one … I’ve attached a video of the effect (at 5 seconds, I press the button briefly).

    As usual, you’ll need to:
    – set the number of LEDs (NUM_LEDS 60),
    – pin for the LEDs (PIN 5), 
    – pin for the button (BUTTON 2), and
    – the number of swipes (blinkcount = 5).

    You also may want to experiment with the values I have used in the “delay()” instructions as well (slower/faster/etc).

    CAUTION:
    For the switch you’ll need a slightly different electrical approach – see attached image button.png for the schematics (source).
    Just follow the original schematics, however you need to remove the switch (the yellow wires) and add this instead:

    GND -> 10K Ohm resistor -> pin 2.
    Pin 2
    -> one side of the switch.
    +5V (Arduino)
    -> other side of the switch.

    The color code of the resistors are (see also my resistor calculator):

    470 Ohms = yellow, violet, brown,
    10 Kilo Ohms = brown, black, orange.

    I have also attached the source as a .ino sketch, and I’m assuming you’re using the FastLED library (recommended).
    You may also notice that I simplified the code, by removing anything NeoPixel related.

    What it does: when the button is being pressed, it will do the effect 5 times (= blinkcount).

    #include "FastLED.h"
    #include <EEPROM.h>
    #define NUM_LEDS 60 
    #define blinkcount 5
    CRGB leds[NUM_LEDS];
    #define PIN 6 
    #define BUTTON 2
    byte selectedEffect=0;
    int buttonState = 0;
    void setup()
    {
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
      pinMode(BUTTON, INPUT);
    }
    void loop() { 
      buttonState = digitalRead(BUTTON);
      if (buttonState == HIGH) {
        
        for(int blink=0; blink<blinkcount; blink++) {
          
          for(int i=0; i<NUM_LEDS; i++) {
            leds[i].setRGB(255,50,0);
            FastLED.show();
            delay(10);
          }
          
          delay(200);
           // Cleanup swipe after swipe
          //setAll(0,0,0);
          for(int i=0; i<NUM_LEDS; i++) {
            leds[i].setRGB(0,0,0);
            FastLED.show();
            delay(10);

          }
          
          delay(200);
          
        }
        setAll(0,0,0);
      }
      else {
        setAll(0,0,0);
      }
    }

    // Set all LEDs to a given color and apply it (visible)
    void setAll(byte red, byte green, byte blue) {
      for(int i = 0; i < NUM_LEDS; i++ ) {
        leds[i].setRGB(red, green, blue); 
      }
      FastLED.show();
    }

    In this code it does a cleanup swipe after each swipe – which is something you may want or not.
    If you do not want this, and just want the leds to go dark right away, then change the code to this (compare the bold section in the code to see what and where).

    #include "FastLED.h"
    #include <EEPROM.h>
    #define NUM_LEDS 60 
    #define blinkcount 5
    CRGB leds[NUM_LEDS];
    #define PIN 6 
    #define BUTTON 2
    byte selectedEffect=0;
    int buttonState = 0;
    void setup()
    {
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
      pinMode(BUTTON, INPUT);
    }
    void loop() { 
      buttonState = digitalRead(BUTTON);
      if (buttonState == HIGH) {
        
        for(int blink=0; blink<blinkcount; blink++) {
          
          for(int i=0; i<NUM_LEDS; i++) {
            leds[i].setRGB(255,50,0);
            FastLED.show();
            delay(10);
          }
          
          delay(200);
           // Instant black after swipe
          setAll(0,0,0);
         
    //for(int i=0; i<NUM_LEDS; i++) {
          //  leds[i].setRGB(0,0,0);
          //  FastLED.show();
          //  delay(10);
          //}
          
          delay(200);
          
        }
        setAll(0,0,0);
      }
      else {
        setAll(0,0,0);
      }
    }
    // Set all LEDs to a given color and apply it (visible)
    void setAll(byte red, byte green, byte blue) {
      for(int i = 0; i < NUM_LEDS; i++ ) {
        leds[i].setRGB(red, green, blue); 
      }
      FastLED.show();
    }

    Hope this is what you’re looking for.

    Attachments:
    12311

    hans
    Keymaster
    12313

    robiv8
    Participant

    Woohhooo

    That’s more than I expected.

    Thank you very much

    I’ll test it tonight.

    12317

    robiv8
    Participant

    That’s exactly what I wanted.
    Great support, thank you very much.

    I dare not ask how I can change direction. Now from right to left to left to right.

    12319

    hans
    Keymaster

    Hi Robiv8!

    You’re most welcome! Glad this is what you were looking for.

    To change direction, you’ll just have to flip the direction,
    So these 2 for-loops:

          for(int i=0; i<NUM_LEDS; i++) {
            leds[i].setRGB(255,50,0);
            FastLED.show();
            delay(10);
          }
          
          delay(200);
          
          //setAll(0,0,0); 
          for(int i=0; i<NUM_LEDS; i++) {
          leds[i].setRGB(0,0,0);
          FastLED.show();
          delay(10);
          }

    could become:

          for(int i=0; i<NUM_LEDS; i++) {
            leds[NUM_LEDS-i].setRGB(255,50,0);
            FastLED.show();
            delay(10);
          }
          
          delay(200);
          
          //setAll(0,0,0); 
          for(int i=0; i<NUM_LEDS; i++) {
            leds[NUM_LEDS-i].setRGB(0,0,0);
            FastLED.show();
            delay(10);
          }

    (untested)

    So combined, using 2 buttons (left and right), this may work (again: untested);

    #include "FastLED.h"
    #include <EEPROM.h>
    #define NUM_LEDS 60 
    #define blinkcount 5
    CRGB leds[NUM_LEDS];
    #define PIN 6 
    #define BUTTONLEFT 2
    #define BUTTONRIGHT 3
    byte selectedEffect = 0;
    int buttonStateLeft = 0;
    int buttonStateRight = 0;
    bool AButtonPressed = false;
    void setup()
    {
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
      pinMode(BUTTONLEFT, INPUT);
      pinMode(BUTTONRIGHT, INPUT);
    }
    void loop() { 
      buttonStateLeft = digitalRead(BUTTONLEFT);
      buttonStateRight = digitalRead(BUTTONRIGHT);
      AButtonPressed = (buttonStateLeft == HIGH) || (buttonStateRight == HIGH);
      
      if (AButtonPressed) {
        
        if(buttonStateLeft == HIGH) {
          DirectionCorrection = 0;
        }
        else {
          DirectionCorrection = NUM_LEDS;
        }
        
        for(int blink=0; blink<blinkcount; blink++) {
          
          for(int i=0; i<NUM_LEDS; i++) {
            if(buttonStateLeft == HIGH) {
              leds[i].setRGB(255,50,0);
            } else {
              leds[NUM_LEDS-i].setRGB(255,50,0);
            }
            FastLED.show();
            delay(10);
          }
          
          delay(200);
          
          // Cleanup swipe after swipe
          //setAll(0,0,0);
          for(int i=0; i<NUM_LEDS; i++) {
            if(buttonStateLeft == HIGH) {
              leds[i].setRGB(0,0,0);
            } else {
              leds[NUM_LEDS-i].setRGB(0,0,0);
            }
            FastLED.show();
            delay(10);
          }
          
          delay(200);
          
        }
        setAll(0,0,0);
      }
      else {
        setAll(0,0,0);
      }
    }
    // Set all LEDs to a given color and apply it (visible)
    void setAll(byte red, byte green, byte blue) {
      for(int i = 0; i < NUM_LEDS; i++ ) {
        leds[i].setRGB(red, green, blue); 
      }
      FastLED.show();
    }

    Hope this helps … and if you’re done with your Vespa, please feel free to share the end result here 

    12321

    robiv8
    Participant

    Many Thanks
    ‘DirectionCorrection’ was not declared in this scope ?


    Oh, I found it myself.
    I added that: bool DirectionCorrection = false;
    and it does.
    Thanks again
    • This reply was modified 2 months, 1 week ago by  robiv8.
    • This reply was modified 2 months, 1 week ago by  robiv8.
    12324

    robiv8
    Participant

    So here is the finished result as I wanted it.

    Perfect

    #define FASTLED_INTERNAL
    #include "FastLED.h"
    #include <EEPROM.h>
    #define NUM_LEDS 36 
    #define blinkcount 3
    CRGB leds[NUM_LEDS];
    #define PIN 2 
    #define BUTTONLEFT 4
    #define BUTTONRIGHT 5
    byte selectedEffect = 0;
    int buttonStateLeft = 0;
    int buttonStateRight = 0;
    bool AButtonPressed = false;
    bool DirectionCorrection = false;
    void setup()
    {
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
      pinMode(BUTTONLEFT, INPUT);
      pinMode(BUTTONRIGHT, INPUT);
      LEDS.setBrightness(200);
    }
    void loop() { 
      buttonStateLeft = digitalRead(BUTTONLEFT);
      buttonStateRight = digitalRead(BUTTONRIGHT);
      AButtonPressed = (buttonStateLeft == HIGH) || (buttonStateRight == HIGH);
      
      if (AButtonPressed) {
        if(buttonStateLeft == HIGH) {
          DirectionCorrection = 0;
        }
        else {
          DirectionCorrection = NUM_LEDS;
        }
        for(int blink=0; blink<blinkcount; blink++) {
          
          for(int i=0; i<NUM_LEDS; i++) {
            if(buttonStateLeft == HIGH) {
              leds[i].setRGB(255,130,0);
            } else {
              leds[NUM_LEDS-i].setRGB(255,130,0);
            }
            FastLED.show();
            delay(10);
          }
          delay(200);
          
          // Instant black after swipe
          setAll(0,0,0);
          delay(200);
        }
        setAll(0,0,0);
      }
      else {
        setAll(0,0,0);
      }
    }
    // Set all LEDs to a given color and apply it (visible)
    void setAll(byte red, byte green, byte blue) {
      for(int i = 0; i < NUM_LEDS; i++ ) {
        leds[i].setRGB(red, green, blue); 
      }
      FastLED.show();
    }
    12327

    hans
    Keymaster

    Awesome! Glad this worked out for you! 

    And if you ever make a little video of your Vespa; please let me know and/or post it here as well! 

    12368

    robiv8
    Participant

    First of all a video how it works and looks.

    72 LEDs each 36 for left and 36 for right.

    Have it changed for two turn signals with an Arduino Nano.

    Once I am in Croatia I will install it.

    Thanks again for your help.

    YT Video

    12370

    robiv8
    Participant

    And here on GitHub

    Robiv8/TurnSignal-WS2812B

    12372

    hans
    Keymaster

    Thanks Robiv8!!

    Looks great! Well done! I’m really liking this and curious what it will look like on your Vespa 

    I encourage other readers to visit the Github link you provided (link).

    Would you mind if I post the picture and the sketch here as well?

Viewing 15 posts - 1 through 15 (of 17 total)



You must be logged in to reply to this topic.