#include #ifdef __AVR__ #include // Required for 16 MHz Adafruit Trinket #endif // Digital IO pin connected to the button. This will be driven with a // pull-up resistor so the switch pulls the pin to ground momentarily. // On a high -> low transition the button press logic will execute. #define BUTTON_PIN 2 #define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels. #define PIXEL_COUNT 81 // Number of NeoPixels // Declare our NeoPixel strip object: Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800); // Argument 1 = Number of pixels in NeoPixel strip // Argument 2 = Arduino pin number (most are valid) // Argument 3 = Pixel type flags, add together as needed: // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) int numPixels = 81; boolean oldState = HIGH; boolean oneTime = LOW; int mode = 0; // Currently-active animation mode, 0-9 int numOfMode = 4; // x-1 int oldMode = numOfMode + 1; long button_pressed = 0; boolean pressed = LOW; int brithness = 254; //Manage brithness boolean getDark = LOW; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); strip.begin(); // Initialize NeoPixel strip object (REQUIRED) strip.setBrightness(brithness); strip.show(); // Initialize all pixels to 'off' } void loop() { // Get current button state. boolean newState = digitalRead(BUTTON_PIN); // Check if state changed from LOW to HIGH (button press). if ((newState == HIGH) && (oldState == LOW)) { // Short delay to debounce button. delay(20); // Check if button is still low after debounce. newState = digitalRead(BUTTON_PIN); if (newState == HIGH) { // Yes, still HIGH //start analyse if long press or short press button_pressed = millis(); pressed = HIGH; } } //if it was pressed analyse if (pressed == HIGH){ //get Stystem time int time_system = millis(); //calculate pressed time int pressed_time = time_system - button_pressed; if ((newState == LOW) && (pressed_time <= 1000)){ ++mode; if (mode > numOfMode) { mode = 0; // Advance to next mode, wrap around after #8 } //if lamp is off, next tapp will activat it again if (brithness == 0){ mode = 0; brithness = 254; strip.setBrightness(brithness); strip.show(); } //deaktivate press analyse pressed = LOW; }else if ((newState == HIGH) && (pressed_time > 1000)&& (brithness > 0)){ //Get darker or brither if (getDark == HIGH){ brithness = strip.getBrightness(); if (brithness < 254){ brithness = brithness + 1; if (brithness > 254){ brithness = 254; } strip.setBrightness(brithness); strip.show(); } }else{ brithness = strip.getBrightness(); if (brithness > 0){ brithness = brithness - 1; if (brithness < 0){ brithness = 0; } if (brithness == 0){ //strip is off mode = -1; pressed = LOW; } strip.setBrightness(brithness); strip.show(); } } }else if ((newState == LOW) && (pressed_time > 1000)&& (brithness > 0)){ //Manage state after getting dark or ligther switch getDark = !getDark; //deaktivate press analyse pressed = LOW; } } Serial.println(mode); if (oldMode != mode) { oneTime = HIGH; } else { oneTime = LOW; } if (mode == 4) { rainbow(2, oneTime); } if (mode == 3) { colorWipe(strip.Color( 0, 255, 0), 10, oneTime); } if (mode == 2) { colorWipe(strip.Color( 255, 0, 255), 10, oneTime); } if (mode == 1) { colorWipe(strip.Color( 0, 255, 255), 10, oneTime); } if (mode == 0) { colorWipe(strip.Color( 255, 255, 255), 10, oneTime); } if (mode == -1) { colorWipe(strip.Color( 0, 0, 0), 10, oneTime); } // Set the last-read button state to the old state. oldState = newState; oldMode = mode; delay(5); } // Fill strip pixels one after another with a color. Strip is NOT cleared // first; anything there will be covered pixel by pixel. Pass in color // (as a single 'packed' 32-bit value, which you can get by calling // strip.Color(red, green, blue) as shown in the loop() function above), // and a delay time (in milliseconds) between pixels. int i = 0; void colorWipe(uint32_t color, int wait, boolean oneTime) { if (oneTime == HIGH) { i = 0; } if (i < numPixels) { //ad one more pixel strip.setPixelColor(i, color); // Set pixel's color (in RAM) strip.show(); // Update strip to match ++i; delay(wait); // Pause for a moment } } long firstPixelHue = 0; // Rainbow cycle along whole strip. Pass delay time (in ms) between frames. void rainbow(int wait, boolean oneTime) { if (oneTime == HIGH) { firstPixelHue = 0; i = 0; } if (firstPixelHue < 3 * 65536) { if (i < numPixels) { // Offset pixel hue by an amount to make one full revolution of the // color wheel (range of 65536) along the length of the strip // (strip.numPixels() steps): int pixelHue = firstPixelHue + (i * 65536L / 500); // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or // optionally add saturation and value (brightness) (each 0 to 255). // Here we're using just the single-argument hue variant. The result // is passed through strip.gamma32() to provide 'truer' colors // before assigning to each pixel: strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue))); ++i; }else{ i = 0; } firstPixelHue += 256; strip.show(); // Update strip with new contents delay(wait); // Pause for a moment }else{ firstPixelHue = 0; } }
|