Untitled

 avatar
unknown
plain_text
a month ago
2.1 kB
2
Indexable
#include <Adafruit_NeoPixel.h>

#define SPEAKER  4
#define BUTTON_PIN 3
#define LED_PIN 2
#define LED_COUNT  12
#define BRIGHTNESS 255

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_RGBW);

void setup() {
  Serial.begin(9600);

  pinMode(BUTTON_PIN, INPUT);
  pinMode(SPEAKER, OUTPUT);
  digitalWrite(SPEAKER,LOW);
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
  strip.setBrightness(BRIGHTNESS);
  pulseWhite(0.5, false);
}

void loop() {
  // read the state of the pushbutton value:
  int readButtonState = digitalRead(BUTTON_PIN);
  if (readButtonState != buttonState) {
    if(readButtonState == HIGH) {
      onButtonClick();
    } else {
      onButtonRelease();
    }
  }
  buttonState = readButtonState;
}

void onButtonClick() {
    for(int j=0; j<256;j++) {
      strip.fill(strip.Color(255, 255, 255, strip.gamma8(j)));
      strip.show();
      delay(1);
    }
        digitalWrite(SPEAKER,HIGH);
        delay(900);
}

void onButtonRelease() {
      for(int j=255; j>=0; j = j - 1) { // Ramp down from 255 to 0
      strip.fill(strip.Color(j, j, j, strip.gamma8(j)));
      strip.show();
    }

          digitalWrite(SPEAKER,LOW);
      delay(900);

    strip.fill(strip.Color(0,0,0,0));
    strip.show();
  
}

void pulseWhite(uint8_t wait, bool withSound) {
    for(int j=0; j<256;j++) {
      strip.fill(strip.Color(255, 255, 255, strip.gamma8(j)));
      strip.show();
      delay(1);
    }

    if(withSound) {
        digitalWrite(SPEAKER,HIGH);
        delay(wait * 500);
    }


    for(int j=255; j>=0; j = j - 1) { // Ramp down from 255 to 0
      strip.fill(strip.Color(j, j, j, strip.gamma8(j)));
      strip.show();
    }

    if(withSound) {
      digitalWrite(SPEAKER,LOW);
      delay(wait * 500);
    }

    strip.fill(strip.Color(0,0,0,0));
    strip.show();
}
Leave a Comment