help with code

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");
  
}
}

Right now when you touch the sensor the lights keep on running for quite a while.

Could thisdelay(300); be part of your problem?

Thank you for response! The delay you are mentioning has to do with the time in between the blinking, not with the time of the entire blinking circuit. I think we probably need some extra lines of coding to get the blinking stopped when the sensor is not being touched.

for (int j=0; j<10; j++) {  //do 10 cycles of chasing

It runs 10 times when theaterChase is executed. Remove this line and see if it helps

marijnbril:
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");
 
}
}

The program tests for ctsValue being HIGH, then if not HIGH, test for ctsValue being LOW. Are there other possible values for ctsValue? If not, there is no need to do the second test as it is logical if not HIGH, the only other option is LOW.

How long does ctsValue remain HIGH after being touched? Does it need to be set LOW by the program?

Paul

marijnbril:
Right now when you touch the sensor the lights keep on running for quite a while.

Almost certainly that is due to the use of delay() as @AWOL pointed out.

The Arduino can do nothing during a delay(). Have a look at how millis() is used to manage timing without blocking in several things at a time

...R