How to Control WS2812B Pixel LEDs with Arduino using Push Button







            CODE 

#include <Adafruit_NeoPixel.h>

#include <EEPROM.h>


#define LED_PIN 6

#define NUM_LEDS 60

#define BTN_NEXT 2

#define BTN_PREV 3

#define POT_PIN A0


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


int currentEffect = 0;

int totalEffects = 35;   // UPDATED TOTAL

int brightness = 100;

bool lastNext = HIGH, lastPrev = HIGH;

bool effectChanged = true;

unsigned long effectTimer = 0;


uint16_t effectStep = 0;


// ==== Effect Names ====

const char* effectNames[] = {

  "Red", "Green", "Blue", "Rainbow", "Yellow Wipe", "Cyan Wipe",

  "Red Theater", "Green Theater", "Blue Theater", "Purple Pulse",

  "White Pulse", "Rainbow Fast", "Orange Wipe", "Violet Wipe",

  "Sky Wipe", "Yellow Pulse", "Cyan Pulse", "White Theater",

  "Rainbow Smooth", "Random Color", "Fire", "Police", "Meteor",

  "Wave", "Rainbow Cycle",


  // NEW ADDED EFFECTS

  "Blue Meteor", "Purple Meteor", "Red Meteor", "Green Meteor",

  "Red Wave", "Green Wave", "Purple Wave",

  "Purple Wipe", "Purple Theater", "Purple Color"

};


// ==== Utility ====

void showColor(uint32_t color) {

  for (int i = 0; i < NUM_LEDS; i++) strip.setPixelColor(i, color);

  strip.show();

}


void resetEffect() {

  effectStep = 0;

  effectTimer = millis();

  strip.clear();

  strip.show();

}


// ==== Existing Effects ====

// (unchanged — keeping your functions)


void effectRainbow() {

  if (millis() - effectTimer > 20) {

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

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

    }

    strip.show();

    effectStep++;

    effectTimer = millis();

  }

}


void effectColorWipe(uint32_t color, int speed) {

  if (millis() - effectTimer > speed) {

    if (effectStep < NUM_LEDS) {

      strip.setPixelColor(effectStep, color);

      strip.show();

      effectStep++;

    } else {

      effectStep = 0;

      strip.clear();

    }

    effectTimer = millis();

  }

}


void effectTheaterChase(uint32_t color, int speed) {

  if (millis() - effectTimer > speed) {

    strip.clear();

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

      strip.setPixelColor(i, color);

    }

    strip.show();

    effectStep++;

    effectTimer = millis();

  }

}


void effectPulse(uint32_t color, int speed) {

  int level = abs((int)(sin(effectStep * 0.1) * 255));

  strip.setBrightness(level);

  showColor(color);

  effectStep++;

  delay(speed);

}


void fireEffect() {

  if (millis() - effectTimer > 30) {

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

      int flicker = random(120, 255);

      strip.setPixelColor(i, strip.Color(flicker, random(0, flicker / 2), 0));

    }

    strip.show();

    effectTimer = millis();

  }

}


void policeEffect() {

  if (millis() - effectTimer > 100) {

    static bool toggle = false;

    toggle = !toggle;

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

      if (toggle)

        strip.setPixelColor(i, (i % 2 == 0) ? strip.Color(255, 0, 0) : 0);

      else

        strip.setPixelColor(i, (i % 2 == 0) ? strip.Color(0, 0, 255) : 0);

    }

    strip.show();

    effectTimer = millis();

  }

}


// ==== Meteor Effect (reused for new colors) ====

void meteorEffect(uint32_t color, int meteorSize, int decay) {

  if (millis() - effectTimer > 30) {


    // Fade all LEDs

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

      uint32_t c = strip.getPixelColor(i);

      int r = (uint8_t)(c >> 16);

      int g = (uint8_t)(c >> 8);

      int b = (uint8_t)c;

      r -= r * decay / 256;

      g -= g * decay / 256;

      b -= b * decay / 256;

      strip.setPixelColor(i, r, g, b);

    }


    // Draw meteor

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

      int idx = (effectStep - j) % NUM_LEDS;

      if (idx >= 0) strip.setPixelColor(idx, color);

    }


    strip.show();

    effectStep++;

    effectTimer = millis();

  }

}


// ==== Wave Effect (reused for new wave colors) ====

void waveEffectColor(uint32_t colorMask) {

  if (millis() - effectTimer > 20) {

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

      int level = (sin((i + effectStep) * 0.2) + 1) * 127;

      strip.setPixelColor(i, 

        ((colorMask >> 16) & 255) * level / 255,

        ((colorMask >> 8) & 255) * level / 255,

        (colorMask & 255) * level / 255

      );

    }

    strip.show();

    effectStep++;

    effectTimer = millis();

  }

}


// ==== Effect Selector ====

void runEffect(int e) {

  switch (e) {


    // === ORIGINAL EFFECTS ===

    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: effectRainbow(); break;

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

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

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

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

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

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

    case 10: effectPulse(strip.Color(255, 255, 255), 5); break;

    case 11: effectRainbow(); break;

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

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

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

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

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

    case 17: effectTheaterChase(strip.Color(255, 255, 255), 40); break;

    case 18: effectRainbow(); break;

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

    case 20: fireEffect(); break;

    case 21: policeEffect(); break;

    case 22: meteorEffect(strip.Color(255, 255, 255), 10, 64); break;

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

    case 24: effectRainbow(); break;


    // === NEW EFFECTS ===

    case 25: meteorEffect(strip.Color(0, 0, 255), 10, 64); break;      // Blue Meteor

    case 26: meteorEffect(strip.Color(255, 0, 255), 10, 64); break;    // Purple Meteor

    case 27: meteorEffect(strip.Color(255, 0, 0), 10, 64); break;      // Red Meteor

    case 28: meteorEffect(strip.Color(0, 255, 0), 10, 64); break;      // Green Meteor


    case 29: waveEffectColor(strip.Color(255, 0, 0)); break;           // Red Wave

    case 30: waveEffectColor(strip.Color(0, 255, 0)); break;           // Green Wave

    case 31: waveEffectColor(strip.Color(128, 0, 255)); break;         // Purple Wave


    case 32: effectColorWipe(strip.Color(128, 0, 255), 40); break;      // Purple Wipe

    case 33: effectTheaterChase(strip.Color(128, 0, 255), 60); break;   // Purple Theater

    case 34: showColor(strip.Color(128, 0, 255)); break;                // Purple Color

  }

}


// ==== Setup ====

void setup() {

  pinMode(BTN_NEXT, INPUT_PULLUP);

  pinMode(BTN_PREV, INPUT_PULLUP);

  strip.begin();

  strip.show();


  Serial.begin(9600);


  currentEffect = EEPROM.read(0);

  if (currentEffect < 0 || currentEffect >= totalEffects) currentEffect = 0;


  strip.setBrightness(brightness);

  resetEffect();

}


// ==== Loop ====

void loop() {

  bool nextState = digitalRead(BTN_NEXT);

  bool prevState = digitalRead(BTN_PREV);


  // Instant brightness

  brightness = map(analogRead(POT_PIN), 0, 1023, 0, 255);

  strip.setBrightness(brightness);


  if (nextState == LOW && lastNext == HIGH) {

    currentEffect++;

    if (currentEffect >= totalEffects) currentEffect = 0;

    effectChanged = true;

  }


  if (prevState == LOW && lastPrev == HIGH) {

    currentEffect--;

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

    effectChanged = true;

  }


  lastNext = nextState;

  lastPrev = prevState;


  if (effectChanged) {

    EEPROM.update(0, currentEffect);

    resetEffect();

    effectChanged = false;

  }


  runEffect(currentEffect);

}


Post a Comment

Previous Post Next Post

Contact Form