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.