My project’s goal is a 60 seconds countdown timer made of an Arduino Uno and a 24 NEOPIXEL ring. The animation of the countdown is made of 2 nested loops (see code below).
Also there’s a capacitive touch sensor which should be used to play/pause the countdown (single touch) as well as resetting the timer to 60 seconds (double touch).
I put the reading of the button state into the inner loop of the animation, but since there is a delay(), the frequency of reading the button value is not enogh to grab a double click.
What do I have to do, to make the button fast and responsive?
My code so far:
#include <FastLED.h>
FASTLED_USING_NAMESPACE
// Constants
const int TOUCH_BUTTON_PIN = 2; // Input pin for touch state
const int LED_PIN = 13; // Pin number for LED
// Global Variables
int buttonState = 0; // Variable for reading button
#if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
#warning "Requires FastLED 3.1 or later; check github for latest code."
#endif
#define DATA_PIN 6
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS 24
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 25
#define FRAMES_PER_SECOND 50
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);
// Configure button pin as input
pinMode(TOUCH_BUTTON_PIN, INPUT);
// Configure LED pin as output
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
for (int slowCircle = 0; slowCircle < NUM_LEDS; slowCircle++) {
// clear this led for the next time around the loop
for (int fastCircle = 0; fastCircle < NUM_LEDS; fastCircle++) {
// *** BUTTON ***
// Read the state of the capacitive touch board
buttonState = digitalRead(TOUCH_BUTTON_PIN);
// If a touch is detected, turn on the LED
if (buttonState == HIGH) {
Serial.println("H");
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("L");
}
leds[fastCircle] = CRGB::Red;
leds[slowCircle] = CRGB::Blue;
FastLED.show();
delay(104);
leds[slowCircle] = CRGB::Red;
}
}
}