Go Down

Topic: Trigger groupings of WS2812 pixels inside code (Read 131 times) previous topic - next topic

Lightguy_05

Hi,

Have some code to read button presses from my Digital pins using internal pullup resistors. (will paste below)

When arduino is first powered on, I need it to print a static RGB value to all 70 of the WS2812 LEDs

Around Line 62 (starts referencing "case 0, case 1, case 2" etc),

I want it to set a static value of my WS2812 LEDs to a specified RGB value when it also is printing a serial command for each case, along the "Serial.println"

i.e. set LED's 0-30 to 155,100,3



I've gotten this far, but can't get the LED's to do anything yet.

Code: [Select]
#include <FastLED.h>

    #define NUM_LEDS 70
    #define LED_PIN 11
    #define BRIGHTNESS 100
    #define LED_TYPE WS2812
    #define COLOR_ORDER GRB
   struct CRGB leds[NUM_LEDS];
   

 //  CRGBArray<NUM_LEDS> leds;

//CRGBSet partA(leds(0,70));  // Define custom pixel range with a name.
//CRGBSet partB(leds(0,16));  // Define custom pixel range with a name.
//CRGBSet partC(leds(17,34));  // Define custom pixel range with a name.
//CRGBSet partD(leds(35,52));  // Define custom pixel range with a name.
//CRGBSet partE(leds(53,70));  // Define custom pixel range with a name.

//CHSV colorOne(255,255,255);  // Define a custom color.
//CHSV colorTwo(255,130,0);  // Define a custom color.
CRGB rgbvalwh(255,255,255);
CRGB rgbvalorg(255,130,0);

#define DEBOUNCE 10  // how many ms to debounce, 5+ ms is usually plenty
 
//define the buttons that we'll use.
byte buttons[] = {0, 2, 4, 6, 8, 9};
 
//determine how big the array up above is, by checking the size
#define NUMBUTTONS sizeof(buttons)
 
//track if a button is just pressed, just released, or 'currently pressed'
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS];
 
void setup() {
  byte i;
  Serial.begin(9600); //set up serial port
  Serial.print("Button checker with ");
  Serial.print(NUMBUTTONS, DEC);
  Serial.println(" buttons");
  // Make input & enable pull-up resistors on switch pins
  for (i=0; i< NUMBUTTONS; i++) {
    pinMode(buttons[i], INPUT);
    digitalWrite(buttons[i], HIGH);

    //LED Code
 
    delay( 1000 ); // power-up safety delay
    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );
    set_max_power_in_volts_and_milliamps(5, 500);               // FastLED Power management set at 5V, 500mA.

   
   
  }
}
 
void loop() {
  fill_solid(leds, 70, rgbvalwh);
 
  byte thisSwitch=thisSwitch_justPressed();
  switch(thisSwitch)
  { 
  case 0:
    Serial.println("switch 1 just pressed"); break;
  case 1:
    Serial.println("switch 2 just pressed"); break;
  case 2:
    Serial.println("switch 3 just pressed"); break;
  case 3:
    Serial.println("switch 4 just pressed"); break;
  case 4:
    Serial.println("switch 5 just pressed"); break;
  case 5:
    Serial.println("switch 6 just pressed"); break;   
  }
}
 
void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  static long lasttime;
  byte index;
  if (millis() < lasttime) {
    // we wrapped around, lets just try again
    lasttime = millis();
  }
  if ((lasttime + DEBOUNCE) > millis()) {
    // not enough time has passed to debounce
    return;
  }
  // ok we have waited DEBOUNCE milliseconds, lets reset the timer
  lasttime = millis();
  for (index = 0; index < NUMBUTTONS; index++) {
    justpressed[index] = 0;       //when we start, we clear out the "just" indicators
    justreleased[index] = 0;
    currentstate[index] = digitalRead(buttons[index]);   //read the button
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
        // just pressed
        justpressed[index] = 1;
      }
      else if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
        justreleased[index] = 1; // just released
      }
      pressed[index] = !currentstate[index];  //remember, digital HIGH means NOT pressed
    }
    previousstate[index] = currentstate[index]; //keep a running tally of the buttons
  }
}
 
byte thisSwitch_justPressed() {
  byte thisSwitch = 255;
  check_switches();  //check the switches &amp; get the current state
  for (byte i = 0; i < NUMBUTTONS; i++) {
    current_keystate[i]=justpressed[i];
    if (current_keystate[i] != previous_keystate[i]) {
      if (current_keystate[i]) thisSwitch=i;
    }
    previous_keystate[i]=current_keystate[i];
  } 
  return thisSwitch;
 
  FastLED.show();                         // Power managed display
}



PaulRB

Have you successfully run any of the FasLED example sketches?

Go Up