No title

 #include <Adafruit_NeoPixel.h>

#include "BluetoothSerial.h"


#define LED_PIN 6

#define NUM_LEDS 60


BluetoothSerial SerialBT;

Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);


int currentEffect = 0;

const int totalEffects = 34;


// ---------- BASIC FUNCTIONS ----------

void showColor(uint32_t color) {

  strip.fill(color);

  strip.show();

}


void clearStrip() {

  strip.clear();

  strip.show();

}


// ---------- EFFECTS ----------

void effectBlink(uint32_t c) {

  showColor(c); delay(300);

  clearStrip(); delay(300);

}


void effectColorWipe(uint32_t c) {

  for (int i = 0; i < NUM_LEDS; i++) {

    strip.setPixelColor(i, c);

    strip.show();

    delay(20);

  }

}


void effectRainbow() {

  for (int j = 0; j < 256; j++) {

    for (int i = 0; i < NUM_LEDS; i++) {

      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV((i * 256 / NUM_LEDS + j) * 256)));

    }

    strip.show();

    delay(10);

  }

}


void theater(uint32_t c) {

  for (int q = 0; q < 3; q++) {

    for (int i = 0; i < NUM_LEDS; i += 3) {

      strip.setPixelColor(i + q, c);

    }

    strip.show();

    delay(100);

    for (int i = 0; i < NUM_LEDS; i += 3) {

      strip.setPixelColor(i + q, 0);

    }

  }

}


void meteor(uint32_t c) {

  clearStrip();

  for (int i = 0; i < NUM_LEDS + 10; i++) {

    if (i < NUM_LEDS)

      strip.setPixelColor(i, c);

    if (i - 10 >= 0 && i - 10 < NUM_LEDS)

      strip.setPixelColor(i - 10, 0);

    strip.show();

    delay(30);

  }

}


// ---------- EFFECT SWITCH ----------

void runEffect(int e) {

  switch (e) {


    case 0: showColor(strip.Color(255,0,0)); break;

    case 1: showColor(strip.Color(0,255,0)); break;

    case 2: showColor(strip.Color(0,0,255)); break;

    case 3: showColor(strip.Color(255,255,255)); break;


    case 4: effectBlink(strip.Color(255,0,0)); break;

    case 5: effectBlink(strip.Color(0,255,0)); break;

    case 6: effectBlink(strip.Color(0,0,255)); break;


    case 7: effectColorWipe(strip.Color(255,0,0)); break;

    case 8: effectColorWipe(strip.Color(0,255,0)); break;

    case 9: effectColorWipe(strip.Color(0,0,255)); break;


    case 10: effectRainbow(); break;


    case 11: theater(strip.Color(255,0,0)); break;

    case 12: theater(strip.Color(0,255,0)); break;

    case 13: theater(strip.Color(0,0,255)); break;


    case 14: meteor(strip.Color(255,255,255)); break;

    case 15: meteor(strip.Color(255,0,0)); break;

    case 16: meteor(strip.Color(0,255,0)); break;

    case 17: meteor(strip.Color(0,0,255)); break;


    case 18: effectBlink(strip.Color(255,255,0)); break;

    case 19: effectBlink(strip.Color(255,0,255)); break;

    case 20: effectBlink(strip.Color(0,255,255)); break;


    case 21: effectColorWipe(strip.Color(255,255,0)); break;

    case 22: effectColorWipe(strip.Color(255,0,255)); break;

    case 23: effectColorWipe(strip.Color(0,255,255)); break;


    case 24: theater(strip.Color(255,255,255)); break;


    case 25: showColor(strip.Color(255,255,0)); break;

    case 26: showColor(strip.Color(255,0,255)); break;

    case 27: showColor(strip.Color(0,255,255)); break;


    case 28: effectBlink(strip.Color(128,0,255)); break;

    case 29: effectColorWipe(strip.Color(128,0,255)); break;


    case 30: theater(strip.Color(128,0,255)); break;

    case 31: meteor(strip.Color(128,0,255)); break;


    case 32: effectBlink(strip.Color(50,50,50)); break;

    case 33: effectColorWipe(strip.Color(50,50,50)); break;

  }

}


// ---------- SETUP ----------

void setup() {

  strip.begin();

  strip.setBrightness(100);

  strip.show();


  SerialBT.begin("ESP32_LED");

}


// ---------- LOOP ----------

void loop() {

  if (SerialBT.available()) {

    String cmd = SerialBT.readStringUntil('\n');

    cmd.trim();


    if (cmd == "R") showColor(strip.Color(255,0,0));

    else if (cmd == "G") showColor(strip.Color(0,255,0));

    else if (cmd == "B") showColor(strip.Color(0,0,255));

    else if (cmd == "Y") showColor(strip.Color(255,255,0));

    else if (cmd == "P") showColor(strip.Color(255,0,255));

    else if (cmd == "U") showColor(strip.Color(255,105,180));


    else if (cmd == "E+") {

      currentEffect++;

      if (currentEffect >= totalEffects) currentEffect = 0;

    }

    else if (cmd == "E-") {

      currentEffect--;

      if (currentEffect < 0) currentEffect = totalEffects - 1;

    }

  }


  runEffect(currentEffect);

}

Post a Comment

Previous Post Next Post

Contact Form