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?