Page 1 of 1
Forum

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.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Debounce issue cont...
 
Share:
Notifications
Clear all

[Solved] Debounce issue continued

7 Posts
2 Users
0 Reactions
2,144 Views
(@lakerice)
Eminent Member
Joined: 4 years ago
Posts: 30
Topic starter  

This is a continuation of my question that was posted in the LED strip effects for Neopixel and Fastled page here . I am posting on the forum, since there was just way too much code I had posted on the other page. The issue was that I was having random button errors like pausing the effect or not switching properly (and was actually related to the All Effects in One sketch page), so I definitely apologize for the confusing places to post :)

In response to Hans' helpful suggestions, I seem to have no issues changing effects now.  Hans had suggested some code to include in the setup, including a short 100 millisecond delay before attaching the interrupt for the button.  As well, he had included a link to information on modifying the button by using a pull up resistor.  Since I don't seem to have any issues, I decided not to add it (also I'd rather not add a breadboard to my compact Arduino enclosure), but I it was good to learn about it.  I might be using the internal 20K pull up resistor that was built in to the Arduino Uno, but I could be wrong about that. 

Anyway, for anyone interested, I have posted my sketch here on pastebin.  About half the effects were removed.  Might be helpful to others.

Thanks again Hans for your help!

 


   
ReplyQuote
Topic Tags
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
 

Thanks Lakerice! 👍 

In the forum, feel free to post as much code as you like 😉 

Your ino file code (I've also attached it as a downloadable file):

#include "FastLED.h"
#include 
#define NUM_LEDS 150

CRGB leds[NUM_LEDS];
#define PIN 5
#define BRIGHTNESS 100

#define BUTTON 2
byte selectedEffect=0;

void setup()
{
  FastLED.addLeds<ws2811, pin,="" grb="">(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  pinMode(2,INPUT_PULLUP); // internal pull-up resistor
  attachInterrupt (digitalPinToInterrupt (BUTTON), changeEffect, CHANGE); // pressed
  digitalWrite(2, HIGH);       // turn on pullup resistors
  delay(100); // little 100ms delay before attaching the interrupt for the button
   
}

//  REPLACE FROM HERE 
void loop() {
  EEPROM.get(0,selectedEffect);
 
  if(selectedEffect>9) {
    selectedEffect=0;
    EEPROM.put(0,0);
  }
 
  switch(selectedEffect) {
            
    case 1  : {
                // NewKITT - Color (red, green, blue), eye size, speed delay, end pause
              NewKITT(0x00, 0xff, 0x11, 8, 10, 50);
                break;
              }
             
    case 2  : {
                // Twinkle - Color (red, green, blue), count, speed delay, only one twinkle (true/false)
                 RunningLights(0xff,0xd7,0x00, 50);
                break;
              }
             
    case 3  : {
                // Sparkle - Color (red, green, blue), speed delay
                Sparkle(0x4d, 0x4d, 0xff, 0);
                break;
              }
               
    case 4  : {
                // SnowSparkle - Color (red, green, blue), sparkle delay, speed delay
               SnowSparkle(0x38, 0x00, 0x33, 20, random(100,1000));
                break;
              }

    case 5 : {
                // rainbowCycle - speed delay
                rainbowCycle(20);
                break;
              }

    case 6 : {
                // theaterChaseRainbow - Speed delay
                theaterChaseRainbow(50);
                break;
              }

    case 7 : {
                // Fire - Cooling rate, Sparking rate, speed delay
                Fire(55,120,15);
                break;
              }

              // simple bouncingBalls not included, since BouncingColoredBalls can perform this as well as shown below
              // BouncingColoredBalls - Number of balls, color (red, green, blue) array, continuous
              // CAUTION: If set to continuous then this effect will never stop!!!
            
    case 8 : {
                // meteorRain - Color (red, green, blue), meteor size, trail decay, random trail decay (true/false), speed delay
                meteorRain(0x38,0x00,0xff,10, 64, true, 30);
                break;
              }
  }
}

void changeEffect() {
  if (digitalRead (BUTTON) == HIGH) {
    selectedEffect++;
    EEPROM.put(0, selectedEffect);
    asm volatile ("  jmp 0");
  }
}


// *************************
// ** LEDEffect Functions **
// *************************


void NewKITT(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay){
  RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  LeftToRight(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  RightToLeft(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  OutsideToCenter(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
  CenterToOutside(red, green, blue, EyeSize, SpeedDelay, ReturnDelay);
}

// used by NewKITT
void CenterToOutside(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i =((NUM_LEDS-EyeSize)/2); i>=0; i--) {
    setAll(0,0,0);
   
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue);
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
   
    setPixel(NUM_LEDS-i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(NUM_LEDS-i-j, red, green, blue);
    }
    setPixel(NUM_LEDS-i-EyeSize-1, red/10, green/10, blue/10);
   
    showStrip();
    delay(SpeedDelay);
  }
  delay(ReturnDelay);
}

// used by NewKITT
void OutsideToCenter(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i = 0; i<=((NUM_LEDS-EyeSize)/2); i++) {
    setAll(0,0,0);
   
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue);
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
   
    setPixel(NUM_LEDS-i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(NUM_LEDS-i-j, red, green, blue);
    }
    setPixel(NUM_LEDS-i-EyeSize-1, red/10, green/10, blue/10);
   
    showStrip();
    delay(SpeedDelay);
  }
  delay(ReturnDelay);
}

// used by NewKITT
void LeftToRight(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i = 0; i < NUM_LEDS-EyeSize-2; i++) {
    setAll(0,0,0);
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue);
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
    showStrip();
    delay(SpeedDelay);
  }
  delay(ReturnDelay);
}

// used by NewKITT
void RightToLeft(byte red, byte green, byte blue, int EyeSize, int SpeedDelay, int ReturnDelay) {
  for(int i = NUM_LEDS-EyeSize-2; i > 0; i--) {
    setAll(0,0,0);
    setPixel(i, red/10, green/10, blue/10);
    for(int j = 1; j <= EyeSize; j++) {
      setPixel(i+j, red, green, blue);
    }
    setPixel(i+EyeSize+1, red/10, green/10, blue/10);
    showStrip();
    delay(SpeedDelay);
  }
  delay(ReturnDelay);
}

void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
  setAll(0,0,0);
 
  for (int i=0; i<count; i++)="" {="" setpixel(random(num_leds),red,green,blue);="" showstrip();="" delay(speeddelay);="" if(onlyone)="" setall(0,0,0);="" }="" void="" sparkle(byte="" red,="" byte="" green,="" blue,="" int="" speeddelay)="" pixel="random(NUM_LEDS);" setpixel(pixel,red,green,blue);="" setpixel(pixel,0,0,0);="" snowsparkle(byte="" sparkledelay,="" setall(red,green,blue);="" setpixel(pixel,0xff,0xff,0xff);="" delay(sparkledelay);="" runninglights(byte="" wavedelay)="" position="0;" for(int="" i="0;" i<num_leds*2;="" position++;="" =="" 0;="" +="" rate;="" i<num_leds;="" sine="" wave,="" 3="" offset="" waves="" make="" a="" rainbow!="" float="" level="sin(i+Position)" *="" 127="" 128;="" setpixel(i,level,0,0);="" setpixel(i,((sin(i+position)="" 128)="" 255)*red,="" ((sin(i+position)="" 255)*green,="" 255)*blue);="" delay(wavedelay);="" rainbowcycle(int="" *c;="" uint16_t="" i,="" j;="" for(j="0;" j<256*5;="" j++)="" 5="" cycles="" of="" all="" colors="" on="" wheel="" for(i="0;" i<="" num_leds;="" c="Wheel(((i" 256="" num_leds)="" j)="" &="" 255);="" setpixel(i,="" *c,="" *(c+1),="" *(c+2));="" used="" by="" rainbowcycle="" and="" theaterchaserainbow="" wheel(byte="" wheelpos)="" static="" c[3];="" if(wheelpos="" <="" 85)="" c[0]="WheelPos" 3;="" c[1]="255" -="" wheelpos="" c[2]="0;" else="" 170)="" return="" c;="" theaterchaserainbow(int="" for="" (int="" j="0;" 256;="" cycle="" in="" the="" q="0;" q++)="" (i+j)="" %="" setpixel(i+q,="" turn="" every="" third="" 0,0,0);="" off="" fire(int="" cooling,="" sparking,="" heat[num_leds];="" cooldown;="" step="" 1.="" cool="" down="" cell="" little="" for(="" cooldown="random(0," ((cooling="" 10)="" 2);="" if(cooldown="">heat[i]) {
      heat[i]=0;
    } else {
      heat[i]=heat[i]-cooldown;
    }
  }
 
  // 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' near the bottom
  if( random(255) < Sparking ) {
    int y = random(7);
    heat[y] = heat[y] + random(160,255);
    //heat[y] = random(160,255);
  }

  // Step 4.  Convert heat to LED colors
  for( int j = 0; j < NUM_LEDS; j++) {
    setPixelHeatColor(j, heat[j] );
  }

  showStrip();
  delay(SpeedDelay);
}

void setPixelHeatColor (int Pixel, byte temperature) {
  // Scale 'heat' down from 0-255 to 0-191
  byte t192 = round((temperature/255.0)*191);
 
  // calculate ramp up from
  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252
 
  // figure out which third of the spectrum we're in:
  if( t192 > 0x80) {                     // hottest
    setPixel(Pixel, 255, 255, heatramp);
  } else if( t192 > 0x40 ) {             // middle
    setPixel(Pixel, 255, heatramp, 0);
  } else {                               // coolest
    setPixel(Pixel, heatramp, 0, 0);
  }
}



void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  setAll(0,0,0);
 
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
   
   
    // fade brightness all LEDs one step
    for(int j=0; j<num_leds; j++)="" {="" if(="" (!meteorrandomdecay)="" ||="" (random(10)="">5) ) {
        fadeToBlack(j, meteorTrailDecay );        
      }
    }
   
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <num_leds) &&="" (i-j="">=0) ) {
        setPixel(i-j, red, green, blue);
      }
    }
   
    showStrip();
    delay(SpeedDelay);
  }
}

// used by meteorrain
void fadeToBlack(int ledNo, byte fadeValue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
    // NeoPixel
    uint32_t oldColor;
    uint8_t r, g, b;
    int value;
   
    oldColor = strip.getPixelColor(ledNo);
    r = (oldColor & 0x00ff0000UL) >> 16;
    g = (oldColor & 0x0000ff00UL) >> 8;
    b = (oldColor & 0x000000ffUL);

    r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
    g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
    b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
   
    strip.setPixelColor(ledNo, r,g,b);
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[ledNo].fadeToBlackBy( fadeValue );
 #endif  
}

//  REPLACE TO HERE 



// ***************************************
// ** FastLed/NeoPixel Common Functions **
// ***************************************

// Apply LED color changes
void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

// Set a LED color (not yet visible)
void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

// 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++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}</num_leds)></num_leds;></count;></ws2811,>


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
 

Thanks for posting the full code!

As for the pullup; yes there is indeed an internal resistor, which you may be using.

Using a breadboard is something I always try to avoid as well - but they are handy when doing some testing, but not so great for the "final product" haha 😉 . The less external components, the better 😋 


   
ReplyQuote
(@lakerice)
Eminent Member
Joined: 4 years ago
Posts: 30
Topic starter  
Posted by: @hans

Thanks for posting the full code!

As for the pullup; yes there is indeed an internal resistor, which you may be using.

Using a breadboard is something I always try to avoid as well - but they are handy when doing some testing, but not so great for the "final product" haha 😉 . The less external components, the better 😋 

Yes I agree; breadboards are not product friendly :) Too bad there aren't more tutorials with 3D printed cases for buttons, etc.

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
 
Posted by: @lakerice

Too bad there aren't more tutorials with 3D printed cases for buttons, etc.

I've been tinkering with several 3D printers as early as 2012, and in the end gave up playing with them around 2015.
I'd be more than happy to write more about using 3D printers - I still love the idea to design something on my computer and have it materialize in plastic 😊 

After buying and selling 3 printers, I have become quite weary about buying a new one.
I do keep an eye on the market for 3D printers, but I still feel that most aren't really suitable for the general public, and promise more than they can deliver.
If you have any experience with a particular brand/model, then please let us know ...

Breadboards are only fun for experiments indeed 😋 


   
ReplyQuote
(@lakerice)
Eminent Member
Joined: 4 years ago
Posts: 30
Topic starter  
Posted by: @hans
Posted by: @lakerice

Too bad there aren't more tutorials with 3D printed cases for buttons, etc.

I've been tinkering with several 3D printers as early as 2012, and in the end gave up playing with them around 2015.
I'd be more than happy to write more about using 3D printers - I still love the idea to design something on my computer and have it materialize in plastic 😊 

After buying and selling 3 printers, I have become quite weary about buying a new one.
I do keep an eye on the market for 3D printers, but I still feel that most aren't really suitable for the general public, and promise more than they can deliver.
If you have any experience with a particular brand/model, then please let us know ...

Breadboards are only fun for experiments indeed 😋 

Yes actually my first printer was a used Ultimaker 2, which I believe came out around 2012.  It was pretty horrible; constant clogged filament, stepper motor grinding, annoying bowden tube removal (any time you wanted to change or clean something).  I was quite surprised considering the extreme expense of those printers and the reputation of Ultimaker.  However, printers have come a ways since then. 

The technology is still a lot I think for the general public, but I believe printers are consistently improving. I bought a Prusa i3 MK3 from the Czech Republic where they are made (hardly any retailers carry them in Canada) and it was expensive after duties and conversion from American to Canadian dollars but the difference was night and day compared to my old Ultimaker.  I haven't had a single problem with filament jams, except one grind. Their print beds are heated and self-level so you never have to worry about manually ensuring the nozzle is aligned with the bed, or having a print not stick to the bed.  They are also extremely quiet; like you wouldn't even know it's on if you weren't watching it.

I think for the price, Prusas are comparatively cheaper than a lot of printers and much better for customer service and quality than the knockoff versions from China.  They are very tedious to put together (hundreds of small screws and delicate 3d printed parts) but are cheaper than the ready-made ones.  P.S. I promise I do not get paid for this recommendation; I'm just really impressed with them :)

I think there's a lot of room for experimentation with Fastled/Neopixels, micro controllers and 3d printing.  For example, many CAD files for enclosures for Arduinos exist, but I had to modify them to add a toggle switch and on/off latching switch, which wasn't easy.  It might be interesting to start a sticky thread or article on this site for the purpose of developing 3D printed enclosures/projects with Arduinos and the Fastled/Neopixels libraries.  Just a thought!

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2805
 

3D Printer: I'm afraid we're going a little off-topic here  😊  ... would you mind continuing the conversation in this forum topic - I am very interested in your findings.

3D Printing and Arduino/RPI projects pretty much go hand in hand 😋 


   
ReplyQuote
Share: