Advertisement
Guest User

Untitled

a guest
Sep 9th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. /*
  2. arduino aircraft lighting update 1
  3. contains various bits that were ripped from strandtest, forum.arduino.cc, arduino IDE
  4. examples, and others, then heavily cut down and commented to the best of my ability.
  5.  
  6. modify this to suit your needs, it's working fine for me now.
  7.  
  8. -floz
  9. */
  10.  
  11. #include <Adafruit_NeoPixel.h>
  12. #ifdef __AVR__
  13. #include <avr/power.h>
  14. #endif
  15.  
  16. //data-pin for the neopixels (output)
  17. #define PIN 6
  18.  
  19. //channel on your hobby receiver connects to this digital pin (input)
  20. #define PWM_SOURCE 5
  21.  
  22. //how long between strobe flashes/battery checks
  23. const long interval = 5050;
  24. //timer var
  25. unsigned long previousMillis = 0;
  26.  
  27. //el togglodyte supremo!
  28. int LightState = 1;
  29. int LastLightState = 0;
  30.  
  31. //magic sauce for neopixel/ws2812b
  32. //check strandtest or other examples for further info
  33. //you can instantiate multiples for all kinds of effects
  34. Adafruit_NeoPixel leds = Adafruit_NeoPixel(2, PIN, NEO_GRB + NEO_KHZ400);
  35.  
  36.  
  37. void setup()
  38. {
  39.  
  40. leds.begin(); // init the instance
  41. leds.show(); // Initialize all pixels to 'off'
  42. Serial.begin(9600); // start serial output
  43. }
  44. void nav_lights(){
  45. //red/green FAA style
  46. leds.setPixelColor(0, 0,255,0);
  47. leds.setPixelColor(1,255,0,0);
  48. leds.show();
  49.  
  50. }
  51. void landing_lights(){
  52. //bright warm white
  53. leds.setPixelColor(0, 255,255,196);
  54. leds.setPixelColor(1, 255,255,196);
  55. leds.show();
  56. }
  57. void battery_timer(){
  58. //after x minutes (default 10) give the pilot a friendly warning flash
  59. //and refuse to exit this mode until reset/powered down
  60. leds.setPixelColor(0, 255,255,0);
  61. leds.setPixelColor(1, 0,0,0);
  62. leds.show();
  63. delay(150);
  64. leds.setPixelColor(1, 255,255,0);
  65. leds.setPixelColor(0, 0,0,0);
  66. leds.show();
  67. delay(150);
  68. LightState = 3;
  69. }
  70.  
  71. void strobe(){
  72. // added strobe thingie
  73. leds.setPixelColor(0, 255,255,255);
  74. leds.setPixelColor(1, 255,255,255);
  75. leds.show();
  76. delay(60);
  77. LightState = LastLightState;
  78. return;
  79. }
  80. void strobe_loop(){
  81. // rapid wingtip flashes of white/off
  82. leds.setPixelColor(0, 255,255,255);
  83. leds.setPixelColor(1, 255,255,255);
  84. leds.show();
  85. delay(100);
  86. leds.setPixelColor(0, 0,0,0);
  87. leds.setPixelColor(1, 0,0,0);
  88. leds.show();
  89. delay(100);
  90. }
  91. void loop() {
  92. //cleaned it up some, using switch/case now
  93.  
  94. if (LightState == 0){
  95. nav_lights();
  96. }
  97. if (LightState == 1){
  98. landing_lights();
  99. }
  100. if (LightState == 2){
  101. strobe_loop();
  102. }
  103. if (LightState ==3){
  104. battery_timer();
  105. }
  106. /*
  107. if (LightState ==4){ //add extra functions like this.
  108. strobe_loop();
  109. }
  110. */
  111.  
  112. unsigned long currentMillis = millis();
  113. //someone else's work, from blinkwithoutdelay example.
  114. if (currentMillis - previousMillis >= interval) {
  115. // this huge number (600000) is ten minutes in milliseconds
  116. // ( min * 60 ) * 1000 = ms
  117. // i adjusted to 6 minutes while learning to fly
  118. if (millis() >= 480000){
  119. battery_timer();
  120. return; //ymmv, comment out?
  121. }
  122. previousMillis = currentMillis;
  123.  
  124. if (LightState != 0){ //strobe check
  125. //if we're not using nav lights, don't bother
  126. return;
  127. }
  128. LastLightState = LightState; //var to return to nav lights after strobe
  129. strobe(); //give us a quick flash
  130. }
  131.  
  132. int pwmin = pulseIn(PWM_SOURCE, HIGH, 20000); //read the PWM line (default pin 5)
  133.  
  134.  
  135. //map the value to however many choices you want to have
  136. int selection = map(pwmin, 1000,1900,0,2);
  137. Serial.print("PWM: ");
  138. Serial.print(pwmin);
  139.  
  140. if (LightState == 0){
  141. Serial.print(" nav lights ");
  142. }
  143.  
  144. if (LightState == 1){
  145. Serial.print(" landing lights ");
  146. }
  147.  
  148. if (LightState == 2){
  149. Serial.print(" strobe lights ");
  150. }
  151. Serial.print(" time: ");
  152. Serial.println(millis());
  153.  
  154. switch (selection) { //what mode are we in according to pwm?
  155. case 0:
  156. LightState = 0; //nav lights
  157. break;
  158. case 1:
  159. LightState = 1; //landing lights
  160. break;
  161. case 2:
  162. LightState = 2; // strobes
  163. break;
  164. /*
  165. case 3: //add extra functions this way
  166. LightState = 3; //battery warning flash test
  167. Serial.println("battery strobe test");
  168. break;
  169. case 4:
  170. LightState = 4;
  171. Serial.println("state #4");
  172. break;
  173. */
  174. }
  175. //Serial.print("time: ");
  176. //Serial.println(millis());
  177. //delay(100); //purely for debugging otherwise the console is spammmmmm
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement