WS2812 and Teensy 3.2 and a button

Status
Not open for further replies.

slurry bowl

Active member
Hello.

I used to run this sketch on Arudino and it worked well.
I am using the Button library to tap through different animations.
It does not work at all on the Teensy.
My hunch is that I need to declare my button pin to utilize the built-in PULLUP resistor, but
everytime I try to modify the code to do this, I mess with the Button declarations.

Any input appreciated.

Code:
#include "FastLED.h"

FASTLED_USING_NAMESPACE

#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif

#define DATA_PIN    8
//#define CLK_PIN   4
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    600
CRGB leds[NUM_LEDS];

CRGBPalette16 gPal;

bool gReverseDirection = false;

#define BRIGHTNESS          100
#define FRAMES_PER_SECOND  120

#include "Button.h"    // Include Button library
const int buttonPin = 16;  // Set digital pin used with debounced pushbutton
Button myButton(buttonPin, true, true, 50);  // Declare the butto
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);
  

  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);

  gPal = CRGBPalette16( CRGB:: Purple, CRGB::Black, CRGB::Blue, CRGB::Green);
  

}

// List of patterns to cycle through.  Each is defined as a separate function below.
typedef void (*SimplePatternList[])();


SimplePatternList gPatterns = {  confetti_GB, juggle_B_less,juggle_B_more,juggle_B_lots,juggle_R_less, juggle_R, juggle_G  };
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()
{
  // Call the current pattern function once, updating the 'leds' array
  gPatterns[gCurrentPatternNumber]();

  // send the 'leds' array out to the actual LED strip
  FastLED.show();  
  // insert a delay to keep the framerate modest
  FastLED.delay(1000/FRAMES_PER_SECOND); 

  // do some periodic updates
  EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  //EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  readbutton();  // check for button press
 
}

#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
  // add one to the current pattern number, and wrap around at the end
  gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}

void rainbow() 
{
  // FastLED's built-in rainbow generator
  fill_rainbow( leds, NUM_LEDS, gHue, 7);
}


void rainbowuniform() 
{
  // FastLED's built-in rainbow generator
  fill_rainbow( leds, NUM_LEDS, gHue, 2);
}

void rainbowWEIRD() 
{
  // FastLED's built-in rainbow generator
  fill_rainbow( leds, NUM_LEDS, gHue, 30);
}
void sinelon()
{
  // a colored dot sweeping back and forth, with fading trails
  fadeToBlackBy( leds, NUM_LEDS, 20);
  int pos = beatsin16(13,0,NUM_LEDS);
  leds[pos] += CHSV( gHue, 255, 192);
}

void confetti() 
{
  // random colored speckles that blink in and fade smoothly
  fadeToBlackBy( leds, NUM_LEDS, 10);
  int pos = random16(NUM_LEDS);
  leds[pos] += CHSV( gHue + random8(64), 20, 250);
}

void rainbowWithGlitter() 
{
  // built-in FastLED rainbow, plus some random sparkly glitter
  rainbow();
  addGlitter(80);
}

void addGlitter( fract8 chanceOfGlitter) 
{
  if( random8() < chanceOfGlitter) {
    leds[ random16(NUM_LEDS) ] += CRGB::White;
  }
}


void juggle_R() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 20);
  byte dothue = 5;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 180);
    //dothue += 32;
  }
}

void juggle_G() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 200);
  byte dothue = 96;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100);
    //dothue += 32;
  }
}
void juggle_B_less() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 240);
  byte dothue = 160;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100);
    //dothue += 32;
  }
}

void juggle_R_less() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 240);
  byte dothue = 5;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 180);
    //dothue += 32;
  }
}

void juggle_B_more() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 100);
  byte dothue = 160;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100);
    //dothue += 32;
  }
}

void juggle_B_lots() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 10);
  byte dothue = 160;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16(i+1,0,NUM_LEDS)] |= CHSV(dothue, 200, 100);
    //dothue += 32;
  }
}

void juggle_Bsteady() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 1);
  byte dothue = 160;
  for( int i = 0; i < 240; i++) {
    leds[beatsin16(i+200,0,NUM_LEDS)] |= CHSV(dothue, 200, 55);
    //dothue += 32;
  }
}

void juggle() {
  // eight colored dots, weaving in and out of sync with each other
  fadeToBlackBy( leds, NUM_LEDS, 20);
  byte dothue = 0;
  for( int i = 0; i < 8; i++) {
    leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 100);
    dothue += 32;
  }
}

void confetti_GB()
{
//   random colored speckles, Green and Blue hues only
  // Green = 96, Blue = 160
  uint8_t p = 30;  // What percentage of the time to make speckles.  [Range 0-100]
 
  fadeToBlackBy( leds, NUM_LEDS, 10);
  if (random8(100) < p) {
    int pos = random16(NUM_LEDS);
    uint8_t hue = random8(2);  // randomly chooses a 0 or 1
    if (hue == 0) {
      hue = random8(92,111);  // pick a hue somewhere in the green zone
    } else {
      hue = random8(156,165);  // pick a hue somewhere in the blue zone
    }
    leds[pos] += CHSV( hue, random8(200,240), 100);
  }
}//end confetti_GB






// Fire2012 by Mark Kriegsman, July 2012
// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
//// 
// This basic one-dimensional 'fire' simulation works roughly as follows:
// There's a underlying array of 'heat' cells, that model the temperature
// at each point along the line.  Every cycle through the simulation, 
// four steps are performed:
//  1) All cells cool down a little bit, losing heat to the air
//  2) The heat from each cell drifts 'up' and diffuses a little
//  3) Sometimes randomly new 'sparks' of heat are added at the bottom
//  4) The heat from each cell is rendered as a color into the leds array
//     The heat-to-color mapping uses a black-body radiation approximation.
//
// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
//
// This simulation scales it self a bit depending on NUM_LEDS; it should look
// "OK" on anywhere from 20 to 100 LEDs without too much tweaking. 
//
// I recommend running this simulation at anywhere from 30-100 frames per second,
// meaning an interframe delay of about 10-35 milliseconds.
//
// Looks best on a high-density LED setup (60+ pixels/meter).
//
//
// There are two main parameters you can play with to control the look and
// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
// in step 3 above).
//
// COOLING: How much does the air cool as it rises?
// Less cooling = taller flames.  More cooling = shorter flames.
// Default 55, suggested range 20-100 
#define COOLING  55

// SPARKING: What chance (out of 255) is there that a new spark will be lit?
// Higher chance = more roaring fire.  Lower chance = more flickery fire.
// Default 120, suggested range 50-200.
#define SPARKING 50


void Fire2012WithPalette()
{
// Array of temperature readings at each simulation cell
  static byte heat[NUM_LEDS];

  // Step 1.  Cool down every cell a little
    for( int i = 0; i < NUM_LEDS; i++) {
      heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
    }
  
    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for( int k= NUM_LEDS - 1; k >= 2; k--) {
      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
    }
    
    // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
    if( random8() < SPARKING ) {
      int y = random8(7);
      heat[y] = qadd8( heat[y], random8(160,255) );
    }

    // Step 4.  Map from heat cells to LED colors
    for( int j = 0; j < NUM_LEDS; j++) {
      // Scale the heat value from 0-255 down to 0-240
      // for best results with color palettes.
      byte colorindex = scale8( heat[j], 240);
      CRGB color = ColorFromPalette( gPal, colorindex);
      int pixelnumber;
      if( gReverseDirection ) {
        pixelnumber = (NUM_LEDS-1) - j;
      } else {
        pixelnumber = j;
      }
      leds[pixelnumber] = color;
    }
}


//BUTTON STUFF
//---------Function to read the button and do something----------
void readbutton() {
  myButton.read();
  if(myButton.wasPressed()) {
    nextPattern();  // Change to the next pattern

    }
}//end_readbutton

thanks.
 
Last edited by a moderator:
Do you actually have bounce in your include section? Not seeing it in the code. And you certainly do need to set as input and enable pullup on the teensy, where the classic Arduino defaults to input.

If you look at the teensy bounce examples, and maye run a test on your pin against them they declare the pinmode in setup and should be without issue.

If you are using the standard arduino bounce you can also de stand alone digital reads of the pin if you are trying to check if problem is the pin hardware or something in the debounce process.
 
If you want me or anyone else here to look at this, you need to be more specific about which library this is:

Code:
#include "Button.h"    // Include Button library

Everybody knows FastLED and that's one of the libraries Teensyduino has in its installer. But for this other one, which code are you actually using?
 
If you want me or anyone else here to look at this, you need to be more specific about which library this is:

Code:
#include "Button.h"    // Include Button library

Everybody knows FastLED and that's one of the libraries Teensyduino has in its installer. But for this other one, which code are you actually using?

The library is written by Michael Adams and is simply called Button. My library manager states: "It handles debouncing automatically, and monitoring of state."

Ive had issues with it because there is another library also called Button for Arduino that is different. ugh. Perhaps I should avoid using it so I can easily declare the Pullup resistor on the Teensy.

Im not very code savy but perhaps this line of code and the 2 declarations true, true, may be relevant:

Button myButton(buttonPin, true, true, 50); // Declare the button
 
Status
Not open for further replies.
Back
Top