touch sensative lamp multiple programs

Dear all,

since a few days ive been building my very own LED lamp:

the lamp is fully made out of steel and it has 2 WS2812B LED strip on it (45x2 LED's).
i am controlling it with an Arduino Uno.

this is what i want the lamp to do:

  • touching the for 2> seconds = turn the lamp on to the first light program. (if lamp is on and touched for 2> seconds, the lamp must go off)
  • a brief touch/tap >1 second = lamp much switch to the next light program (im thinking of 10 preset programs at the moment).

thats basically it.
any suggestions are welcome

thanks in advance!

Tymen

Are you able to build your own circuits?, You can use this schematic to make your touch circuit and feed it into the arduino for timing how long it is being touched.


If you want, you can try to modify this touch sensor.
http://www.ebay.com/itm/Digital-Finger-Touch-Sensor-TTP223B-Module-Capacitive-Touch-Switch-for-Arduino-/400842907560?pt=LH_DefaultDomain_0&hash=item5d54195fa8

i already got my touch sensor working like this:

#include <CapacitiveSensor.h>
 
boolean lightOn=false; // I like booleans
CapacitiveSensor   tapper = CapacitiveSensor(4,2); // 750k resistor between pins 0 & 1, pin 1 is sensor pin
unsigned long lastTap; // Used as a kind of a debounce effect
 
void setup()                    
{
pinMode(13, OUTPUT);
digitalWrite(13, lightOn);
lastTap=millis();
}
 
void loop()                    
{
 
    if (tapper.capacitiveSensor(30) > 1000) {
      if ((millis()-lastTap)>8) {
      lightOn=!lightOn;
      lastTap=millis();
      }
    }
    delay(50);
    digitalWrite(13, lightOn);
 
    delay(100);                             
}

i am able to turn my ledpin 13 on and off by touch. but i dont know how to get a counter running with the sensor for my switch case.

this is the code i now use with my push button:

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUM_LEDS 45

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// constants used here to set pin numbers:
const int buttonPin = 10;     // the number of the pushbutton pin

// Variables will change:

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

void setup() {
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
  
  //initialize the serial port
  Serial.begin(9600);
   pinMode(buttonPin, INPUT);  
  //initialize the buttonPin as output
digitalWrite(buttonPin, HIGH); 
}

void loop() {
  
  
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
    // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
      if(buttonPushCounter==6) {
      buttonPushCounter=1;}
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;


switch (buttonPushCounter){
    
  case 1:
     buttonPushCounter==1; {
      rainbow(200);
       break;}
       
        
  case 2:
     buttonPushCounter==2; {
       colorWipe(strip.Color(255, 0, 0), 50); // Red
     break;}
       
  case 3:
     buttonPushCounter==3; {
       colorWipe(strip.Color(0, 255, 0), 50); // green
      break;}
         
  case 4:
     buttonPushCounter==4; {
    colorWipe(strip.Color(0, 0, 255), 50); // blue
      break;}
      
  case 5:
     buttonPushCounter==5; {
    colorWipe(strip.Color(128, 128, 128), 50); // white
      break;}
   }

  
   
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      if (digitalRead(buttonPin) != lastButtonState)  // <------------- add this
       return;         // <------------ and this
      delay(wait);
  }
}
void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    // check if a button pressed
    if (digitalRead(buttonPin) != lastButtonState)  // <------------- add this
       return;         // <------------ and this
    delay(wait);
  }
}
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

This would be equivalent to you reading the state of your button.

buttonState = !!(tapper.capacitiveSensor(30) > 1000); !! will convert any value greater or less than 0 to be seen as 1.

Proof: C++ Shell

Once you have that working, you can look for other durations like 2000 or something else.

this is what i have now. but it does not really work :frowning:

can some one check my code? especially the <<--------------'s

thanks in advance

#include <Adafruit_NeoPixel.h> //ledstrip lib
#include <CapacitiveSensor.h> //capacitive lib
#define PIN 6                 // datapin for led strip
#define NUM_LEDS 45           //number of leds per strip

// initing led strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);


CapacitiveSensor   tapper=CapacitiveSensor(4,2);  // capacitive sending ans sensing pin

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous button state

void setup() {
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  lastTap=millis(); //set millis
  }

void loop() {
  //read the capsensor: <1000=0, >1000=1 (>1000 is touch)
  buttonState = !!(tapper.capacitiveSensor(30) > 1000);  //<<------------------ is this correct???
  
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++; //actually increase counter
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter); //serial print counter
      if(buttonPushCounter==6) {
      buttonPushCounter=1;} // if the counter 6, then the counter will be reset to 1
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

switch (buttonPushCounter){  //the different light programs per counter, 1 to 5
    
  case 1:
     buttonPushCounter==1; {
      rainbow(200);
       break;}
       
        
  case 2:
     buttonPushCounter==2; {
       colorWipe(strip.Color(255, 0, 0), 50); // Red
     break;}
       
  case 3:
     buttonPushCounter==3; {
       colorWipe(strip.Color(0, 255, 0), 50); // green
      break;}
         
  case 4:
     buttonPushCounter==4; {
    colorWipe(strip.Color(0, 0, 255), 50); // blue
      break;}
      
  case 5:
     buttonPushCounter==5; {
    colorWipe(strip.Color(128, 128, 128), 50); // white
      break;}
   }

  
   
}

//presets

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      if (digitalRead(buttonPin) != lastButtonState)  // <<------------- how do i need to edit this?
       return;         
      delay(wait);
  }
}
void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    // check if a button pressed
    if (digitalRead(buttonPin) != lastButtonState)  // <<------------- how do i need to edit this?
       return;         
    delay(wait);
  }
}
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

If you debug the code, what does buttonState show?
Add Serial.println(buttonState); and see what you get.

HazardsMind:
If you debug the code, what does buttonState show?
Add Serial.println(buttonState); and see what you get.

it does see my first touch and sets count to 1.
i do get my first light program, which is nice!

but it doesnt see my other touches. i think it get stuck my my rainbow loop.
this is the current code:
ps. i deleted the check for buttonstate since it was incorrect anyway, it wouldnt let me compile

#include <Adafruit_NeoPixel.h> //ledstrip lib
#include <CapacitiveSensor.h> //capacitive lib
#define PIN 6                 // datapin for led strip
#define NUM_LEDS 45           //number of leds per strip

// initing led strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);


CapacitiveSensor   tapper=CapacitiveSensor(4,2);  // capacitive sending ans sensing pin

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous button state
unsigned long lastTap;

void setup() {
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  lastTap=millis(); //set millis
   Serial.begin(9600);
  }

void loop() {
  //read the capsensor: <1000=0, >1000=1 (>1000 is touch)
  buttonState = !!(tapper.capacitiveSensor(30) > 1000);  //<<------------------ is this correct???
  
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++; //actually increase counter
      Serial.println(buttonState); 
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter); //serial print counter
      if(buttonPushCounter==6) {
      buttonPushCounter=1;} // if the counter 6, then the counter will be reset to 1
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

switch (buttonPushCounter){  //the different light programs per counter, 1 to 5
    
  case 1:
     buttonPushCounter==1; {
      rainbow(200);
       break;}
       
        
  case 2:
     buttonPushCounter==2; {
       colorWipe(strip.Color(255, 0, 0), 50); // Red
     break;}
       
  case 3:
     buttonPushCounter==3; {
       colorWipe(strip.Color(0, 255, 0), 50); // green
      break;}
         
  case 4:
     buttonPushCounter==4; {
    colorWipe(strip.Color(0, 0, 255), 50); // blue
      break;}
      
  case 5:
     buttonPushCounter==5; {
    colorWipe(strip.Color(128, 128, 128), 50); // white
      break;}
   }

  
   
}

//presets

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
              
      delay(wait);
  }
}
void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    // check if a button pressed
          
    delay(wait);
  }
}
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Edit: I made a mistake before, it should be fixed now.

Your rainbow function takes a minimum of 51 seconds to complete, this is because you are using FOR loops which block the code from doing anything until they are done or told to break. I suggest you get rid of the for loops and use IF statements and the arduino’s clock. (millis() and micros() )

Try this:

#include <Adafruit_NeoPixel.h> //ledstrip lib
#include <CapacitiveSensor.h> //capacitive lib
#define PIN 6                 // datapin for led strip
#define NUM_LEDS 45           //number of leds per strip

// initing led strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

CapacitiveSensor   tapper = CapacitiveSensor(4, 2); // capacitive sending ans sensing pin

int buttonPushCounter = 0, lastCount = -1 ;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous button state
unsigned long lastTap, OldTime = 0;
unsigned int Rain = 0, CW = 0;
byte P = strip.numPixels();


void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  lastTap = millis(); //set millis
  Serial.begin(9600);
}

void loop()
{
  //read the capsensor: <1000=0, >1000=1 (>1000 is touch)
  buttonState = !!(tapper.capacitiveSensor(30) > 1000);  //<<------------------ is this correct???

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++; //actually increase counter
      Serial.println(buttonState);
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter); //serial print counter

      if (buttonPushCounter >= 6)
      {
        buttonPushCounter = 1;
      } // if the counter 6, then the counter will be reset to 1
    }
    else
    {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }

  switch (buttonPushCounter)  //the different light programs per counter, 1 to 5
  {
    case 1:
      OldTime = rainbow(200, OldTime);
      break;

    case 2:
      OldTime = colorWipe(strip.Color(255, 0, 0), 50, OldTime); // Red
      break;

    case 3:
      OldTime = colorWipe(strip.Color(0, 255, 0), 50, OldTime); // green
      break;

    case 4:
      OldTime = colorWipe(strip.Color(0, 0, 255), 50, OldTime); // blue
      break;

    case 5:
      OldTime = colorWipe(strip.Color(128, 128, 128), 50, OldTime); // white
      break;
      
      default: break;
  }

  if (buttonPushCounter != lastCount)
  {
    lastCount = buttonPushCounter;
    Rain = 0; // reset the Rain variable in the rainbow function
    CW = 0; // reset the CW variable in the colorWipe function
  }

}

//presets

unsigned long colorWipe(uint32_t c, uint8_t wait, unsigned long prevTime)
{
  unsigned long currentTime = millis();
  if (currentTime - prevTime >= wait)
  {
    if (CW < P)
    {
      strip.setPixelColor(CW, c);
      strip.show();
      CW++;
    }
    else
      CW = 0;

    if (currentTime < prevTime)
      prevTime = 0;

    prevTime += wait;
  }

  return prevTime;
}

unsigned long rainbow(uint8_t wait, unsigned long prevTime) // +-51 seconds
{
  unsigned long currentTime = millis();
  if (currentTime - prevTime >= wait)
  {
    if (Rain < 256)
    {
      for (uint16_t i = 0; i < P; i++) // this should go quite quickly, so you might not need to do anything here
      {
        strip.setPixelColor(i, Wheel((i + Rain) & 255));
      }
      strip.show();
      // check if a button pressed

      Rain++;
    }
    else
      Rain = 0;

    if (currentTime < prevTime)
      prevTime = 0;

    prevTime += wait;
  }

  return prevTime;
}

uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85)
  {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
  else if (WheelPos < 170)
  {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  else
  {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

HazardsMind:
Edit: I made a mistake before, it should be fixed now.

Your rainbow function takes a minimum of 51 seconds to complete, this is because you are using FOR loops which block the code from doing anything until they are done or told to break. I suggest you get rid of the for loops and use IF statements and the arduino’s clock. (millis() and micros() )

Try this:

#include <Adafruit_NeoPixel.h> //ledstrip lib

#include <CapacitiveSensor.h> //capacitive lib
#define PIN 6                // datapin for led strip
#define NUM_LEDS 45          //number of leds per strip

// initing led strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

CapacitiveSensor  tapper = CapacitiveSensor(4, 2); // capacitive sending ans sensing pin

int buttonPushCounter = 0, lastCount = -1 ;  // counter for the number of button presses
int buttonState = 0;        // current state of the button
int lastButtonState = 0;    // previous button state
unsigned long lastTap, OldTime = 0;
unsigned int Rain = 0, CW = 0;
byte P = strip.numPixels();

void setup() {

strip.begin();
  strip.show(); // Initialize all pixels to ‘off’
  lastTap = millis(); //set millis
  Serial.begin(9600);
}

void loop()
{
  //read the capsensor: <1000=0, >1000=1 (>1000 is touch)
  buttonState = !!(tapper.capacitiveSensor(30) > 1000);  //<<------------------ is this correct???

// compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++; //actually increase counter
      Serial.println(buttonState);
      Serial.println(“on”);
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter); //serial print counter

if (buttonPushCounter >= 6)
      {
        buttonPushCounter = 1;
      } // if the counter 6, then the counter will be reset to 1
    }
    else
    {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println(“off”);
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }

switch (buttonPushCounter)  //the different light programs per counter, 1 to 5
  {
    case 1:
      OldTime = rainbow(200, OldTime);
      break;

case 2:
      OldTime = colorWipe(strip.Color(255, 0, 0), 50, OldTime); // Red
      break;

case 3:
      OldTime = colorWipe(strip.Color(0, 255, 0), 50, OldTime); // green
      break;

case 4:
      OldTime = colorWipe(strip.Color(0, 0, 255), 50, OldTime); // blue
      break;

case 5:
      OldTime = colorWipe(strip.Color(128, 128, 128), 50, OldTime); // white
      break;
     
      default: break;
  }

if (buttonPushCounter != lastCount)
  {
    lastCount = buttonPushCounter;
    Rain = 0; // reset the Rain variable in the rainbow function
    CW = 0; // reset the CW variable in the colorWipe function
  }

}

//presets

unsigned long colorWipe(uint32_t c, uint8_t wait, unsigned long prevTime)
{
  unsigned long currentTime = millis();
  if (currentTime - prevTime >= wait)
  {
    if (CW < P)
    {
      strip.setPixelColor(CW, c);
      strip.show();
      CW++;
    }
    else
      CW = 0;

if (currentTime < prevTime)
      prevTime = 0;

prevTime += wait;
  }

return prevTime;
}

unsigned long rainbow(uint8_t wait, unsigned long prevTime) // ±51 seconds
{
  unsigned long currentTime = millis();
  if (currentTime - prevTime >= wait)
  {
    if (Rain < 256)
    {
      for (uint16_t i = 0; i < P; i++) // this should go quite quickly, so you might not need to do anything here
      {
        strip.setPixelColor(i, Wheel((i + Rain) & 255));
      }
      strip.show();
      // check if a button pressed

Rain++;
    }
    else
      Rain = 0;

if (currentTime < prevTime)
      prevTime = 0;

prevTime += wait;
  }

return prevTime;
}

uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85)
  {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
  else if (WheelPos < 170)
  {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  else
  {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

many thanks for your reply.

i would like to add that my previous code went to the first program after a minute ± without any input.

im at work now. ill try the code asap.

the code works! thank you very much! i think ill be able to proceed by my self.

again thanks a lot!

i dont know why or how. but my lamp is doing the first case (off) only half and skips directly to the next case… i only happens with the “off” case (first i had it as the last case, same happens)

#include <Adafruit_NeoPixel.h> //ledstrip lib
#include <CapacitiveSensor.h> //capacitive lib
#define PIN 6                 // datapin for led strip
#define NUM_LEDS 45           //number of leds per strip

// initing led strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

CapacitiveSensor   tapper = CapacitiveSensor(4, 2); // capacitive sending ans sensing pin

int buttonPushCounter = 1, lastCount = -1 ;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous button state
unsigned long lastTap, OldTime = 0;
unsigned int Rain = 0, CW = 0;
byte P = strip.numPixels();


void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  lastTap = millis(); //set millis
  Serial.begin(9600);
}

void loop()
{
  //read the capsensor: <1000=0, >1000=1 (>1000 is touch)
  buttonState = !!(tapper.capacitiveSensor(30) > 8000);  //<<------------------ is this correct???

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      long total = tapper.capacitiveSensor(30);
      buttonPushCounter++; //actually increase counter
      Serial.println(buttonState);
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter); //serial print counter
      Serial.println(total);
      if (buttonPushCounter >= 8)
      {
        buttonPushCounter = 1;
      } // if the counter 6, then the counter will be reset to 1
    }
    else
    {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }

  switch (buttonPushCounter)  //the different light programs per counter, 1 to 8
  {
    case 1:
      OldTime = colorWipe(strip.Color(0, 0, 0), 50, OldTime); // off
      break;
        
    case 2:
      OldTime = colorWipe(strip.Color(218, 168, 30), 0, OldTime); // orange
      break;

    case 3:
      OldTime = colorWipe(strip.Color(32, 178, 170), 50, OldTime); // turquase
      break;

    case 4:
      OldTime = colorWipe(strip.Color(0, 0, 255), 50, OldTime); // blue
      break;
      
     case 5:
      OldTime = colorWipe(strip.Color(72, 0, 139), 50, OldTime); // deep purple
      break;
      
      case 6:
      OldTime = colorWipe(strip.Color(250, 230, 215), 50, OldTime); // white
      break;
      
     case 7:
      OldTime = rainbow(1000, OldTime);
      break;

          
       default: break;
      
  }

  if (buttonPushCounter != lastCount)
  {
    lastCount = buttonPushCounter;
    Rain = 0; // reset the Rain variable in the rainbow function
    CW = 0; // reset the CW variable in the colorWipe function
  }

}

//presets

unsigned long colorWipe(uint32_t c, uint8_t wait, unsigned long prevTime)
{
  unsigned long currentTime = millis();
  if (currentTime - prevTime >= wait)
  {
    if (CW < P)
    {
      strip.setPixelColor(CW, c);
      strip.show();
      CW++;
    }
    else
      CW = 0;

    if (currentTime < prevTime)
      prevTime = 0;

    prevTime += wait;
  }

  return prevTime;
}

unsigned long rainbow(uint8_t wait, unsigned long prevTime) // +-51 seconds
{
  unsigned long currentTime = millis();
  if (currentTime - prevTime >= wait)
  {
    if (Rain < 256)
    {
      for (uint16_t i = 0; i < P; i++) // this should go quite quickly, so you might not need to do anything here
      {
        strip.setPixelColor(i, Wheel((i + Rain) & 255));
      }
      strip.show();
      // check if a button pressed

      Rain++;
    }
    else
      Rain = 0;

    if (currentTime < prevTime)
      prevTime = 0;

    prevTime += wait;
  }

  return prevTime;
}

uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85)
  {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
  else if (WheelPos < 170)
  {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  else
  {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

When that happens, does it look as though it is being touched?

Do these show in the serial monitor when that happens?

Serial.println(buttonState);
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter); //serial print counter
Serial.println(total);

HazardsMind:
When that happens, does it look as though it is being touched?

Do these show in the serial monitor when that happens?

yes, from 8 it skips straight to 2, so i bypasses 1 ??

number of button pushes: 7
2488
off
1
on <------ last real touch
number of button pushes: 8
1285
off
1
on
number of button pushes: 2
3362
off

Ok, I see what is happening. Change your loop function to this.

void loop()
{
  //read the capsensor: <1000=0, >1000=1 (>1000 is touch)
  buttonState = !!(tapper.capacitiveSensor(30) > 8000);  //<<------------------ is this correct???

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      long total = tapper.capacitiveSensor(30);
      
      Serial.println(buttonState);
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter); //serial print counter
      Serial.println(total);

      if (buttonPushCounter > 6) // not 7.
      {
        buttonPushCounter = 1;
      } // if the counter 7, then the counter will be reset to 1
      else 
         buttonPushCounter++; //actually increase counter
    }
    else
    {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }

  switch (buttonPushCounter)  //the different light programs per counter, 1 to 8
  {
    case 1:
      OldTime = colorWipe(strip.Color(0, 0, 0), 50, OldTime); // off
      break;

    case 2:
      OldTime = colorWipe(strip.Color(218, 168, 30), 0, OldTime); // orange
      break;

    case 3:
      OldTime = colorWipe(strip.Color(32, 178, 170), 50, OldTime); // turquase
      break;

    case 4:
      OldTime = colorWipe(strip.Color(0, 0, 255), 50, OldTime); // blue
      break;

    case 5:
      OldTime = colorWipe(strip.Color(72, 0, 139), 50, OldTime); // deep purple
      break;

    case 6:
      OldTime = colorWipe(strip.Color(250, 230, 215), 50, OldTime); // white
      break;

    case 7:
      OldTime = rainbow(1000, OldTime);
      break;


    default: break;

  }

  if (buttonPushCounter != lastCount)
  {
    lastCount = buttonPushCounter;
    Rain = 0; // reset the Rain variable in the rainbow function
    CW = 0; // reset the CW variable in the colorWipe function
  }

}

HazardsMind:
Ok, I see what is happening. Change your loop function to this.

void loop()

{
  //read the capsensor: <1000=0, >1000=1 (>1000 is touch)
  buttonState = !!(tapper.capacitiveSensor(30) > 8000);  //<<------------------ is this correct???

// compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      long total = tapper.capacitiveSensor(30);
     
      Serial.println(buttonState);
      Serial.println(“on”);
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter); //serial print counter
      Serial.println(total);

if (buttonPushCounter > 6) // not 7.
      {
        buttonPushCounter = 1;
      } // if the counter 7, then the counter will be reset to 1
      else
        buttonPushCounter++; //actually increase counter
    }
    else
    {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println(“off”);
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }

switch (buttonPushCounter)  //the different light programs per counter, 1 to 8
  {
    case 1:
      OldTime = colorWipe(strip.Color(0, 0, 0), 50, OldTime); // off
      break;

case 2:
      OldTime = colorWipe(strip.Color(218, 168, 30), 0, OldTime); // orange
      break;

case 3:
      OldTime = colorWipe(strip.Color(32, 178, 170), 50, OldTime); // turquase
      break;

case 4:
      OldTime = colorWipe(strip.Color(0, 0, 255), 50, OldTime); // blue
      break;

case 5:
      OldTime = colorWipe(strip.Color(72, 0, 139), 50, OldTime); // deep purple
      break;

case 6:
      OldTime = colorWipe(strip.Color(250, 230, 215), 50, OldTime); // white
      break;

case 7:
      OldTime = rainbow(1000, OldTime);
      break;

default: break;

}

if (buttonPushCounter != lastCount)
  {
    lastCount = buttonPushCounter;
    Rain = 0; // reset the Rain variable in the rainbow function
    CW = 0; // reset the CW variable in the colorWipe function
  }

}

it still does the same… :frowning:

You might have some kind of interference/sensitivity issue with the sensor that is making it look like it is being touched or possibly even a memory leak somewhere.

The only way for it to increment the buttonPushCounter, the lamp would have to be touched.

If it is a memory leak, then the issue could be with this here

unsigned long colorWipe(uint32_t c, uint8_t wait, unsigned long prevTime)

Try making it unsigned long. Same for your rainbow function.

some other strange stuff is going on as well. it does the deep purple case, but then goes straight to the next when it is done.
and when i change the "off" case (instead of 0,0,0 > 100,100,100) it does the whole strip and goes to the next within 5 seconds. btw, i moved my off function to case 2. but still the same

i find it really hard to troubleshoot myself since i do not really understand you construction, espacially the oldtime part.

Your functions are using the blink without delay method, so what this does is it looks at the time “OldTime” and it compares it to the current time “millis()”. If the difference of the two times are equal or greater than your wait time, then increment the variables in the colorwipe function.

The reason I am saying OldTime = colorWipe(strip.Color(0, 0, 0), 50, OldTime), is because the OldTime is still being used and only when the difference is >= your wait time, does OldTime increment.

Its just a good but confusing way to make sure everything is running on the same timer.

Another thing you can do is move this part before the switch case, and not after it.

if (buttonPushCounter != lastCount)
{
lastCount = buttonPushCounter;
Rain = 0; // reset the Rain variable in the rainbow function
CW = 0; // reset the CW variable in the colorWipe function
}

I don’t have your exact setup, but I do have a single RGB led that I can test with, when I get home tonight. I’ll let you know what I find and how to change it.

HazardsMind:
Your functions are using the blink without delay method, so what this does is it looks at the time "OldTime" and it compares it to the current time "millis()". If the difference of the two times are equal or greater than your wait time, then increment the variables in the colorwipe function.

The reason I am saying OldTime = colorWipe(strip.Color(0, 0, 0), 50, OldTime), is because the OldTime is still being used and only when the difference is >= your wait time, does OldTime increment.

Its just a good but confusing way to make sure everything is running on the same timer.

Another thing you can do is move this part before the switch case, and not after it.
I don't have your exact setup, but I do have a single RGB led that I can test with, when I get home tonight. I'll let you know what I find and how to change it.

thanks you so much for helping me out. you have no idea how much i appreciate this!

this is my boar lay out. i have everything stuck down on the foot plate. i do see a massive increase of sensor results when i plug the main power in (5V 6A adapter). i have taped my led strip directly on the metal (only a thin layer of clear paint). i noticed when i use white colors the value seems to increase.

Give this a try, if you still get the same issues, then the problem is with your touch sensor.

When using the Serial monitor, you can save SRAM (Arduino has a limited amount) by putting your unchanging strings into the flash memory with the F() macro. Look for it in the code to see what i’m talking about.

#include <Adafruit_NeoPixel.h> //ledstrip lib
#include <CapacitiveSensor.h> //capacitive lib
#define PIN 6                 // datapin for led strip
#define NUM_LEDS 45           //number of leds per strip
// initing led strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

//CapacitiveSensor   tapper = CapacitiveSensor(4, 2); // capacitive sending ans sensing pin

int buttonPushCounter = 0, lastCount = -1 ;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous button state
unsigned long lastTap, OldTime = 0;
unsigned int Rain = 0, CW = 0;
byte P = strip.numPixels();


void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  lastTap = millis(); //set millis
  Serial.begin(9600);
}

void loop()
{
  //read the capsensor: <1000=0, >1000=1 (>1000 is touch)
  buttonState = !!(tapper.capacitiveSensor(30) > 8000);  //<<------------------ is this correct???

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is HIGH then the button
      // wend from off to on:
      //long total = tapper.capacitiveSensor(30);
      buttonPushCounter++; //actually increase counter
      if (buttonPushCounter > 7)
      {
        buttonPushCounter = 1;
      } // if the counter 6, then the counter will be reset to

      Serial.println(F("on"));
      Serial.print(F("number of button pushes:  "));
      Serial.println(buttonPushCounter); //serial print counter
      //Serial.println(total);
    }
    else
    {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println(F("off"));
    }
    // save the current state as the last state,
    //for next time through the loop
    lastButtonState = buttonState;
  }

  if (buttonPushCounter != lastCount)
  {
    lastCount = buttonPushCounter;
    Rain = 0; // reset the Rain variable in the rainbow function
    CW = 0; // reset the CW variable in the colorWipe function
    Serial.print(F("Case: "));
    Serial.println(buttonPushCounter);
  }

  switch (buttonPushCounter)  //the different light programs per counter, 1 to 8
  {
    case 1:
      OldTime = colorWipe(strip.Color(0, 0, 0), 50, OldTime); // off
      break;

    case 2:
      OldTime = colorWipe(strip.Color(218, 168, 30), 0, OldTime); // orange
      break;

    case 3:
      OldTime = colorWipe(strip.Color(32, 178, 170), 50, OldTime); // turquase
      break;

    case 4:
      OldTime = colorWipe(strip.Color(0, 0, 255), 50, OldTime); // blue
      break;

    case 5:
      OldTime = colorWipe(strip.Color(72, 0, 139), 50, OldTime); // deep purple
      break;

    case 6:
      OldTime = colorWipe(strip.Color(250, 230, 215), 50, OldTime); // white
      break;

    case 7:
      OldTime = rainbow(1000, OldTime);
      break;

    default: break;
  }
}

//presets

unsigned long colorWipe(uint32_t c, unsigned long wait, unsigned long prevTime)
{
  unsigned long currentTime = millis();
  if (currentTime - prevTime >= wait)
  {
    if (CW < P)
    {
      strip.setPixelColor(CW, c);
      strip.show();
      CW++;
    }
    else
      CW = 0;

    if (currentTime < prevTime)
      prevTime = 0;

    prevTime += wait;
  }

  return prevTime;
}

unsigned long rainbow(unsigned long wait, unsigned long prevTime) // +-51 seconds
{
  unsigned long currentTime = millis();
  if (currentTime - prevTime >= wait)
  {
    if (Rain < 256)
    {
      for (uint16_t i = 0; i < P; i++) // this should go quite quickly, so you might not need to do anything here
      {
        strip.setPixelColor(i, Wheel((i + Rain) & 255));
      }
      strip.show();
      // check if a button pressed

      Rain++;
    }
    else
      Rain = 0;

    if (currentTime < prevTime)
      prevTime = 0;

    prevTime += wait;
  }

  return prevTime;
}

uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85)
  {
    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
  else if (WheelPos < 170)
  {
    WheelPos -= 85;
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  else
  {
    WheelPos -= 170;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}