Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: midwesta on Jul 17, 2017, 12:37 am

Title: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 17, 2017, 12:37 am
Hello I am trying to use WS2812B LED's to make brake lights for my motorcycle.

I have written a program that lights up an 8x8 panel from the center to the outside when I press button3.

Button 1 and 2 are left and right blinkers so it is okay if they loop.

The problem is the program loops and keeps lighting from the inside out over and over. I want this to happen once and then stay lit until i release the button. What am I doing wrong?

Bonus:

*how do I control the brightness of the LED's using fastLED??
*how do I group LED's - like ROW1 = LED 0-7, ROW2= 8-15... and so on... ?
Title: Re: Running Loop once and leaving LED lit until button released
Post by: Wawa on Jul 17, 2017, 04:48 am
Use a state machine.

Add a variable   oldButtonState = 0;.

if(buttonState1 == HIGH && oldButtonState == LOW) {
  // "braking" code here
  oldButtonState = HIGH; // so this block can only run once
  }

if(buttonState1 == LOW && oldButtonState == HIGH) {
  // "stop braking" code here
  oldButtonState == LOW;
  }

Leo..
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 18, 2017, 04:51 am
Okay thanks Leo - I got it to work

but now - I set my stop braking code to be the LED at a lower brightness - which will be my running tail light - so now at the beginning of each code I set all the LED to black so the blinker or stop light can be seen easier.

This screwed up my brake light - it is looping again - if i remove the set LED to black coding from the beginning it works good again - why would the black coding do this?
Title: Re: Running Loop once and leaving LED lit until button released
Post by: Grumpy_Mike on Jul 18, 2017, 09:36 pm
Quote
why would the black coding do this?
Line 32 is wrong, mind you this crystal ball is sometimes not very reliable so you better post your code so we can check.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 19, 2017, 12:50 am
Code: [Select]

#include <bitswap.h>
#include <chipsets.h>
#include <color.h>
#include <colorpalettes.h>
#include <colorutils.h>
#include <controller.h>
#include <cpp_compat.h>
#include <dmx.h>
#include <FastLED.h>
#include <fastled_config.h>
#include <fastled_delay.h>
#include <fastled_progmem.h>
#include <fastpin.h>
#include <fastspi.h>
#include <fastspi_bitbang.h>
#include <fastspi_dma.h>
#include <fastspi_nop.h>
#include <fastspi_ref.h>
#include <fastspi_types.h>
#include <hsv2rgb.h>
#include <led_sysdefs.h>
#include <lib8tion.h>
#include <noise.h>
#include <pixelset.h>
#include <pixeltypes.h>
#include <platforms.h>
#include <power_mgt.h>

#include "FastLED.h"

// fast led constants
#define DATA_PIN    3        // change to your data pin
#define COLOR_ORDER GRB      // if colors are mismatched; change this
#define NUM_LEDS    256          // change to the number of LEDs in your strip
#define FRAMES_PER_SECOND  120



// change WS2812B to match your type of LED, if different
// list of supported types is here:
// https://github.com/FastLED/FastLED/wiki/Overview

#define LED_TYPE    WS2812B
#define BRIGHTNESS          200
#define DIM         10
#define ALL 0-255


const int buttonPin1 = 5;     // the number of the pushbutton pin
const int buttonPin2 = 6;
const int buttonPin3 = 7;
const int buttonPin4 = 10;

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int oldButtonState3 = 0;


// this creates an LED array to hold the values for each led in your strip
CRGB leds[NUM_LEDS];




void setup()
{
    delay(3000); // 3 second delay for recovery
  
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  // the wiki features a much more basic setup line:
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);

Serial.begin(9600);

// initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
  
  // List of patterns to cycle through.  Each is defined as a separate function below.
typedef void (*SimplePatternList[])();


uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
}

 void loop()
{  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
  

  if(buttonState3 == HIGH && oldButtonState3 == LOW){

 


  // left tail light black
  
      leds[0] = CRGB::Black;
      leds[1] = CRGB::Black;
      leds[2] = CRGB::Black;
      leds[3] = CRGB::Black;
      leds[4] = CRGB::Black;
      leds[5] = CRGB::Black;
      leds[6] = CRGB::Black;
      leds[7] = CRGB::Black;
      leds[8] = CRGB::Black;
      leds[9] = CRGB::Black;
      leds[10] = CRGB::Black;
      leds[11] = CRGB::Black;
      leds[12] = CRGB::Black;
      leds[13] = CRGB::Black;
      leds[14] = CRGB::Black;
      leds[15] = CRGB::Black;
      leds[16] = CRGB::Black;
      leds[17] = CRGB::Black;
      leds[18] = CRGB::Black;
      leds[19] = CRGB::Black;
      leds[20] = CRGB::Black;
      leds[21] = CRGB::Black;
      leds[22] = CRGB::Black;
      leds[23] = CRGB::Black;
      leds[24] = CRGB::Black;
      leds[25] = CRGB::Black;
      leds[26] = CRGB::Black;
      leds[27] = CRGB::Black;
      leds[28] = CRGB::Black;
      leds[29] = CRGB::Black;
      leds[30] = CRGB::Black;
      leds[31] = CRGB::Black;
      leds[32] = CRGB::Black;
      leds[33] = CRGB::Black;
      leds[34] = CRGB::Black;
      leds[35] = CRGB::Black;
      leds[36] = CRGB::Black;
      leds[37] = CRGB::Black;
      leds[38] = CRGB::Black;
      leds[39] = CRGB::Black;
      leds[40] = CRGB::Black;
      leds[41] = CRGB::Black;
      leds[42] = CRGB::Black;
      leds[43] = CRGB::Black;
      leds[44] = CRGB::Black;
      leds[45] = CRGB::Black;
      leds[46] = CRGB::Black;
      leds[47] = CRGB::Black;
      leds[48] = CRGB::Black;
      leds[49] = CRGB::Black;
      leds[50] = CRGB::Black;
      leds[51] = CRGB::Black;
      leds[52] = CRGB::Black;
      leds[53] = CRGB::Black;
      leds[54] = CRGB::Black;
      leds[55] = CRGB::Black;
      leds[56] = CRGB::Black;
      leds[57] = CRGB::Black;
      leds[58] = CRGB::Black;
      leds[59] = CRGB::Black;
      leds[60] = CRGB::Black;
      leds[61] = CRGB::Black;
      leds[62] = CRGB::Black;
      leds[63] = CRGB::Black;


      // right tail light black

      leds[192] = CRGB::Black;
      leds[193] = CRGB::Black;
      leds[194] = CRGB::Black;
      leds[195] = CRGB::Black;
      leds[196] = CRGB::Black;
      leds[197] = CRGB::Black;
      leds[198] = CRGB::Black;
      leds[199] = CRGB::Black;
      leds[200] = CRGB::Black;
      leds[201] = CRGB::Black;
      leds[202] = CRGB::Black;
      leds[203] = CRGB::Black;
      leds[204] = CRGB::Black;
      leds[205] = CRGB::Black;
      leds[206] = CRGB::Black;
      leds[207] = CRGB::Black;
      leds[208] = CRGB::Black;
      leds[209] = CRGB::Black;
      leds[210] = CRGB::Black;
      leds[211] = CRGB::Black;
      leds[212] = CRGB::Black;
      leds[213] = CRGB::Black;
      leds[214] = CRGB::Black;
      leds[215] = CRGB::Black;
      leds[216] = CRGB::Black;
      leds[217] = CRGB::Black;
      leds[218] = CRGB::Black;
      leds[219] = CRGB::Black;
      leds[220] = CRGB::Black;
      leds[221] = CRGB::Black;
      leds[222] = CRGB::Black;
      leds[223] = CRGB::Black;
      leds[224] = CRGB::Black;
      leds[225] = CRGB::Black;
      leds[226] = CRGB::Black;
      leds[227] = CRGB::Black;
      leds[228] = CRGB::Black;
      leds[229] = CRGB::Black;
      leds[230] = CRGB::Black;
      leds[231] = CRGB::Black;
      leds[232] = CRGB::Black;
      leds[233] = CRGB::Black;
      leds[234] = CRGB::Black;
      leds[235] = CRGB::Black;
      leds[236] = CRGB::Black;
      leds[237] = CRGB::Black;
      leds[238] = CRGB::Black;
      leds[239] = CRGB::Black;
      leds[240] = CRGB::Black;
      leds[241] = CRGB::Black;
      leds[242] = CRGB::Black;
      leds[243] = CRGB::Black;
      leds[244] = CRGB::Black;
      leds[245] = CRGB::Black;
      leds[246] = CRGB::Black;
      leds[247] = CRGB::Black;
      leds[248] = CRGB::Black;
      leds[249] = CRGB::Black;
      leds[250] = CRGB::Black;
      leds[251] = CRGB::Black;
      leds[252] = CRGB::Black;
      leds[253] = CRGB::Black;
      leds[254] = CRGB::Black;
      leds[255] = CRGB::Black;

  


[/code :) ]
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 19, 2017, 12:55 am
Code: [Select]



        // Turn the first led red for 1 second   leds[i] %= 192;
      leds[36]  = CRGB::Red;
       leds[35] = CRGB::Red;
       leds[28] = CRGB::Red;
       leds[27] = CRGB::Red;

       // left brake light
        leds[227] = CRGB::Red;
       leds[228] = CRGB::Red;
       leds[219] = CRGB::Red;
       leds[220] = CRGB::Red;









      
             FastLED.show();
      delay(200);

        // Turn the first led red for 1 second
      leds[42] = CRGB::Red;
       leds[43] = CRGB::Red;
       leds[44] = CRGB::Red;
       leds[45] = CRGB::Red;
       leds[37] = CRGB::Red;
       leds[29] = CRGB::Red;
       leds[21] = CRGB::Red;
       leds[20] = CRGB::Red;
        leds[19] = CRGB::Red;
       leds[18] = CRGB::Red;
       leds[26] = CRGB::Red;
        leds[34] = CRGB::Red;

        // left brake light

        leds[234] = CRGB::Red;
       leds[235] = CRGB::Red;
       leds[236] = CRGB::Red;
       leds[237] = CRGB::Red;
       leds[226] = CRGB::Red;
       leds[221] = CRGB::Red;
       leds[210] = CRGB::Red;
       leds[211] = CRGB::Red;
        leds[212] = CRGB::Red;
       leds[213] = CRGB::Red;
       leds[218] = CRGB::Red;
        leds[229] = CRGB::Red;


        
             FastLED.show();
      delay(200);
      
  

  // Turn the first led red for 1 second
    
       leds[49] = CRGB::Red;
       leds[50] = CRGB::Red;
       leds[51] = CRGB::Red;
       leds[52] = CRGB::Red;
       leds[53] = CRGB::Red;
       leds[46] = CRGB::Red;
       leds[38] = CRGB::Red;
       leds[30] = CRGB::Red;
       leds[22] = CRGB::Red;
       leds[14] = CRGB::Red;
       leds[13] = CRGB::Red;
       leds[12] = CRGB::Red;
       leds[11] = CRGB::Red;
       leds[10] = CRGB::Red;
       leds[9] = CRGB::Red;
       leds[17] = CRGB::Red;
       leds[25] = CRGB::Red;
       leds[33] = CRGB::Red;
       leds[41] = CRGB::Red;
       leds[54] = CRGB::Red;


       // left brake light

       leds[241] = CRGB::Red;
       leds[242] = CRGB::Red;
       leds[243] = CRGB::Red;
       leds[244] = CRGB::Red;
       leds[245] = CRGB::Red;
       leds[246] = CRGB::Red;
       leds[233] = CRGB::Red;
       leds[230] = CRGB::Red;
       leds[217] = CRGB::Red;
       leds[214] = CRGB::Red;
       leds[201] = CRGB::Red;
       leds[202] = CRGB::Red;
       leds[203] = CRGB::Red;
       leds[204] = CRGB::Red;
       leds[205] = CRGB::Red;
       leds[206] = CRGB::Red;
       leds[209] = CRGB::Red;
       leds[222] = CRGB::Red;
       leds[225] = CRGB::Red;
       leds[238] = CRGB::Red;
      
       FastLED.show();
      delay(200);
      


        // Turn the first led red for 1 second
      leds[63] = CRGB::Red;
       leds[62] = CRGB::Red;
       leds[61] = CRGB::Red;
       leds[60] = CRGB::Red;
       leds[59] = CRGB::Red;
       leds[58] = CRGB::Red;
       leds[57] = CRGB::Red;
       leds[56] = CRGB::Red;
       leds[55] = CRGB::Red;
       leds[47] = CRGB::Red;
       leds[39] = CRGB::Red;
       leds[31] = CRGB::Red;
       leds[23] = CRGB::Red;
       leds[15] = CRGB::Red;
       leds[7] = CRGB::Red;
       leds[6] = CRGB::Red;
       leds[5] = CRGB::Red;
       leds[4] = CRGB::Red;
       leds[3] = CRGB::Red;
       leds[2] = CRGB::Red;
       leds[1] = CRGB::Red;
       leds[8] = CRGB::Red;
       leds[16] = CRGB::Red;
       leds[24] = CRGB::Red;
       leds[32] = CRGB::Red;
       leds[40] = CRGB::Red;
       leds[48] = CRGB::Red;
       leds[0] = CRGB::Red;

       // left brake light

       leds[255] = CRGB::Red;
       leds[254] = CRGB::Red;
       leds[253] = CRGB::Red;
       leds[252] = CRGB::Red;
       leds[251] = CRGB::Red;
       leds[250] = CRGB::Red;
       leds[249] = CRGB::Red;
       leds[248] = CRGB::Red;
       leds[247] = CRGB::Red;
       leds[232] = CRGB::Red;
       leds[231] = CRGB::Red;
       leds[216] = CRGB::Red;
       leds[215] = CRGB::Red;
       leds[200] = CRGB::Red;
       leds[199] = CRGB::Red;
       leds[198] = CRGB::Red;
       leds[197] = CRGB::Red;
       leds[196] = CRGB::Red;
       leds[195] = CRGB::Red;
       leds[194] = CRGB::Red;
       leds[193] = CRGB::Red;
       leds[192] = CRGB::Red;
       leds[207] = CRGB::Red;
       leds[208] = CRGB::Red;
       leds[223] = CRGB::Red;
       leds[224] = CRGB::Red;
       leds[239] = CRGB::Red;
       leds[240] = CRGB::Red;
       FastLED.show();
      delay(30);

  
oldButtonState3 == HIGH; // so this block can only run once

  }
//(buttonState3 == LOW && oldButtonState3 == HIGH)
  
 if(buttonState3 == LOW) {
  // "stop braking" code here
   // turn LED off:
  
FastLED.setBrightness(DIM);
      
      // Set the first led back to black for 1 second
      leds[0] = CRGB::Red;
      leds[1] = CRGB::Red;
      leds[2] = CRGB::Red;
      leds[3] = CRGB::Red;
      leds[4] = CRGB::Red;
      leds[5] = CRGB::Red;
      leds[6] = CRGB::Red;
      leds[7] = CRGB::Red;
      leds[8] = CRGB::Red;
      leds[9] = CRGB::Red;
      leds[10] = CRGB::Red;
      leds[11] = CRGB::Red;
      leds[12] = CRGB::Red;
      leds[13] = CRGB::Red;
      leds[14] = CRGB::Red;
      leds[15] = CRGB::Red;
      leds[16] = CRGB::Red;
      leds[17] = CRGB::Red;
      leds[18] = CRGB::Red;
      leds[19] = CRGB::Red;
      leds[20] = CRGB::Red;
      leds[21] = CRGB::Red;
      leds[22] = CRGB::Red;
      leds[23] = CRGB::Red;
      leds[24] = CRGB::Red;
      leds[25] = CRGB::Red;
      leds[26] = CRGB::Red;
      leds[27] = CRGB::Red;
      leds[28] = CRGB::Red;
      leds[29] = CRGB::Red;
      leds[30] = CRGB::Red;
      leds[31] = CRGB::Red;
      leds[32] = CRGB::Red;
      leds[33] = CRGB::Red;
      leds[34] = CRGB::Red;
      leds[35] = CRGB::Red;
      leds[36] = CRGB::Red;
      leds[37] = CRGB::Red;
      leds[38] = CRGB::Red;
      leds[39] = CRGB::Red;
      leds[40] = CRGB::Red;
      leds[41] = CRGB::Red;
      leds[42] = CRGB::Red;
      leds[43] = CRGB::Red;
      leds[44] = CRGB::Red;
      leds[45] = CRGB::Red;
      leds[46] = CRGB::Red;
      leds[47] = CRGB::Red;
      leds[48] = CRGB::Red;
      leds[49] = CRGB::Red;
      leds[50] = CRGB::Red;
      leds[51] = CRGB::Red;
      leds[52] = CRGB::Red;
      leds[53] = CRGB::Red;
      leds[54] = CRGB::Red;
      leds[55] = CRGB::Red;
      leds[56] = CRGB::Red;
      leds[57] = CRGB::Red;
      leds[58] = CRGB::Red;
      leds[59] = CRGB::Red;
      leds[60] = CRGB::Red;
      leds[61] = CRGB::Red;
      leds[62] = CRGB::Red;
      leds[63] = CRGB::Red;


      // left tail light black

      leds[192] = CRGB::Red;
      leds[193] = CRGB::Red;
      leds[194] = CRGB::Red;
      leds[195] = CRGB::Red;
      leds[196] = CRGB::Red;
      leds[197] = CRGB::Red;
      leds[198] = CRGB::Red;
      leds[199] = CRGB::Red;
      leds[200] = CRGB::Red;
      leds[201] = CRGB::Red;
      leds[202] = CRGB::Red;
      leds[203] = CRGB::Red;
      leds[204] = CRGB::Red;
      leds[205] = CRGB::Red;
      leds[206] = CRGB::Red;
      leds[207] = CRGB::Red;
      leds[208] = CRGB::Red;
      leds[209] = CRGB::Red;
      leds[210] = CRGB::Red;
      leds[211] = CRGB::Red;
      leds[212] = CRGB::Red;
      leds[213] = CRGB::Red;
      leds[214] = CRGB::Red;
      leds[215] = CRGB::Red;
      leds[216] = CRGB::Red;
      leds[217] = CRGB::Red;
      leds[218] = CRGB::Red;
      leds[219] = CRGB::Red;
      leds[220] = CRGB::Red;
      leds[221] = CRGB::Red;
      leds[222] = CRGB::Red;
      leds[223] = CRGB::Red;
      leds[224] = CRGB::Red;
      leds[225] = CRGB::Red;
      leds[226] = CRGB::Red;
      leds[227] = CRGB::Red;
      leds[228] = CRGB::Red;
      leds[229] = CRGB::Red;
      leds[230] = CRGB::Red;
      leds[231] = CRGB::Red;
      leds[232] = CRGB::Red;
      leds[233] = CRGB::Red;
      leds[234] = CRGB::Red;
      leds[235] = CRGB::Red;
      leds[236] = CRGB::Red;
      leds[237] = CRGB::Red;
      leds[238] = CRGB::Red;
      leds[239] = CRGB::Red;
      leds[240] = CRGB::Red;
      leds[241] = CRGB::Red;
      leds[242] = CRGB::Red;
      leds[243] = CRGB::Red;
      leds[244] = CRGB::Red;
      leds[245] = CRGB::Red;
      leds[246] = CRGB::Red;
      leds[247] = CRGB::Red;
      leds[248] = CRGB::Red;
      leds[249] = CRGB::Red;
      leds[250] = CRGB::Red;
      leds[251] = CRGB::Red;
      leds[252] = CRGB::Red;
      leds[253] = CRGB::Red;
      leds[254] = CRGB::Red;
      leds[255] = CRGB::Red;

      FastLED.show();

      delay(30);
      
      oldButtonState3 == LOW;

      FastLED.setBrightness(BRIGHTNESS);


  }
  
  }

Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 19, 2017, 01:29 am
Okay I couldn't post the whole code because only 9k character limit so it is in two parts

It keeps running the loop when I have all the black coding at the front.

It works fine when the black coding is not there.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: PaulRB on Jul 19, 2017, 07:37 am
Please edit your posts and change them to use code tags for the code. It will then appear in a reasonable size box with a scroll bar. It will still need to be across two posts, but I'm sure we can soon show you how to shrink it down to a reasonable size, and help with your original question.

For example
Code: [Select]
       // left brake light

        leds[234] = CRGB::Red;
       leds[235] = CRGB::Red;
       leds[236] = CRGB::Red;
       leds[237] = CRGB::Red;
       leds[226] = CRGB::Red;
       leds[221] = CRGB::Red;
       leds[210] = CRGB::Red;
       leds[211] = CRGB::Red;
        leds[212] = CRGB::Red;
       leds[213] = CRGB::Red;
       leds[218] = CRGB::Red;
        leds[229] = CRGB::Red;

becomes
Code: [Select]
       // left brake light

        for (int i = 234; i <= 229; i++) leds[i] = CRGB::Red;
Title: Re: Running Loop once and leaving LED lit until button released
Post by: Grumpy_Mike on Jul 19, 2017, 09:05 am
Also do you rely need all those libraries? I can't see any of them being used apart from the Neopixel one.

The problem is all that superfluous verbiage makes it hard to see the actual code structure.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: INTP on Jul 19, 2017, 10:08 am
ho ly sh . . . .

Code: [Select]

     leds[0] = CRGB::Red;
.
.
.
      leds[63] = CRGB::Red;



those 64 lines can be reduced to

Code: [Select]

for (int z = 0; z < 64; z++;) {
 leds[z] = CRGB::Red;
}


Same goes for all of those other monstrosities. No wonder your code is long.

And second that with 'wth is with all of those libraries', please explain your setup, what are all the things you are having the Arduino do.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 19, 2017, 03:24 pm
I was trying to figure out how to group the LED's! Okay I will clean up the code and re-post!

Thanks for the help so far this is HUGE (pun intended haha)
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 19, 2017, 04:45 pm
Code: [Select]


#include "FastLED.h"

// fast led constants
#define DATA_PIN    3        // change to your data pin
#define COLOR_ORDER GRB      // if colors are mismatched; change this
#define NUM_LEDS    256          // change to the number of LEDs in your strip
#define FRAMES_PER_SECOND  120
#define LED_TYPE    WS2812B
#define BRIGHTNESS          200
#define DIM         10
#define ALL 0-255

const int buttonPin1 = 5;     // the number of the pushbutton pin
const int buttonPin2 = 6;
const int buttonPin3 = 7;
const int buttonPin4 = 10;

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int oldButtonState3 = 0;


// this creates an LED array to hold the values for each led in your strip
CRGB leds[NUM_LEDS];


void setup()
{
    delay(3000); // 3 second delay for recovery
 
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  // the wiki features a much more basic setup line:
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);

Serial.begin(9600);

// initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
}

 void loop()
{  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);
 

  if(buttonState3 == HIGH && oldButtonState3 == LOW){

  // turn tail lights black quickly

for (int z = 0; z < 64; z++;) {
 leds[z] = CRGB::Black;
    for (int b = 192; z < 256; z++;) {
 leds[b] = CRGB::Black;
      FastLED.show();
      delay(200);

  // Turn the center 4 led red for 1 second
      leds[36]  = CRGB::Red;
       leds[35] = CRGB::Red;
       leds[28] = CRGB::Red;
       leds[27] = CRGB::Red;
//left brake light
        leds[227] = CRGB::Red;
       leds[228] = CRGB::Red;
       leds[219] = CRGB::Red;
       leds[220] = CRGB::Red;
      FastLED.show();
      delay(200);

        // Turn the next ring led red for 1 second
      leds[42] = CRGB::Red;
       leds[43] = CRGB::Red;
       leds[44] = CRGB::Red;
       leds[45] = CRGB::Red;
       leds[37] = CRGB::Red;
       leds[29] = CRGB::Red;
       leds[21] = CRGB::Red;
       leds[20] = CRGB::Red;
        leds[19] = CRGB::Red;
       leds[18] = CRGB::Red;
       leds[26] = CRGB::Red;
        leds[34] = CRGB::Red;

        // left brake light

        leds[234] = CRGB::Red;
       leds[235] = CRGB::Red;
       leds[236] = CRGB::Red;
       leds[237] = CRGB::Red;
       leds[226] = CRGB::Red;
       leds[221] = CRGB::Red;
       leds[210] = CRGB::Red;
       leds[211] = CRGB::Red;
        leds[212] = CRGB::Red;
       leds[213] = CRGB::Red;
       leds[218] = CRGB::Red;
        leds[229] = CRGB::Red;
        FastLED.show();
       delay(200);
     
 

  // Turn the third ring led red for 1 second
     
       leds[49] = CRGB::Red;
       leds[50] = CRGB::Red;
       leds[51] = CRGB::Red;
       leds[52] = CRGB::Red;
       leds[53] = CRGB::Red;
       leds[46] = CRGB::Red;
       leds[38] = CRGB::Red;
       leds[30] = CRGB::Red;
       leds[22] = CRGB::Red;
       leds[14] = CRGB::Red;
       leds[13] = CRGB::Red;
       leds[12] = CRGB::Red;
       leds[11] = CRGB::Red;
       leds[10] = CRGB::Red;
       leds[9] = CRGB::Red;
       leds[17] = CRGB::Red;
       leds[25] = CRGB::Red;
       leds[33] = CRGB::Red;
       leds[41] = CRGB::Red;
       leds[54] = CRGB::Red;

       // left brake light
       leds[241] = CRGB::Red;
       leds[242] = CRGB::Red;
       leds[243] = CRGB::Red;
       leds[244] = CRGB::Red;
       leds[245] = CRGB::Red;
       leds[246] = CRGB::Red;
       leds[233] = CRGB::Red;
       leds[230] = CRGB::Red;
       leds[217] = CRGB::Red;
       leds[214] = CRGB::Red;
       leds[201] = CRGB::Red;
       leds[202] = CRGB::Red;
       leds[203] = CRGB::Red;
       leds[204] = CRGB::Red;
       leds[205] = CRGB::Red;
       leds[206] = CRGB::Red;
       leds[209] = CRGB::Red;
       leds[222] = CRGB::Red;
       leds[225] = CRGB::Red;
       leds[238] = CRGB::Red;
       FastLED.show();
      delay(200);
     
        // Turn the fourth ring red for 1 second
      leds[63] = CRGB::Red;
       leds[62] = CRGB::Red;
       leds[61] = CRGB::Red;
       leds[60] = CRGB::Red;
       leds[59] = CRGB::Red;
       leds[58] = CRGB::Red;
       leds[57] = CRGB::Red;
       leds[56] = CRGB::Red;
       leds[55] = CRGB::Red;
       leds[47] = CRGB::Red;
       leds[39] = CRGB::Red;
       leds[31] = CRGB::Red;
       leds[23] = CRGB::Red;
       leds[15] = CRGB::Red;
       leds[7] = CRGB::Red;
       leds[6] = CRGB::Red;
       leds[5] = CRGB::Red;
       leds[4] = CRGB::Red;
       leds[3] = CRGB::Red;
       leds[2] = CRGB::Red;
       leds[1] = CRGB::Red;
       leds[8] = CRGB::Red;
       leds[16] = CRGB::Red;
       leds[24] = CRGB::Red;
       leds[32] = CRGB::Red;
       leds[40] = CRGB::Red;
       leds[48] = CRGB::Red;
       leds[0] = CRGB::Red;

       // left brake light
       leds[255] = CRGB::Red;
       leds[254] = CRGB::Red;
       leds[253] = CRGB::Red;
       leds[252] = CRGB::Red;
       leds[251] = CRGB::Red;
       leds[250] = CRGB::Red;
       leds[249] = CRGB::Red;
       leds[248] = CRGB::Red;
       leds[247] = CRGB::Red;
       leds[232] = CRGB::Red;
       leds[231] = CRGB::Red;
       leds[216] = CRGB::Red;
       leds[215] = CRGB::Red;
       leds[200] = CRGB::Red;
       leds[199] = CRGB::Red;
       leds[198] = CRGB::Red;
       leds[197] = CRGB::Red;
       leds[196] = CRGB::Red;
       leds[195] = CRGB::Red;
       leds[194] = CRGB::Red;
       leds[193] = CRGB::Red;
       leds[192] = CRGB::Red;
       leds[207] = CRGB::Red;
       leds[208] = CRGB::Red;
       leds[223] = CRGB::Red;
       leds[224] = CRGB::Red;
       leds[239] = CRGB::Red;
       leds[240] = CRGB::Red;
       FastLED.show();
      delay(30);

   
oldButtonState3 == HIGH; // so this block can only run once

  }
 
 if(buttonState3 == LOW) {
  // "stop braking" code here
 
        // turn both tail light dim red
FastLED.setBrightness(DIM);
 
      leds[b] = CRGB::Red;
      leds[z] = CRGB::Red;     
FastLED.show();
  delay(30);
     
      oldButtonState3 == LOW;

      FastLED.setBrightness(BRIGHTNESS);


  }
 
  }


Title: Re: Running Loop once and leaving LED lit until button released
Post by: Grumpy_Mike on Jul 19, 2017, 05:32 pm
Better but still away to go.
When you have a load of LEDs that do not have consecutive numbers you use an other array as a list or look up table of these array numbers. So for example when you want to turn on the center four lights you first define the array that contains the numbers and then use the array contents as the LED array you want to change.

Code: [Select]

// outside of a function define:-
byte center4 = {36,35,28,27};

// then inside a function use
for (int b = 0; b<4; b++;) {
 leds[center4[b]] = CRGB::RED;
}

That will help a lot the longer the list goes on.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 19, 2017, 06:48 pm
Can you explain why you need both center4 and b? Seems like center4 should already call out those numbers?

Also what does b++ mean?
Title: Re: Running Loop once and leaving LED lit until button released
Post by: INTP on Jul 19, 2017, 07:06 pm
Read up and watch some videos about 'for loops'. You need to understand it. You also seem to be missing a few closing brackets }
Title: Re: Running Loop once and leaving LED lit until button released
Post by: Grumpy_Mike on Jul 19, 2017, 09:06 pm
Can you explain why you need both center4 and b? Seems like center4 should already call out those numbers?
The variable center4 is an array, in C you need to access each element one at a time by using a sutiable index given in the square braces.

If you just used b then you would turn on just LEDs 0 to 3. As center4 is an array then the first element of that array returns 36. So center4[0] returns 36, that is the number of LED you want to turn on. The variable b is called the loop index that goes from zero to less than four which is 3.

Quote
Also what does b++ mean?
The operation ++ adds one to the variable it is placed next to. So b++ means what ever the value of b was, add one to it.


Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 20, 2017, 04:27 am
So here is a shorter version that works except:

The brake code is looping - I want button three to do the code once and stay bright until I release.

The blinkers are working good - EXCEPT I would like the non blinking light to stay at the 10 brightness (DIM)

Code: [Select]


#include "FastLED.h"

// fast led constants
#define DATA_PIN    3        // change to your data pin
#define COLOR_ORDER GRB      // if colors are mismatched; change this
#define NUM_LEDS    256          // change to the number of LEDs in your strip
#define FRAMES_PER_SECOND  120
#define LED_TYPE    WS2812B
#define BRIGHTNESS          200
#define DIM         10

// constant variables
const int buttonPin1 = 5;     // the number of the pushbutton pin
const int buttonPin2 = 6;
const int buttonPin3 = 7;
const int buttonPin4 = 10;

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int oldButtonState3 = 0;

// this creates an LED array to hold the values for each led in your strip
CRGB leds[NUM_LEDS];

void setup()
{
    delay(3000); // 3 second delay for recovery
  
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  // the wiki features a much more basic setup line:
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);

Serial.begin(9600);

// initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
}

 void loop()
{  
      FastLED.setBrightness(DIM);
      
// Set the right tail light to black for 1 second
      for (int i = 0; i <= 63; i++)
              leds[i] = CRGB::Red;

// Set the left tail light to black for 1 second
      for (int j = 192; j <= 255; j++)
             leds[j] = CRGB::Red;
              FastLED.show();
  
// read the state of the pushbutton value:
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
    buttonState3 = digitalRead(buttonPin3);
    buttonState4 = digitalRead(buttonPin4);
  




 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
          if (buttonState1 == HIGH) {


//LEFT TURN SIGNAL



// left tail light black
        for (int j = 192; j <= 255; j++)
              leds[j] = CRGB::Black;


        FastLED.setBrightness(BRIGHTNESS);
// Turn the first led red

        for (int a = 192; a <= 220; a++)
        leds[a] = CRGB::Red;
        FastLED.show();
        delay(100);
      
// Set the first led back to black
        for (int a = 192; a <= 220; a++)
        leds[a] = CRGB::Black;
        FastLED.show();
        delay(30);

// Turn the first led red for 1 second
       leds[207] = CRGB::Red;
       leds[209] = CRGB::Red;
       leds[221] = CRGB::Red;
       leds[227] = CRGB::Red;
       leds[228] = CRGB::Red;
       leds[218] = CRGB::Red;
       leds[214] = CRGB::Red;
       leds[200] = CRGB::Red;
       FastLED.show();
       delay(100);
      
// Set the first led back to black for 1 second
      leds[207] = CRGB::Black;
      leds[209] = CRGB::Black;
      leds[221] = CRGB::Black;
      leds[227] = CRGB::Black;
      leds[228] = CRGB::Black;
      leds[218] = CRGB::Black;
      leds[214] = CRGB::Black;
      leds[200] = CRGB::Black;
      FastLED.show();
      delay(30);

// Turn the first led red for 1 second
       leds[208] = CRGB::Red;
       leds[222] = CRGB::Red;
       leds[226] = CRGB::Red;
       leds[236] = CRGB::Red;
       leds[235] = CRGB::Red;
       leds[229] = CRGB::Red;
       leds[217] = CRGB::Red;
       leds[215] = CRGB::Red;
       FastLED.show();
        delay(100);
      
// Set the first led back to black for 1 second
      leds[208] = CRGB::Black;
      leds[222] = CRGB::Black;
      leds[226] = CRGB::Black;
      leds[236] = CRGB::Black;
      leds[235] = CRGB::Black;
      leds[229] = CRGB::Black;
      leds[217] = CRGB::Black;
      leds[215] = CRGB::Black;
      FastLED.show();
      delay(30);

// Turn the first led red for 1 second
       leds[223] = CRGB::Red;
       leds[225] = CRGB::Red;
       leds[237] = CRGB::Red;
       leds[243] = CRGB::Red;
       leds[244] = CRGB::Red;
       leds[234] = CRGB::Red;
       leds[230] = CRGB::Red;
       leds[216] = CRGB::Red;
        FastLED.show();
        delay(100);
      
// Set the first led back to black for 1 second
      leds[223] = CRGB::Black;
      leds[225] = CRGB::Black;
      leds[237] = CRGB::Black;
      leds[243] = CRGB::Black;
      leds[244] = CRGB::Black;
      leds[234] = CRGB::Black;
      leds[230] = CRGB::Black;
      leds[216] = CRGB::Black;
      FastLED.show();
      delay(30);

// Turn the first led red for 1 second
       leds[224] = CRGB::Red;
       leds[238] = CRGB::Red;
       leds[242] = CRGB::Red;
       leds[252] = CRGB::Red;
       leds[251] = CRGB::Red;
       leds[245] = CRGB::Red;
       leds[233] = CRGB::Red;
       leds[231] = CRGB::Red;
       FastLED.show();
       delay(100);
      
// Set the first led back to black for 1 second
      leds[224] = CRGB::Black;
      leds[238] = CRGB::Black;
      leds[242] = CRGB::Black;
      leds[252] = CRGB::Black;
      leds[251] = CRGB::Black;
      leds[245] = CRGB::Black;
      leds[233] = CRGB::Black;
      leds[231] = CRGB::Black;
      FastLED.show();
      delay(30);

      


  }

 

Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 20, 2017, 04:39 am
Second half of the code:

Code: [Select]
// RIGHT TURN SIGNAL

if (buttonState2 == HIGH) {

FastLED.setBrightness(BRIGHTNESS);
  
// Set the first led back to black for 1 second
        for (int i = 0; i <= 63; i++)
            leds[i] = CRGB::Black;

// Turn the first led red for 1 second
       leds[56] = CRGB::Red;
       leds[57] = CRGB::Red;
       leds[58] = CRGB::Red;
       leds[59] = CRGB::Red;
       leds[60] = CRGB::Red;
       leds[61] = CRGB::Red;
       leds[62] = CRGB::Red;
       leds[63] = CRGB::Red;
       leds[49] = CRGB::Red;
       leds[50] = CRGB::Red;
       leds[51] = CRGB::Red;
       leds[52] = CRGB::Red;
       leds[53] = CRGB::Red;
       leds[54] = CRGB::Red;
       leds[42] = CRGB::Red;
       leds[43] = CRGB::Red;
       leds[44] = CRGB::Red;
       leds[45] = CRGB::Red;
       leds[35] = CRGB::Red;
       leds[36] = CRGB::Red;
       FastLED.show();
       delay(100);
      
// Set the first led back to black for 1 second
       leds[56] = CRGB::Black;
       leds[57] = CRGB::Black;
       leds[58] = CRGB::Black;
       leds[59] = CRGB::Black;
       leds[60] = CRGB::Black;
       leds[61] = CRGB::Black;
       leds[62] = CRGB::Black;
       leds[63] = CRGB::Black;
       leds[49] = CRGB::Black;
       leds[50] = CRGB::Black;
       leds[51] = CRGB::Black;
       leds[52] = CRGB::Black;
       leds[53] = CRGB::Black;
       leds[54] = CRGB::Black;
       leds[42] = CRGB::Black;
       leds[43] = CRGB::Black;
       leds[44] = CRGB::Black;
       leds[45] = CRGB::Black;
       leds[35] = CRGB::Black;
       leds[36] = CRGB::Black;
        FastLED.show();
        delay(30);

// Turn the first led red for 1 second
       leds[48] = CRGB::Red;
       leds[55] = CRGB::Red;
       leds[46] = CRGB::Red;
       leds[41] = CRGB::Red;
       leds[37] = CRGB::Red;
       leds[34] = CRGB::Red;
       leds[28] = CRGB::Red;
       leds[27] = CRGB::Red;
       FastLED.show();
        delay(100);
      
// Set the first led back to black for 1 second
       leds[48] = CRGB::Black;
       leds[55] = CRGB::Black;
       leds[46] = CRGB::Black;
       leds[41] = CRGB::Black;
       leds[37] = CRGB::Black;
       leds[34] = CRGB::Black;
       leds[28] = CRGB::Black;
       leds[27] = CRGB::Black;
        FastLED.show();
        delay(30);

// Turn the first led red for 1 second
       leds[47] = CRGB::Red;
       leds[40] = CRGB::Red;
       leds[33] = CRGB::Red;
       leds[38] = CRGB::Red;
       leds[26] = CRGB::Red;
       leds[29] = CRGB::Red;
       leds[19] = CRGB::Red;
       leds[20] = CRGB::Red;
       FastLED.show();
       delay(100);
      
// Set the first led back to black for 1 second
       leds[47] = CRGB::Black;
       leds[40] = CRGB::Black;
       leds[33] = CRGB::Black;
       leds[38] = CRGB::Black;
       leds[26] = CRGB::Black;
       leds[29] = CRGB::Black;
       leds[19] = CRGB::Black;
       leds[20] = CRGB::Black;
        FastLED.show();
        delay(30);

// Turn the first led red for 1 second
       leds[39] = CRGB::Red;
       leds[32] = CRGB::Red;
       leds[25] = CRGB::Red;
       leds[30] = CRGB::Red;
       leds[21] = CRGB::Red;
       leds[18] = CRGB::Red;
       leds[12] = CRGB::Red;
       leds[11] = CRGB::Red;
             FastLED.show();
      delay(100);
      
 // Set the first led back to black for 1 second
      leds[39] = CRGB::Black;
       leds[32] = CRGB::Black;
       leds[25] = CRGB::Black;
       leds[30] = CRGB::Black;
       leds[21] = CRGB::Black;
       leds[18] = CRGB::Black;
       leds[12] = CRGB::Black;
       leds[11] = CRGB::Black;
        FastLED.show();
        delay(30);

// Turn the first led red for 1 second
       leds[31] = CRGB::Red;
       leds[24] = CRGB::Red;
       leds[22] = CRGB::Red;
       leds[17] = CRGB::Red;
       leds[13] = CRGB::Red;
       leds[10] = CRGB::Red;
       leds[4] = CRGB::Red;
       leds[3] = CRGB::Red;
       FastLED.show();
       delay(100);
      
// Set the first led back to black for 1 second
      leds[31] = CRGB::Black;
      leds[24] = CRGB::Black;
      leds[22] = CRGB::Black;
      leds[17] = CRGB::Black;
      leds[13] = CRGB::Black;
      leds[10] = CRGB::Black;
      leds[4] = CRGB::Black;
      leds[3] = CRGB::Black;
      FastLED.show();
      delay(30);

  }

 
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 20, 2017, 04:45 am
Needed a third :(

Code: [Select]

//BRAKE LIGHT
       FastLED.setBrightness(BRIGHTNESS);
      if(buttonState3 == HIGH){
// Set the right tail light black
        for (int i = 0; i <= 63; i++)
              leds[i] = CRGB::Black;
              
// left tail light black
        for (int j = 192; j <= 255; j++)
            leds[j] = CRGB::Black;
              FastLED.show();
              delay(200);

// Turn the first led red for 1 second   leds[i] %= 192;
       leds[36]  = CRGB::Red;
       leds[35] = CRGB::Red;
       leds[28] = CRGB::Red;
       leds[27] = CRGB::Red;

// left brake light
       leds[227] = CRGB::Red;
       leds[228] = CRGB::Red;
       leds[219] = CRGB::Red;
       leds[220] = CRGB::Red;
        FastLED.show();
        delay(200);
        
// Turn the first led red for 1 second
       leds[42] = CRGB::Red;
       leds[43] = CRGB::Red;
       leds[44] = CRGB::Red;
       leds[45] = CRGB::Red;
       leds[37] = CRGB::Red;
       leds[29] = CRGB::Red;
       leds[21] = CRGB::Red;
       leds[20] = CRGB::Red;
       leds[19] = CRGB::Red;
       leds[18] = CRGB::Red;
       leds[26] = CRGB::Red;
       leds[34] = CRGB::Red;

// left brake light
       leds[234] = CRGB::Red;
       leds[235] = CRGB::Red;
       leds[236] = CRGB::Red;
       leds[237] = CRGB::Red;
       leds[226] = CRGB::Red;
       leds[221] = CRGB::Red;
       leds[210] = CRGB::Red;
       leds[211] = CRGB::Red;
       leds[212] = CRGB::Red;
       leds[213] = CRGB::Red;
       leds[218] = CRGB::Red;
       leds[229] = CRGB::Red;
        FastLED.show();
        delay(200);
      
// Turn the first led red for 1 second
       leds[49] = CRGB::Red;
       leds[50] = CRGB::Red;
       leds[51] = CRGB::Red;
       leds[52] = CRGB::Red;
       leds[53] = CRGB::Red;
       leds[46] = CRGB::Red;
       leds[38] = CRGB::Red;
       leds[30] = CRGB::Red;
       leds[22] = CRGB::Red;
       leds[14] = CRGB::Red;
       leds[13] = CRGB::Red;
       leds[12] = CRGB::Red;
       leds[11] = CRGB::Red;
       leds[10] = CRGB::Red;
       leds[9] = CRGB::Red;
       leds[17] = CRGB::Red;
       leds[25] = CRGB::Red;
       leds[33] = CRGB::Red;
       leds[41] = CRGB::Red;
       leds[54] = CRGB::Red;

// left brake light
       leds[241] = CRGB::Red;
       leds[242] = CRGB::Red;
       leds[243] = CRGB::Red;
       leds[244] = CRGB::Red;
       leds[245] = CRGB::Red;
       leds[246] = CRGB::Red;
       leds[233] = CRGB::Red;
       leds[230] = CRGB::Red;
       leds[217] = CRGB::Red;
       leds[214] = CRGB::Red;
       leds[201] = CRGB::Red;
       leds[202] = CRGB::Red;
       leds[203] = CRGB::Red;
       leds[204] = CRGB::Red;
       leds[205] = CRGB::Red;
       leds[206] = CRGB::Red;
       leds[209] = CRGB::Red;
       leds[222] = CRGB::Red;
       leds[225] = CRGB::Red;
       leds[238] = CRGB::Red;  
        FastLED.show();
         delay(200);
      
// Turn the first led red for 1 second
       leds[63] = CRGB::Red;
       leds[62] = CRGB::Red;
       leds[61] = CRGB::Red;
       leds[60] = CRGB::Red;
       leds[59] = CRGB::Red;
       leds[58] = CRGB::Red;
       leds[57] = CRGB::Red;
       leds[56] = CRGB::Red;
       leds[55] = CRGB::Red;
       leds[47] = CRGB::Red;
       leds[39] = CRGB::Red;
       leds[31] = CRGB::Red;
       leds[23] = CRGB::Red;
       leds[15] = CRGB::Red;
       leds[7] = CRGB::Red;
       leds[6] = CRGB::Red;
       leds[5] = CRGB::Red;
       leds[4] = CRGB::Red;
       leds[3] = CRGB::Red;
       leds[2] = CRGB::Red;
       leds[1] = CRGB::Red;
       leds[8] = CRGB::Red;
       leds[16] = CRGB::Red;
       leds[24] = CRGB::Red;
       leds[32] = CRGB::Red;
       leds[40] = CRGB::Red;
       leds[48] = CRGB::Red;
       leds[0] = CRGB::Red;
      
// left brake light
       leds[255] = CRGB::Red;
       leds[254] = CRGB::Red;
       leds[253] = CRGB::Red;
       leds[252] = CRGB::Red;
       leds[251] = CRGB::Red;
       leds[250] = CRGB::Red;
       leds[249] = CRGB::Red;
       leds[248] = CRGB::Red;
       leds[247] = CRGB::Red;
       leds[232] = CRGB::Red;
       leds[231] = CRGB::Red;
       leds[216] = CRGB::Red;
       leds[215] = CRGB::Red;
       leds[200] = CRGB::Red;
       leds[199] = CRGB::Red;
       leds[198] = CRGB::Red;
       leds[197] = CRGB::Red;
       leds[196] = CRGB::Red;
       leds[195] = CRGB::Red;
       leds[194] = CRGB::Red;
       leds[193] = CRGB::Red;
       leds[192] = CRGB::Red;
       leds[207] = CRGB::Red;
       leds[208] = CRGB::Red;
       leds[223] = CRGB::Red;
       leds[224] = CRGB::Red;
       leds[239] = CRGB::Red;
       leds[240] = CRGB::Red;
        FastLED.show();
        delay(30);

  
        oldButtonState3 = HIGH; // so this block can only run once

}

// "stop braking" code here
// turn LED off:
          if(buttonState3 == LOW) {
        delay(30);
      
          oldButtonState3 == LOW;
          FastLED.setBrightness(BRIGHTNESS);

  }
 

}    
Title: Re: Running Loop once and leaving LED lit until button released
Post by: Grumpy_Mike on Jul 20, 2017, 06:33 am
This is heavy going.

The reply #15 was to show you how to light LEDs in a loop where the LED number do not follow each other but are in an arbitrary sequence. I gave you one short example. I expected you to apply that technique to all the rest of the turged code you had, so you did not need three posts to get your code over.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: INTP on Jul 20, 2017, 08:52 am
Code: [Select]
// Set the left tail light to black for 1 second
      for (int j = 192; j <= 255; j++)
             leds[j] = CRGB::Red;
              FastLED.show();
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 21, 2017, 04:40 am
Better but still away to go.
When you have a load of LEDs that do not have consecutive numbers you use an other array as a list or look up table of these array numbers. So for example when you want to turn on the center four lights you first define the array that contains the numbers and then use the array contents as the LED array you want to change.

Code: [Select]

// outside of a function define:-
byte center4 = {36,35,28,27};

// then inside a function use
for (int b = 0; b<4; b++;) {
 leds[center4[b]] = CRGB::RED;
}

That will help a lot the longer the list goes on.
Can you double check your coding here - because it is not working.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: INTP on Jul 21, 2017, 04:48 am
byte center4 []= {36,35,28,27};


just forgot the []

which you should've picked up on if you read up on arrays once they were mentioned. Maybe you're unaware there's a reference section (https://www.arduino.cc/en/Reference/Array).
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 21, 2017, 05:37 am
how do i declare b in the scope? I figured out the brackets but can't figure out how to declare b
Title: Re: Running Loop once and leaving LED lit until button released
Post by: INTP on Jul 21, 2017, 06:02 am
int b

is declaring b.  (https://www.arduino.cc/en/Reference/VariableDeclaration)

Title: Re: Running Loop once and leaving LED lit until button released
Post by: INTP on Jul 21, 2017, 06:04 am
And this (https://www.arduino.cc/en/Reference/Scope), since you'll ask about it without trying to find it yourself.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: Grumpy_Mike on Jul 21, 2017, 11:18 am
how do i declare b in the scope? I figured out the brackets but can't figure out how to declare b
Looks like you typed in the code wrong for that bit. It is delclaired inside the if statement and is in scope between the {.   } of the if.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 21, 2017, 05:50 pm
Figured it out - you had an extra ) and ; i think.

Here is my code thats around 22% memory and not 56+% memory like before.


So how do I get the brightness to stay dim on the non directional light and for the brake code to stop looping?




Code: [Select]
#include "FastLED.h"

// fast led constants
#define DATA_PIN    3        // change to your data pin
#define COLOR_ORDER GRB      // if colors are mismatched; change this
#define NUM_LEDS    256          // change to the number of LEDs in your strip
#define FRAMES_PER_SECOND  120
#define LED_TYPE    WS2812B
#define BRIGHTNESS          200
#define DIM         10


// constant variables
const int buttonPin1 = 5;     // the number of the pushbutton pin
const int buttonPin2 = 6;
const int buttonPin3 = 7;
const int buttonPin4 = 10;



// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int oldButtonState3 = 0;

// this creates an LED array to hold the values for each led in your strip
CRGB leds[NUM_LEDS];

void setup()
{
    delay(3000); // 3 second delay for recovery

  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds,NUM_LEDS).setCorrection(TypicalLEDStrip);


  // the wiki features a much more basic setup line:
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);

Serial.begin(9600);

// initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);


}

 void loop()



{
      FastLED.setBrightness(DIM);

// Set the right tail light to black for 1 second
      for (int i = 0; i <= 63; i++)
              leds[i] = CRGB::Red;

// Set the left tail light to black for 1 second
      for (int j = 192; j <= 255; j++)
             leds[j] = CRGB::Red;
              FastLED.show();

// read the state of the pushbutton value:
    buttonState1 = digitalRead(buttonPin1);
    buttonState2 = digitalRead(buttonPin2);
    buttonState3 = digitalRead(buttonPin3);
    buttonState4 = digitalRead(buttonPin4);





 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
          if (buttonState1 == HIGH) {


//LEFT TURN SIGNAL



// left tail light black
        for (int f = 192; f <= 255; f++)
              leds[f] = CRGB::Black;


        FastLED.setBrightness(BRIGHTNESS);


// Set the first led back to black
        for (int a = 192; a <= 220; a++)
        leds[a] = CRGB::Black;
        FastLED.show();
        delay(30);


// FIRST ARROW RED

byte firstArrow20[] = {192,193,194,195,196,197,198,199,201,202,203,204,205,206,210,211,212,213,219,220};

for (int g = 0; g<20; g++) {
 leds[firstArrow20[g]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// FIRST ARROW BLACK

for (int g = 0; g<20; g++) {
 leds[firstArrow20[g]] = CRGB::Black;
}
FastLED.show();
 delay (30);



// SECOND ARROW RED

byte secondArrow8[] = {207,209,221,227,228,218,214,200};

for (int h = 0; h<8; h++) {
 leds[secondArrow8[h]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// SECOND ARROW BLACK

for (int h = 0; h<8; h++) {
 leds[secondArrow8[h]] = CRGB::Black;
}
FastLED.show();
 delay (30);


// THIRD ARROW RED

byte thirdArrow8[] = {208,222,226,236,235,229,217,215};

for (int k = 0; k<8; k++) {
 leds[thirdArrow8[k]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// THIRD ARROW BLACK

for (int k = 0; k<8; k++) {
 leds[thirdArrow8[k]] = CRGB::Black;
}
FastLED.show();
 delay (30);



 // FOURTH ARROW RED

byte fourthArrow8[] = {223,225,237,243,244,234,230,216};

for (int l = 0; l<8; l++) {
 leds[fourthArrow8[l]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// FOURTH ARROW BLACK

for (int l = 0; l<8; l++) {
 leds[fourthArrow8[l]] = CRGB::Black;
}
FastLED.show();
 delay (30);

 

 // FIFTH ARROW RED

byte fifthArrow8[] = {224,238,242,252,251,245,233,231};

for (int m = 0; m<8; m++) {
 leds[fifthArrow8[m]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// FIFTH ARROW BLACK

for (int m = 0; m<8; m++) {
 leds[fifthArrow8[m]] = CRGB::Black;
}
FastLED.show();
 delay (30);


  }

// RIGHT TURN SIGNAL

if (buttonState2 == HIGH) {

FastLED.setBrightness(BRIGHTNESS);

// Set the first led back to black for 1 second
        for (int i = 0; i <= 63; i++)
            leds[i] = CRGB::Black;

FastLED.show();
        delay(30);


// SIXTH ARROW RED

byte sixthArrow20[] = {56,57,58,59,60,61,62,63,49,50,51,52,53,54,42,43,44,45,35,36};

for (int n = 0; n<20; n++) {
 leds[sixthArrow20[n]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// SIXTH ARROW BLACK

for (int n = 0; n<20; n++) {
 leds[sixthArrow20[n]] = CRGB::Black;
}
FastLED.show();
 delay (30);



// SEVENTH ARROW RED

byte seventhArrow8[] = {48,55,46,41,37,34,28,27};

for (int o = 0; o<8; o++) {
 leds[seventhArrow8[o]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// SEVENTH ARROW BLACK

for (int o = 0; o<8; o++) {
 leds[seventhArrow8[o]] = CRGB::Black;
}
FastLED.show();
 delay (30);


// EIGTH ARROW RED

byte eigthArrow8[] = {47,40,33,38,26,29,19,20};

for (int p = 0; p<8; p++) {
 leds[eigthArrow8[p]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// EIGHT ARROW BLACK

for (int p = 0; p<8; p++) {
 leds[eigthArrow8[p]] = CRGB::Black;
}
FastLED.show();
 delay (30);



 // NINTH ARROW RED

byte ninthArrow8[] = {39,32,25,30,21,18,12,11};

for (int q = 0; q<8; q++) {
 leds[ninthArrow8[q]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// NINTH ARROW BLACK

for (int q = 0; q<8; q++) {
 leds[ninthArrow8[q]] = CRGB::Black;
}
FastLED.show();
 delay (30);

 

 // TENTH ARROW RED

byte tenthArrow8[] = {31,24,22,17,13,10,4,3};

for (int r = 0; r<8; r++) {
 leds[tenthArrow8[r]] = CRGB::Red;
}
FastLED.show();
 delay (100);

// TENTH ARROW BLACK

for (int r = 0; r<8; r++) {
 leds[tenthArrow8[r]] = CRGB::Black;
}
FastLED.show();
 delay (30);

  }

//BRAKE LIGHT
       FastLED.setBrightness(BRIGHTNESS);
      if(buttonState3 == HIGH){
// Set the right tail light black
        for (int i = 0; i <= 63; i++)
              leds[i] = CRGB::Black;

// left tail light black
        for (int j = 192; j <= 255; j++)
            leds[j] = CRGB::Black;
              FastLED.show();
              delay(200);

//CENTER 4
byte center4[] = {36,35,28,27,227,228,219,220};

for (int b = 0; b<8; b++) {
 leds[center4[b]] = CRGB::Red;
}
FastLED.show();
delay(200);

// FIRST RING
byte firstRing12[] = {42,43,44,45,37,29,21,20,19,18,26,34,234,235,236,237,226,221,210,211,212,213,218,229};

for (int c = 0; c<24; c++) {
 leds[firstRing12[c]] = CRGB::Red;
}
FastLED.show();
delay(200);


// SECOND RING
byte secondRing20[] = {49,50,51,52,53,46,38,30,22,14,13,12,11,10,9,17,25,33,41,54,241,242,243,244,245,246,233,230,217,214,201,202,203,204,205,206,209,222,225,238};

for (int d = 0; d<40; d++) {
 leds[secondRing20[d]] = CRGB::Red;
}FastLED.show();
 delay (200);


// THIRD RING
byte thirdRing28[] = {63,62,61,60,59,58,57,56,55,47,39,31,23,15,7,6,5,4,3,2,1,0,8,16,24,32,40,48,255,254,253,252,251,250,249,248,247,232,231,216,
215,200,199,198,197,196,195,194,193,192,207,208,223,224,239,240};

for (int e = 0; e<56; e++) {
 leds[thirdRing28[e]] = CRGB::Red;
}
FastLED.show();
 delay (200);



oldButtonState3 = HIGH; // so this block can only run once

}

// "stop braking" code here
// turn LED off:
if(buttonState3 == LOW) {

          oldButtonState3 == LOW;
          FastLED.setBrightness(BRIGHTNESS);

  }


}
Title: Re: Running Loop once and leaving LED lit until button released
Post by: INTP on Jul 21, 2017, 10:53 pm
Black is 0,0,0. You'll have to write crgb to whatever you think works for 'dim'. E.g. 80,0,0 for dim red.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 22, 2017, 03:45 am
Black is 0,0,0. You'll have to write crgb to whatever you think works for 'dim'. E.g. 80,0,0 for dim red.
Is that to make the brake light stop looping? Because I am not calling black for the left and right non blinker sections.
Title: Re: Running Loop once and leaving LED lit until button released
Post by: midwesta on Jul 24, 2017, 03:09 am
Do i need to use a latch function?
Title: Re: Running Loop once and leaving LED lit until button released
Post by: Grumpy_Mike on Jul 24, 2017, 09:17 am
Do i need to use a latch function?
What latch function?
If you want the LEDs to stop on for the whole time a button is pressed just hold your code in a while loop.
Code: [Select]

while(digitalRead(pin) == pressed) { } // where the variable pressed is the logic level you get when the button is held down.