I've got a weird issue while using the FastLED library. I have an Uno board, a 10 neopixel strip, a 10k potentiometer, and a tactile button. As the code below shows, I have a timer that changes the mode every 10 seconds. That works, however the timing is off. The modes switch about every 5-6 seconds when serial communication is off and every 8 when serial prints are on. What could be causing this?
Here is my sketch:
#include "FastLED.h"
//#define DEBUG
#define NUM_LEDS 10
#define DATA_PIN 12
#define BUTTON_PIN 3
#define POT_PIN A0
CRGB leds[NUM_LEDS];
int stripState = 0;
int caseMax = 7;
int buttonState = LOW;
int lastButtonState = LOW;
long lastDebounceTime = 0;
int debounceDelay = 50;
long modeTime;
int modeDelay = 10000;
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
Serial.println(millis());
button();
modeTimer();
mode();
FastLED.show();
}
void potBrightness() {
FastLED.setBrightness(map(analogRead(POT_PIN), 0, 1023, 0, 255));
}
void button() {
int buttonReading = digitalRead(BUTTON_PIN);
if (buttonReading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonReading != buttonState) {
buttonState = buttonReading;
if (buttonState == LOW) {
stripState = (stripState + 1) % caseMax;
modeTime = millis();
}
}
}
lastButtonState = buttonReading;
}
void modeTimer() {
if (millis() > modeTime + modeDelay) {
modeTime = millis();
stripState = (stripState + 1) % caseMax;
}
}
void debug() {
#ifdef DEBUG
Serial.print("Case: ");
Serial.print(stripState);
Serial.print("\t");
Serial.print("Millis: ");
Serial.print(millis());
Serial.println("\t");
Serial.print("Last Switch:");
Serial.println(modeTime);
Serial.print("\t");
Serial.print("Brightness");
Serial.println(map(analogRead(POT_PIN), 0, 1000, 0, 255));
#endif
}
void mode() {
switch (stripState) {
case 0:
debug();
changeCRGBPixels(CRGB::Black, NUM_LEDS);
break;
case 1:
debug();
FastLED.setBrightness(50);
changeCRGBPixels(CRGB::White, NUM_LEDS);
break;
case 2:
debug();
changeCRGBPixels(CRGB::White, NUM_LEDS);
potBrightness();
break;
case 3:
debug();
changeCRGBPixels(CRGB::Blue, NUM_LEDS);
potBrightness();
break;
case 4:
debug();
changeCRGBPixels(CRGB::Green, NUM_LEDS);
potBrightness();
break;
case 5:
debug();
changeCHSVPixels(map(analogRead(POT_PIN), 0, 1023, 0, 255), 255, 255);
break;
case 6:
debug();
int ledsOn = map(analogRead(POT_PIN), 0, 1000, 0, NUM_LEDS);
if (ledsOn < NUM_LEDS) {
changeCRGBPixels(CRGB::Black, NUM_LEDS);
}
changeCRGBPixels(CRGB::Cyan, ledsOn);
break;
}
}
void changeCRGBPixels(uint32_t color, int numLEDs) {
for (int i = 0; i < numLEDs; i++) {
leds[i] = color;
}
}
void changeCHSVPixels(int h, int s, int v) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(h, s, v);
}
}
and a more simple sketch:
#include "FastLED.h"
#define NUM_LEDS 10
#define DATA_PIN 12
#define BUTTON_PIN 3
#define POT_PIN A0
CRGB leds[NUM_LEDS];
int stripState = 0;
int caseMax = 2;
int buttonState = LOW;
int lastButtonState = LOW;
long lastDebounceTime = 0;
int debounceDelay = 50;
long modeTime;
int modeDelay = 10000;
int buttonReading;
void setup() {
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
}
void loop() {
buttonReading = digitalRead(BUTTON_PIN);
Serial.println(digitalRead(BUTTON_PIN));
/*
if (buttonReading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonReading != buttonState) {
buttonState = buttonReading;
if (buttonState == LOW) {
stripState = (stripState + 1) % caseMax;
modeTime = millis();
}
}
}
lastButtonState = buttonReading;
*/
if (millis() > modeTime + modeDelay) {
modeTime = millis();
stripState = (stripState + 1) % caseMax;
}
if (stripState == 0) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White;
}
}
if (stripState == 1) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(analogRead(POT_PIN), 255, 255);
}
}
FastLED.show();
}
test timer:
long modeTime;
int modeDelay = 10000;
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
if (millis() > modeTime + modeDelay) {
modeTime = millis();
digitalWrite(13, !digitalRead(13));
}
}
I tested it with another Uno board, different power source (9v battery/USB), different sketches. I also made sure that the board works with a simple timer and no added libraries. Test turns the on board led on and off every 10 seconds.
**What could be causing this difference in time? **