AniimatronicWingsWithLights_Works

#include

#ifdef __AVR__

 #include // Required for 16 MHz Adafruit Trinket

#endif

                                                                

#include

                                                                

#define PIXEL_PIN    10

#define PIXEL2_PIN   2

                                                                

#define BUTTON_PIN   7

#define PIXEL_COUNT  40

                                                                

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

Adafruit_NeoPixel strip2(PIXEL_COUNT, PIXEL2_PIN, NEO_GRB + NEO_KHZ800);

                                                                

bool modeChanged = false;

int mode = 0; // 0 for rainbow, 1-7 for solid colors, 8 for off

unsigned long lastDebounceTime = 0;

unsigned long debounceDelay = 50;

                                                                

Servo servoR;

Servo servoL;

                                                                

int servoRPin = 6;

int servoLPin = 20;

                                                                

void setup() {

  Serial.begin(9600);

  strip.begin();

  strip.show();

  strip2.begin();

  strip2.show();

  pinMode(BUTTON_PIN, INPUT_PULLUP);

  servoR.attach(servoRPin, 500, 2500);

  servoL.attach(servoLPin, 500, 2500);

}

                                                                

void loop() {

  int reading = digitalRead(BUTTON_PIN);

                                                                

  // Check button state and debounce

  if (reading == LOW) {

        if ((millis() - lastDebounceTime) > debounceDelay) {

          if (!modeChanged) {

                mode++;

                modeChanged = true;

                if (mode > 8) { // Update the maximum mode number

                  mode = 0;

                }

                Serial.print("Mode changed to: ");

                Serial.println(mode);

          }

          lastDebounceTime = millis();

        }

  } else {

                modeChanged = false; // Reset mode changed flag when button is not pressed

  }

                                                                

  if (mode == 0) {

        rainbowAnimation();

  } else if (mode >= 1 && mode <= 7) {

        solidColor(mode - 1);

  } else if (mode == 8) {

        turnOffLights();

  }

                                                                

  if (mode != 8) { // Flap wings only if not in "off" mode

        flapWings(getFlapSpeed(mode));

  }

}

                                                                

void flapWings(int speed) {

  static unsigned long lastUpdate = 0;

  static bool flapping = true;

  static int pos = 0;

                                                                

  if (millis() - lastUpdate > speed) {

        if (flapping) {

      servoR.write(pos);

          servoL.write(180 - pos);

          pos++;

          if (pos >= 90) {

                flapping = false;

          }

        } else {

          servoR.write(pos);

          servoL.write(180 - pos);

          pos--;

          if (pos <= 0) {

                        flapping = true;

          }

        }

        lastUpdate = millis();

  }

}

                                                                

int getFlapSpeed(int mode) {

  switch (mode) {

                case 0: return 30;

                case 1: return 60;

                case 2: return 50;

                case 3: return 40;

                case 4: return 30;

                case 5: return 20;

                case 6: return 15;

                case 7: return 10;

                default: return 30;

  }

}

                                                                

void rainbowAnimation() {

  static unsigned long lastUpdate = 0;

  static uint16_t j = 0;

                                                                

  if (millis() - lastUpdate > 20) {

                for (int i = 0; i < strip.numPixels(); i++) {

                  strip.setPixelColor(i, Wheel((i + j) & 255));

                  strip2.setPixelColor(i, Wheel((i + j) & 255));

                }

                strip.show();

                strip2.show();

                j = (j + 1) % 256;

                lastUpdate = millis();

  }

}

                                                                

void solidColor(int color) {

  uint32_t col;

  switch(color) {

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

                case 1: col = strip.Color(255, 165, 0); break;

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

                case 3: col = strip.Color(0, 255, 0); break;

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

                case 5: col = strip.Color(75, 0, 130); break;

                case 6: col = strip.Color(238, 130, 238); break;

                default: col = strip.Color(0, 0, 0); break;

  }

  for(int i = 0; i < strip.numPixels(); i++) {

                strip.setPixelColor(i, col);

                strip2.setPixelColor(i, col);

  }

  strip.show();

  strip2.show();

}

                                                                

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);

}

                                                                

void turnOffLights() {

  for(int i = 0; i < strip.numPixels(); i++) {

                strip.setPixelColor(i, strip.Color(0, 0, 0));

                strip2.setPixelColor(i, strip.Color(0, 0, 0));

  }

  strip.show();

  strip2.show();

}