Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #include <Servo.h>
- //button 1 setup
- const int buttonPin = 10;
- int ledState = 0; // the current state of the output pin
- int buttonState; // the current reading from the input pin
- int lastButtonState = LOW;
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 50; // the debounce time; increase if the output flickers
- //button 2 setup
- const int buttonPin2 = 11;
- int ServoState = 0; // the current state of the output pin
- int buttonState2; // the current reading from the input pin
- int lastButtonState2 = LOW;
- long lastDebounceTime2 = 0; // the last time the output pin was toggled
- long debounceDelay2 = 50; // the debounce time; increase if the output flickers
- //button 3 setup
- const int buttonPin3 = 12;
- //LED SETUP
- #define LED_PIN 9
- #define NUM_LEDS 7
- #define BRIGHTNESS 255
- #define LED_TYPE WS2811
- #define COLOR_ORDER GRB
- CRGB leds[NUM_LEDS];
- #define UPDATES_PER_SECOND 100
- CRGBPalette16 currentPalette;
- TBlendType currentBlending;
- extern CRGBPalette16 myRedWhiteBluePalette;
- extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
- //Servo Setup
- Servo myServo;
- Servo servo1;
- Servo servo2;
- int posA = 165; // variable to store the servo position
- int posB = 0;
- int flappingoverride = LOW;
- int openingstate = LOW;
- int flappingstate = LOW;
- int flapcounter = 0;
- int wagstate = LOW;
- int wagdir = HIGH;
- //Timer Setup
- long lastservotime = 0;
- long servodelay = 100;
- //long previousMillis = 0; // will store last time LED was updated
- //long interval = 1000;
- void setup()
- {
- delay( 3000 ); // power-up safety delay
- //button setup
- pinMode(buttonPin, INPUT);
- pinMode(buttonPin2, INPUT);
- pinMode(buttonPin3, INPUT);
- //LED SETUP
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
- FastLED.setBrightness( BRIGHTNESS );
- currentPalette = RainbowColors_p;
- currentBlending = BLEND;
- //Servo Setup
- servo1.attach(5);
- servo2.attach(6);
- delay(10);
- servo1.write(posA);
- delay(10);
- servo2.write(posB);
- }
- void loop()
- {
- ///////////////////////////////////button 1 ops////////////////////////////////////////////////
- /////////////////////////////////LIGHTS AND SOUND//////////////////////////////////////////////
- int reading = digitalRead(buttonPin);
- if (reading != lastButtonState)
- {
- // reset the debouncing timer
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay)
- {
- // whatever the reading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != buttonState)
- {
- buttonState = reading;
- // only toggle the LED if the new button state is HIGH
- if (buttonState == HIGH)
- {
- ledState = ledState+1;
- if (ledState>3){ledState=0;}
- }
- }
- }
- lastButtonState = reading;
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////button 2 ops////////////////////////////////////////////////////
- //////////////////////////////////////SERVOS///////////////////////////////////////////////////////
- int reading2 = digitalRead(buttonPin2);
- if (reading2 != lastButtonState2)
- {
- // reset the debouncing timer
- lastDebounceTime2 = millis();
- }
- if ((millis() - lastDebounceTime2) > debounceDelay2)
- {
- if (reading2 != buttonState2)
- {
- buttonState2 = reading2;
- if (buttonState2 == HIGH)
- {
- flappingstate =! flappingstate;
- }
- }
- }
- lastButtonState2 = reading2;
- ///////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////Button 3 OPS////////////////////////////////////////////////
- /////////////////////////////////////FLAP OVERRIDE/////////////////////////////////////////////////
- int reading3 = digitalRead(buttonPin3);
- if (reading3 = HIGH)
- {
- //flappingoverride = HIGH;
- }
- ////////////////////////////////////SERVO OPS//////////////////////////////////////////////////////
- unsigned long currenttime = millis();
- if(currenttime - lastservotime > servodelay)
- {
- lastservotime = currenttime;
- if (flappingoverride == LOW)
- {
- if (flappingstate == HIGH)
- {
- if (flapcounter < 4)
- {
- if (openingstate == HIGH)
- {
- posA=posA-1;
- posB=posB+1;
- servo1.write(posA);
- servo2.write(posB);
- if (posA<81)
- {
- openingstate=LOW;
- }
- //Open the wings
- //set opening state to close
- }
- if (openingstate == LOW)
- {
- posA=posA+1;
- posB=posB-1;
- servo1.write(posA);
- servo2.write(posB);
- if (posA>164)
- {
- openingstate=HIGH;
- flapcounter=flapcounter+1;
- }
- }
- }
- if (flapcounter>3)
- {
- if (wagstate==LOW)
- {
- posA=posA-1;
- posB=posB+1;
- servo1.write(posA);
- servo2.write(posB);
- if (posA<81)
- {
- wagstate=HIGH;
- }
- }
- if (wagstate==HIGH)
- {
- if (wagdir == HIGH)
- {
- posA=posA-1;
- posB=posB-1;
- servo1.write(posA);
- servo2.write(posB);
- if (posA<1)
- {
- wagdir=LOW;
- }
- }
- if (wagdir == LOW)
- {
- posA=posA+1;
- posB=posB+1;
- servo1.write(posA);
- servo2.write(posB);
- if (posA>164)
- {
- wagdir = HIGH;
- // flapcounter=0;
- // wagstate=LOW;
- }
- }
- }
- }
- }
- }
- }
- ///////////////////////////////////LED AND SOUND OPS////////////////////////////////////////////////
- /////////4 MODES - LIGHTS RANDOM, LIGHTS RANDOM MUSIC, LIGHTS PINK, LIGHTS PINK MUSIC///////////////
- int brightness = 255;
- if (ledState<2)
- {
- if (ledState==1) ///////////////////FLASH TO MUSIC/////////////////
- { ///////////////////RANDOM PALETTE/////////////////
- int reading = analogRead(0);
- int reading2=(reading-30);
- if (reading2<1){reading2=0;}
- brightness = reading2*100 ;
- ChangePalettePeriodically();
- }
- ////////////////////////////////////////////////////////////////////
- else {brightness = 255; //////////////SOLID ON////////////////////
- ChangePalettePeriodically();} ////////RANDOM PALETTE//////////////
- }
- /////////////////////////////////////////////////////////////////////
- if (ledState>1)
- {
- if (ledState==3) ///////////////////FLASH TO MUSIC/////////////////
- { ///////////////////PINK PALETTE////////////////////
- int reading = analogRead(0);
- int reading2=(reading-30);
- if (reading2<1){reading2=0;}
- brightness = reading2*100 ;
- pinkPalette();
- }
- ////////////////////////////////////////////////////////////////////
- else {brightness = 255; //////////////SOLID ON////////////////////
- pinkPalette(); ///////////////PINK PALETTE//////////////////
- }
- }
- //////////////////////////////////////////////////////////////////////
- /////////////////////////SEND DATA TO LED FUNCTIONS///////////////////
- static uint8_t startIndex = 0;
- startIndex = startIndex + 1;
- FillLEDsFromPaletteColors( startIndex, brightness);
- FastLED.show();
- FastLED.delay(1000 / UPDATES_PER_SECOND);
- /////////////////////////////////////////////////////////////////////////
- }//END OF VOID LOOP
- void FillLEDsFromPaletteColors( uint8_t colorIndex, uint8_t brightness)
- {
- for( int i = 0; i < NUM_LEDS; i++)
- {
- leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
- colorIndex += 3;
- }
- }
- void ChangePalettePeriodically()
- {
- uint8_t secondHand = (millis() / 1000) % 60;
- static uint8_t lastSecond = 99;
- if( lastSecond != secondHand)
- {
- lastSecond = secondHand;
- if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = BLEND; }
- if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
- if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = BLEND; }
- if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = BLEND; }
- if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = BLEND; }
- if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
- if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = BLEND; }
- if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = BLEND; }
- if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = BLEND; }
- if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
- if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = BLEND; }
- }
- }
- void SetupTotallyRandomPalette()
- {
- for( int i = 0; i < 16; i++)
- {
- currentPalette[i] = CHSV( random8(), 255, random8());
- }
- }
- void SetupBlackAndWhiteStripedPalette()
- {
- // 'black out' all 16 palette entries...
- fill_solid( currentPalette, 16, CRGB::Black);
- // and set every fourth one to white.
- currentPalette[0] = CRGB::White;
- currentPalette[4] = CRGB::White;
- currentPalette[8] = CRGB::White;
- currentPalette[12] = CRGB::White;
- }
- void pinkPalette()
- {
- fill_solid( currentPalette, 16, CHSV( HUE_PINK, 255, 255)); //CRGB::HotPink
- }
- void SetupPurpleAndGreenPalette()
- {
- CRGB purple = CHSV( HUE_PURPLE, 255, 255);
- CRGB green = CHSV( HUE_GREEN, 255, 255);
- CRGB black = CRGB::Black;
- currentPalette = CRGBPalette16(
- green, green, black, black,
- purple, purple, black, black,
- green, green, black, black,
- purple, purple, black, black );
- }
- const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
- {
- CRGB::Red,
- CRGB::Gray,
- CRGB::Blue,
- CRGB::Black,
- CRGB::Red,
- CRGB::Gray,
- CRGB::Blue,
- CRGB::Black,
- CRGB::Red,
- CRGB::Red,
- CRGB::Gray,
- CRGB::Gray,
- CRGB::Blue,
- CRGB::Blue,
- CRGB::Black,
- CRGB::Black
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement