Well, i am very new at this arduino thing and its awesome.
Shortly i made this RGB light thing for my room and i putted button.
I wanted, when you hold button like 3000 milliseconds it turn rgb off, but i have used 16 hours at internet trying find solution for that. I have tried every thing. Help me.
#include <Adafruit_NeoPixel.h>
//this should work
#define BUTTON_LEAD 12
#define LED_LEAD 6
#define NUMBER_OF_PIXELS 60
#define NUMBER_OF_MODES 6
#define NUM_LEDS 60
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, 6, NEO_GRB + NEO_KHZ800);
bool buttonState = LOW;
bool lastButtonState = LOW;
//delay between pixel flash
const int PIXEL_FLASH_INTERVAL =1 ;//original 25
unsigned long previousMillis = millis();
int mode = 1; // Default mode is one.
byte colors[3][3] = { {0xff, 0,0xff}, //magenta
{0xff, 0, 0}, //red
{0 , 0xff , 0xff} }; //turqoise
void setup() {
pinMode(BUTTON_LEAD, INPUT_PULLUP);
strip.begin();
strip.show();
}
int pos = 0, dir = 1; // Position, direction of "eye"
void loop() {
switch (mode) {
case 1:
rainbow();
break;
case 2:
BouncingColoredBalls(3, colors);
//colorWipe(strip.Color(0, 255, 0));
break;
case 3:
scanner();
break;
case 4:
colorWipe(50);
break;
case 5:
theaterChaseRainbow(20);
break;
case 6:
theaterChase(strip.Color(3, 253, 170), 50); // sinivihree
theaterChase(strip.Color(253, 171, 67), 50); // valkonen
theaterChase(strip.Color(248, 12, 198), 50); // pinkki
break;
default:
mode = 1;
break;
}
}
/*colorWipe function*/
void colorWipe(uint8_t wait) {
uint32_t c = strip.Color(255, 0, 0); //starts as red
int b = 0;
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i,c);
strip.show();
delay(wait);
if (buttonListener()) {
clearPixel();
break;
}
if(i == 59){
b++;
}
if(b == 1){
if(i == 59){
c = strip.Color(12, 240, 214); //sets color to aqua
i = -1;
}
}
if(b == 2){
if(i == 59){
c = strip.Color(5, 233, 3); //sets color to green
i = -1;
}
}
if(b == 3){
if(i == 59){
b = 0;
i = -1;
c = strip.Color(255, 0, 0); //sets color back to red
}
}
}
}
/* rainbow function */
void rainbow() {
uint16_t j = 0;
while(j<256*3) {
if(millis() - previousMillis >= PIXEL_FLASH_INTERVAL) {
previousMillis = millis();
uint16_t i = 0;
while(i < strip.numPixels()) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
i++;
if(buttonListener()) { return; }
}
strip.show();
j++;
}
}
}
/* Wheel function */
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
/* monitor button press */
bool buttonListener() {
bool modeChanged = false;
buttonState = digitalRead(BUTTON_LEAD);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
mode++;
modeChanged = true;
delay(250); // Debounce delay
}
}
lastButtonState = buttonState;
return modeChanged;
}
void scanner() {
if (millis() - previousMillis >= PIXEL_FLASH_INTERVAL) {
previousMillis = millis();
if(buttonListener()) { return; }
clearPixel();
int j;
// Draw 5 pixels centered on pos. setPixelColor() will clip any
// pixels off the ends of the strip
strip.setPixelColor(pos - 2, 0x0000FF); // blue
strip.setPixelColor(pos - 1, 0x00FF00); // green
strip.setPixelColor(pos , 0xFF0000); // red
strip.setPixelColor(pos + 1, 0x00FF00); // green
strip.setPixelColor(pos + 2, 0x0000FF); // blue
strip.show();
//delay(60);
// Rather than being sneaky and erasing just the tail pixel,
// it's easier to erase it all and draw a new one next time.
for(j=-2; j<= 2; j++) strip.setPixelColor(pos+j, 0);
// Bounce off ends of strip
pos += dir;
if(pos < 0) {
pos = 1;
dir = -dir;
} else if(pos >= strip.numPixels()) {
pos = strip.numPixels() - 2;
dir = -dir;
}
}
}
void BouncingColoredBalls(int BallCount, byte colors[][3]) {
float Gravity = -9.81;
int StartHeight = 1;
float Height[BallCount];
float ImpactVelocityStart = sqrt( -2 * Gravity * StartHeight );
float ImpactVelocity[BallCount];
float TimeSinceLastBounce[BallCount];
int Position[BallCount];
long ClockTimeSinceLastBounce[BallCount];
float Dampening[BallCount];
for (int i = 0 ; i < BallCount ; i++) {
ClockTimeSinceLastBounce[i] = millis();
Height[i] = StartHeight;
Position[i] = 0;
ImpactVelocity[i] = ImpactVelocityStart;
TimeSinceLastBounce[i] = 0;
Dampening[i] = 0.90 - float(i)/pow(BallCount,2);
}
while (true) {
for (int i = 0 ; i < BallCount ; i++) {
TimeSinceLastBounce[i] = millis() - ClockTimeSinceLastBounce[i];
Height[i] = 0.5 * Gravity * pow( TimeSinceLastBounce[i]/1000 , 2.0 ) + ImpactVelocity[i] * TimeSinceLastBounce[i]/1000;
if ( Height[i] < 0 ) {
Height[i] = 0;
ImpactVelocity[i] = Dampening[i] * ImpactVelocity[i];
ClockTimeSinceLastBounce[i] = millis();
if ( ImpactVelocity[i] < 0.01 ) {
ImpactVelocity[i] = ImpactVelocityStart;
}
}
Position[i] = round( Height[i] * (NUM_LEDS - 1) / StartHeight);
}
for (int i = 0 ; i < BallCount ; i++) {
setPixel(Position[i],colors[i][0],colors[i][1],colors[i][2]);
}
if(buttonListener()) { return; }
showStrip();
clearPixel();
}
}
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
for (int q=0; q < 3; q++) {
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
strip.show();
delay(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
if(buttonListener()) { return; }
}
}
}
}
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(wait);
for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
if(buttonListener()) { return; }
}
}
}
}
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
void clearPixel(){
for(int i=0;i<NUMBER_OF_PIXELS;i++){
strip.setPixelColor(i, 0,0,0);
}
strip.show();
}