Advertisement
Guest User

wings

a guest
Oct 23rd, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.85 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <Servo.h>
  3.  
  4.  
  5. //button 1 setup
  6. const int buttonPin = 10;
  7. int ledState = 0; // the current state of the output pin
  8. int buttonState; // the current reading from the input pin
  9. int lastButtonState = LOW;
  10. long lastDebounceTime = 0; // the last time the output pin was toggled
  11. long debounceDelay = 50; // the debounce time; increase if the output flickers
  12.  
  13. //button 2 setup
  14. const int buttonPin2 = 11;
  15. int ServoState = 0; // the current state of the output pin
  16. int buttonState2; // the current reading from the input pin
  17. int lastButtonState2 = LOW;
  18. long lastDebounceTime2 = 0; // the last time the output pin was toggled
  19. long debounceDelay2 = 50; // the debounce time; increase if the output flickers
  20.  
  21. //button 3 setup
  22. const int buttonPin3 = 12;
  23.  
  24.  
  25.  
  26. //LED SETUP
  27. #define LED_PIN 9
  28. #define NUM_LEDS 7
  29. #define BRIGHTNESS 255
  30. #define LED_TYPE WS2811
  31. #define COLOR_ORDER GRB
  32. CRGB leds[NUM_LEDS];
  33. #define UPDATES_PER_SECOND 100
  34. CRGBPalette16 currentPalette;
  35. TBlendType currentBlending;
  36. extern CRGBPalette16 myRedWhiteBluePalette;
  37. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  38.  
  39.  
  40. //Servo Setup
  41. Servo myServo;
  42. Servo servo1;
  43. Servo servo2;
  44. int posA = 165; // variable to store the servo position
  45. int posB = 0;
  46. int flappingoverride = LOW;
  47. int openingstate = LOW;
  48. int flappingstate = LOW;
  49. int flapcounter = 0;
  50. int wagstate = LOW;
  51. int wagdir = HIGH;
  52. //Timer Setup
  53. long lastservotime = 0;
  54. long servodelay = 100;
  55.  
  56. //long previousMillis = 0; // will store last time LED was updated
  57. //long interval = 1000;
  58.  
  59. void setup()
  60. {
  61. delay( 3000 ); // power-up safety delay
  62.  
  63.  
  64. //button setup
  65. pinMode(buttonPin, INPUT);
  66. pinMode(buttonPin2, INPUT);
  67. pinMode(buttonPin3, INPUT);
  68.  
  69.  
  70.  
  71.  
  72. //LED SETUP
  73. FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  74. FastLED.setBrightness( BRIGHTNESS );
  75. currentPalette = RainbowColors_p;
  76. currentBlending = BLEND;
  77.  
  78.  
  79. //Servo Setup
  80. servo1.attach(5);
  81. servo2.attach(6);
  82. delay(10);
  83. servo1.write(posA);
  84. delay(10);
  85. servo2.write(posB);
  86. }
  87.  
  88.  
  89. void loop()
  90. {
  91.  
  92.  
  93. ///////////////////////////////////button 1 ops////////////////////////////////////////////////
  94. /////////////////////////////////LIGHTS AND SOUND//////////////////////////////////////////////
  95. int reading = digitalRead(buttonPin);
  96. if (reading != lastButtonState)
  97. {
  98. // reset the debouncing timer
  99. lastDebounceTime = millis();
  100. }
  101.  
  102. if ((millis() - lastDebounceTime) > debounceDelay)
  103. {
  104. // whatever the reading is at, it's been there for longer
  105. // than the debounce delay, so take it as the actual current state:
  106.  
  107. // if the button state has changed:
  108. if (reading != buttonState)
  109. {
  110. buttonState = reading;
  111. // only toggle the LED if the new button state is HIGH
  112. if (buttonState == HIGH)
  113. {
  114. ledState = ledState+1;
  115. if (ledState>3){ledState=0;}
  116. }
  117. }
  118. }
  119. lastButtonState = reading;
  120. ///////////////////////////////////////////////////////////////////////////////////////////////////
  121.  
  122.  
  123.  
  124. ///////////////////////////////////button 2 ops////////////////////////////////////////////////////
  125. //////////////////////////////////////SERVOS///////////////////////////////////////////////////////
  126. int reading2 = digitalRead(buttonPin2);
  127. if (reading2 != lastButtonState2)
  128. {
  129. // reset the debouncing timer
  130. lastDebounceTime2 = millis();
  131. }
  132.  
  133. if ((millis() - lastDebounceTime2) > debounceDelay2)
  134. {
  135. if (reading2 != buttonState2)
  136. {
  137. buttonState2 = reading2;
  138. if (buttonState2 == HIGH)
  139. {
  140. flappingstate =! flappingstate;
  141.  
  142. }
  143. }
  144. }
  145. lastButtonState2 = reading2;
  146. ///////////////////////////////////////////////////////////////////////////////////////////////////
  147.  
  148.  
  149. ///////////////////////////////////////Button 3 OPS////////////////////////////////////////////////
  150. /////////////////////////////////////FLAP OVERRIDE/////////////////////////////////////////////////
  151. int reading3 = digitalRead(buttonPin3);
  152. if (reading3 = HIGH)
  153. {
  154. //flappingoverride = HIGH;
  155. }
  156.  
  157.  
  158.  
  159. ////////////////////////////////////SERVO OPS//////////////////////////////////////////////////////
  160. unsigned long currenttime = millis();
  161. if(currenttime - lastservotime > servodelay)
  162. {
  163. lastservotime = currenttime;
  164. if (flappingoverride == LOW)
  165. {
  166. if (flappingstate == HIGH)
  167. {
  168. if (flapcounter < 4)
  169. {
  170. if (openingstate == HIGH)
  171. {
  172. posA=posA-1;
  173. posB=posB+1;
  174. servo1.write(posA);
  175. servo2.write(posB);
  176. if (posA<81)
  177.  
  178. {
  179. openingstate=LOW;
  180. }
  181. //Open the wings
  182. //set opening state to close
  183.  
  184. }
  185. if (openingstate == LOW)
  186. {
  187. posA=posA+1;
  188. posB=posB-1;
  189. servo1.write(posA);
  190. servo2.write(posB);
  191. if (posA>164)
  192. {
  193. openingstate=HIGH;
  194. flapcounter=flapcounter+1;
  195. }
  196.  
  197. }
  198. }
  199. if (flapcounter>3)
  200. {
  201. if (wagstate==LOW)
  202. {
  203. posA=posA-1;
  204. posB=posB+1;
  205. servo1.write(posA);
  206. servo2.write(posB);
  207. if (posA<81)
  208. {
  209. wagstate=HIGH;
  210. }
  211. }
  212. if (wagstate==HIGH)
  213. {
  214. if (wagdir == HIGH)
  215. {
  216. posA=posA-1;
  217. posB=posB-1;
  218. servo1.write(posA);
  219. servo2.write(posB);
  220. if (posA<1)
  221. {
  222. wagdir=LOW;
  223. }
  224. }
  225. if (wagdir == LOW)
  226. {
  227. posA=posA+1;
  228. posB=posB+1;
  229. servo1.write(posA);
  230. servo2.write(posB);
  231. if (posA>164)
  232. {
  233.  
  234. wagdir = HIGH;
  235. // flapcounter=0;
  236. // wagstate=LOW;
  237. }
  238. }
  239. }
  240.  
  241. }
  242. }
  243. }
  244. }
  245. ///////////////////////////////////LED AND SOUND OPS////////////////////////////////////////////////
  246. /////////4 MODES - LIGHTS RANDOM, LIGHTS RANDOM MUSIC, LIGHTS PINK, LIGHTS PINK MUSIC///////////////
  247.  
  248. int brightness = 255;
  249. if (ledState<2)
  250. {
  251. if (ledState==1) ///////////////////FLASH TO MUSIC/////////////////
  252. { ///////////////////RANDOM PALETTE/////////////////
  253. int reading = analogRead(0);
  254. int reading2=(reading-30);
  255. if (reading2<1){reading2=0;}
  256. brightness = reading2*100 ;
  257. ChangePalettePeriodically();
  258. }
  259. ////////////////////////////////////////////////////////////////////
  260.  
  261.  
  262.  
  263. else {brightness = 255; //////////////SOLID ON////////////////////
  264. ChangePalettePeriodically();} ////////RANDOM PALETTE//////////////
  265. }
  266. /////////////////////////////////////////////////////////////////////
  267.  
  268.  
  269.  
  270. if (ledState>1)
  271. {
  272. if (ledState==3) ///////////////////FLASH TO MUSIC/////////////////
  273. { ///////////////////PINK PALETTE////////////////////
  274. int reading = analogRead(0);
  275. int reading2=(reading-30);
  276. if (reading2<1){reading2=0;}
  277. brightness = reading2*100 ;
  278. pinkPalette();
  279. }
  280. ////////////////////////////////////////////////////////////////////
  281.  
  282.  
  283.  
  284. else {brightness = 255; //////////////SOLID ON////////////////////
  285. pinkPalette(); ///////////////PINK PALETTE//////////////////
  286. }
  287. }
  288. //////////////////////////////////////////////////////////////////////
  289.  
  290.  
  291.  
  292. /////////////////////////SEND DATA TO LED FUNCTIONS///////////////////
  293. static uint8_t startIndex = 0;
  294. startIndex = startIndex + 1;
  295. FillLEDsFromPaletteColors( startIndex, brightness);
  296. FastLED.show();
  297. FastLED.delay(1000 / UPDATES_PER_SECOND);
  298. /////////////////////////////////////////////////////////////////////////
  299.  
  300. }//END OF VOID LOOP
  301.  
  302.  
  303.  
  304.  
  305.  
  306. void FillLEDsFromPaletteColors( uint8_t colorIndex, uint8_t brightness)
  307. {
  308. for( int i = 0; i < NUM_LEDS; i++)
  309. {
  310. leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  311. colorIndex += 3;
  312. }
  313.  
  314. }
  315.  
  316.  
  317.  
  318. void ChangePalettePeriodically()
  319. {
  320. uint8_t secondHand = (millis() / 1000) % 60;
  321. static uint8_t lastSecond = 99;
  322.  
  323. if( lastSecond != secondHand)
  324. {
  325. lastSecond = secondHand;
  326. if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = BLEND; }
  327. if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
  328. if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = BLEND; }
  329. if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = BLEND; }
  330. if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = BLEND; }
  331. if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
  332. if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = BLEND; }
  333. if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = BLEND; }
  334. if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = BLEND; }
  335. if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
  336. if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = BLEND; }
  337. }
  338. }
  339.  
  340.  
  341. void SetupTotallyRandomPalette()
  342. {
  343. for( int i = 0; i < 16; i++)
  344. {
  345. currentPalette[i] = CHSV( random8(), 255, random8());
  346. }
  347. }
  348.  
  349.  
  350. void SetupBlackAndWhiteStripedPalette()
  351. {
  352. // 'black out' all 16 palette entries...
  353. fill_solid( currentPalette, 16, CRGB::Black);
  354. // and set every fourth one to white.
  355. currentPalette[0] = CRGB::White;
  356. currentPalette[4] = CRGB::White;
  357. currentPalette[8] = CRGB::White;
  358. currentPalette[12] = CRGB::White;
  359. }
  360.  
  361. void pinkPalette()
  362. {
  363. fill_solid( currentPalette, 16, CHSV( HUE_PINK, 255, 255)); //CRGB::HotPink
  364. }
  365.  
  366.  
  367. void SetupPurpleAndGreenPalette()
  368. {
  369. CRGB purple = CHSV( HUE_PURPLE, 255, 255);
  370. CRGB green = CHSV( HUE_GREEN, 255, 255);
  371. CRGB black = CRGB::Black;
  372.  
  373. currentPalette = CRGBPalette16(
  374. green, green, black, black,
  375. purple, purple, black, black,
  376. green, green, black, black,
  377. purple, purple, black, black );
  378. }
  379.  
  380.  
  381.  
  382. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
  383. {
  384. CRGB::Red,
  385. CRGB::Gray,
  386. CRGB::Blue,
  387. CRGB::Black,
  388.  
  389. CRGB::Red,
  390. CRGB::Gray,
  391. CRGB::Blue,
  392. CRGB::Black,
  393.  
  394. CRGB::Red,
  395. CRGB::Red,
  396. CRGB::Gray,
  397. CRGB::Gray,
  398. CRGB::Blue,
  399. CRGB::Blue,
  400. CRGB::Black,
  401. CRGB::Black
  402. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement