touch sensative lamp multiple programs

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

still the same happens...

maybe there is a work around. what about an extra "else" if the lamp is touched longer than 2 second to go in off state and return to the cases when touched again?

You can try this. check if it is greater than or equal to 1 second and less than or equal to 2 seconds.

unsigned long capTime = tapper.capacitiveSensor(30);
buttonState = !!( (capTime >= 1000) && (capTime <= 2000) );

HazardsMind:
You can try this. check if it is greater than or equal to 1 second and less than or equal to 2 seconds.

unsigned long capTime = tapper.capacitiveSensor(30);

buttonState = !!( (capTime >= 1000) && (capTime <= 2000) );

where should i put the "off" string? in the else of the first if?

colorWipe(strip.Color(0, 0, 0), 50, OldTime);

leave it in case 1.

i dont think I really got this.

there do i put the rule that checks if the touch was between 1 and 2 seconds.

i really trying to get a grip on the code :zipper_mouth_face:

Replace:

buttonState = !!(tapper.capacitiveSensor(30) > 8000); //<<------------------ is this correct???

with

unsigned long capTime = tapper.capacitiveSensor(30);
buttonState = !!( (capTime >= 1000) && (capTime <= 2000) );

now it freaks out since it no longer has it capsense threshold.

shouldnt it be like this?

unsigned long capTime = tapper.capacitiveSensor(30);
buttonState = !!( (tapper.capacitiveSensor(30) > 8000) &&  (capTime >= 1000) && (capTime <= 2000) );

Thats not what I gave you.

What happens when you use what I gave you?

then it freaks out. non stop "touches"

What does capTime show you in the serial monitor? Touched and untouched

It sounds like you are having some serious sensitivity issues.

i think the ledstrip and power supply is giving way too much interference.

An isolated touch pad on the lamp might be the solution since the leds and power supply no longer interfere the touched sensor (which in this case is the complete lamp with everything on it.

ill do one more test. ill hook one arduino on my ledstrip and do a strandtest. ill let me other arduino do a sensor test and see what the values will do when performing diferent light modes. maybe this will explain what is going on.

i ve done some tests and i can clearly see a big increase when my leds strip goes dark.

im going to try some changes in the hardware. i think that will fix this issue.

many many thanks for your help so far!