Hi!
We've got a code for a touch sensor in combination with a LED string. Right now when you touch the sensor the lights keep on running for quite a while. Could someone maybe help us out with making sure the lights stop working the moment the sensor is not being touched anymore?
// Henry's Bench
// Capacitive Touch Sensor Tutorial
// When Sig Output is high, touch sensor is being pressed
#include <Adafruit_NeoPixel.h>
#define ctsPin 3 // Pin for capactitive touch sensor
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
void setup() {
Serial.begin(9600);
pinMode(ctsPin, INPUT);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
strip.show();
delay(300);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
void loop() {
int ctsValue = digitalRead(ctsPin);
if (ctsValue == HIGH){
theaterChase(strip.Color(40, 95, 0), 50); // Yellow
strip.show();
Serial.println("TOUCHED");}
else if(ctsValue == LOW){
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, 0,0,0);
strip.show();
delay(0);
}
Serial.println("not touched");
}
}