But then I enabled WiFi. Now if I have wifi going, and I press the button, the button constantly cycles while holding it down. Like its bouncing or something. And it cycles through all my light types. But if I increase my interval for checking my debounce time to something high (like 250ms) I do NOT get the 'bouncing' button with WiFi enabled.
Here is my code. If I comment out setup_wifi(); in the main setup, I have no issues. If I change 100 to 250 in the btnISR() I have no problems. Neither of which I want to do. Anyone have any ideas what I'm not understanding.
(sorry the code is messy)
Code: Select all
#include <FastLED.h>
#include <WiFi.h>
#include <PubSubClient.h>
// WIFI
char ssid[] = "##";
const char* password = "##";
// Static IP Stuff
IPAddress local_IP(##);
IPAddress gateway(##);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);
// The definitions to control the actual LEDs through the FastLED library
#define LED_PIN 23
#define NUM_LEDS 8
#define BRIGHTNESS 15 // Up to 255
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
// Set up some pallete variables to be used later
CRGBPalette16 currentPalette;
TBlendType currentBlending;
// Set up the buttons(as struct) for controlling the lights
struct Button {
const uint8_t PIN; // Pin the button is attached to
uint32_t station; // station we are 'at'. 0 = white reading, 1 = rainbow, etc
bool pressed; // last pressed value (true/false)
};
Button btn01 = {39, 0, true};
/*
* Intterrupt for button push
*
* IRAM_ATTR is specific for the ESP32 to run faster -- loads it into RAM instead of Flash memory
*/
void IRAM_ATTR btnISR() {
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If I make this 100 changed to 250, i have no issue
if (interrupt_time - last_interrupt_time > 100)
{
// Do stuff here
btn01.pressed = true;
btn01.station += 1;
if (btn01.station > 6) {
btn01.station = 0;
}
}
last_interrupt_time = interrupt_time;
}
/*
* setup_wifi()
* initial connection to wifi
*/
void setup_wifi() {
// This calls the config to load in the static stuff
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
} // end setup_wifi
void fillLEDsFromPalette( uint8_t colorIndex ) {
uint8_t brightness = 35;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending );
colorIndex += 3;
}
}
void setup() {
Serial.begin(115200);
// If I comment out this, and don't initialize wifi, everything works as expected
setup_wifi();
pinMode(btn01.PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(btn01.PIN), btnISR, RISING);
delay( 1500 ); // power-up safety delay
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness( BRIGHTNESS );
currentBlending = LINEARBLEND;
} // end setup()
void loop() {
static uint8_t startIndex = 0;
startIndex = startIndex + 1;
if (btn01.station == 0 and btn01.pressed) {
Serial.println("White");
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White;
}
btn01.pressed = false;
}
if (btn01.station == 1 and btn01.pressed) {
Serial.println("Red");
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red;
}
btn01.pressed = false;
}
if (btn01.station == 2 and btn01.pressed) {
Serial.println("Green");
for( int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Green;
}
btn01.pressed = false;
}
if (btn01.station == 3) {
Serial.println("Rainbow");
currentPalette = RainbowColors_p;
fillLEDsFromPalette( startIndex ) ;
btn01.pressed = false;
}
FastLED.show();
FastLED.delay(50);
} // end void loop()