Advertisement
Guest User

Untitled

a guest
Sep 10th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.90 KB | None | 0 0
  1. /*
  2. flightlights - fixed wing
  3.  
  4. This version of flightlights includes configurations for fixed-wing aircraft. red/green FAA, landing lights, and
  5. machine-guns are included. Feel free to edit this source and re-burn it to your board.
  6.  
  7. -floz
  8. */
  9.  
  10. #include <Adafruit_NeoPixel.h> // include the NeoPixel library. super handy, that.
  11.  
  12. #ifdef __AVR__ //some magic sauce for AVR microcontrollers?
  13. #include <avr/power.h> //some magic sauce for AVR microcontrollers?
  14. #endif //some magic sauce for AVR microcontrollers?
  15.  
  16. //data-pin for the neopixel LEDs (output)
  17. #define LED_PIN 6
  18.  
  19. //channel on your hobby receiver connects to this digital pin (input)
  20. #define PWM_SOURCE 5
  21.  
  22. //how many LEDs in the chain?
  23. #define NUM_PIXELS 2
  24.  
  25. #define VERSION 1.2
  26. //how long between strobe flashes/battery checks
  27. const long interval = 5050;
  28.  
  29. // how long until battery warning?
  30. const long battery_timeout = 420000;
  31.  
  32. //not really .45, 45ms maximum random on for machinegun fire effect
  33. const long gun_caliber = 45;
  34.  
  35. //timer var
  36. unsigned long previousMillis = 0;
  37.  
  38. //serial console input
  39. String inputString = ""; // a String to hold incoming data
  40.  
  41. //flag (is input string done?)
  42. boolean stringComplete = false; // whether the string is complete
  43.  
  44. // el togglodyte supremo!
  45. // vars used when performing strobe
  46. int MaxLightState = 6;// one over the total number of choices you have.
  47. int LightState = 0; //pick one start with (0 = navlights, 1 = landing lights, 4 = machinegun)
  48.  
  49. int LastLightState = 0;
  50.  
  51. //magic sauce for neopixel/ws2812b LEDs
  52. //check strandtest or other examples for further info
  53. //you can instantiate multiples, define a new pin for each.
  54. Adafruit_NeoPixel leds = Adafruit_NeoPixel(NUM_PIXELS, LED_PIN, NEO_GRB + NEO_KHZ400);
  55.  
  56.  
  57. void setup()
  58. {
  59. //this should be the first void ____ () function in this program
  60. randomSeed(analogRead(0)); //random seed for machine-gun flash effect (works on rf/emf noise, funnily enough)
  61. leds.begin(); // init the LED strand instance
  62. leds.setPixelColor(0,255,0,0);
  63. leds.setPixelColor(1,255,0,0);
  64. leds.show(); // Initialize all pixels
  65. delay(250);
  66. leds.setPixelColor(0,0,255,0);
  67. leds.setPixelColor(1,0,255,0);
  68. leds.show(); // Initialize all pixels
  69. delay(250);
  70. leds.setPixelColor(0,0,0,255);
  71. leds.setPixelColor(1,0,0,255);
  72. leds.show(); // Initialize all pixels
  73. delay(250);
  74. leds.setPixelColor(0,0,0,0);
  75. leds.setPixelColor(1,0,0,0);
  76. leds.show(); // Initialize all pixels
  77. // chose to add color by color init to ensure LED is functional on boot
  78. Serial.begin(9600); // start serial output
  79. inputString.reserve(200);
  80. Serial.print(" flightlights fixed wing ver: ");
  81. Serial.print(VERSION);
  82. Serial.println(" boot complete!");
  83. }
  84.  
  85. void(* resetFunc) (void) = 0;
  86. //define each of your effect functions here:
  87.  
  88. void nav_lights(){
  89. //red/green FAA style
  90. leds.setPixelColor(0, 0,255,0);
  91. leds.setPixelColor(1,255,0,0);
  92. leds.show();
  93.  
  94. }
  95. void landing_lights(){
  96. //bright warm white (r,g,b)
  97. leds.setPixelColor(0, 255,255,196);
  98. leds.setPixelColor(1, 255,255,196);
  99. leds.show();
  100. }
  101. void battery_timer(){
  102. //after x minutes (set above) give the pilot a warning flash
  103. //and then go to critical flasher after two more minutes
  104.  
  105. //two minutes over, go red
  106. if (millis() >= battery_timeout + 120000){
  107. LightState = 5;
  108. return;
  109. }
  110.  
  111. leds.setPixelColor(0, 255,255,0);
  112. leds.setPixelColor(1, 0,0,0);
  113. leds.show();
  114. delay(150);
  115. leds.setPixelColor(1, 255,255,0);
  116. leds.setPixelColor(0, 0,0,0);
  117. leds.show();
  118. delay(150);
  119.  
  120. }
  121. void critical_flash(){
  122. leds.setPixelColor(0, 255,0,0);
  123. leds.setPixelColor(1, 0,0,0);
  124. leds.show();
  125. delay(150);
  126. leds.setPixelColor(1, 255,0,0);
  127. leds.setPixelColor(0, 0,0,0);
  128. leds.show();
  129. delay(150);
  130.  
  131. }
  132. void strobe(){
  133. // added strobe thingie - provides a quick flash and returns to nav red/green cleanly
  134. leds.setPixelColor(0, 255,255,255);
  135. leds.setPixelColor(1, 255,255,255);
  136. leds.show();
  137. delay(60);
  138. LightState = LastLightState;
  139. return;
  140. }
  141. void strobe_loop(){
  142. // rapid wingtip flashes of white/off (find my crashed plane mode)
  143. leds.setPixelColor(0, 255,255,255);
  144. leds.setPixelColor(1, 255,255,255);
  145. leds.show();
  146. delay(100);
  147. leds.setPixelColor(0, 0,0,0);
  148. leds.setPixelColor(1, 0,0,0);
  149. leds.show();
  150. delay(100);
  151. }
  152. void machine_gun_loop(){
  153. /* rapid fire-like flashes at random intervals
  154. *
  155. * gives a nice random fire effect, provided as a well-documented example
  156. * of how to write your own functions.
  157. *
  158. * if you had other lights and machineguns, you would need to specify what
  159. * pixel/LEDs were where by changing the ID
  160. * for example: leds.setPixelColor(ID,red,green,blue);
  161. */
  162. int randNumber = random(gun_caliber); //generate a random number 0 to 45
  163. leds.setPixelColor(0, 255,128,0); //set a nice orangeish fire color for first pixel
  164. leds.setPixelColor(1, 0,0,0); //turn second pixel off
  165. leds.show(); // make it so!
  166. delay(randNumber); //wait random amount of milliseconds, see random 0 to 60 above
  167. randNumber = random(gun_caliber+10); //get a new random number
  168. leds.setPixelColor(0, 0,0,0); //turn first pixel off
  169. leds.setPixelColor(1, 255,128,0); //set a nice orangeish fire color for second pixel
  170.  
  171. leds.show(); // make it so!
  172. delay(randNumber); //wait random amount of milliseconds again
  173. }
  174. void lookupLightState(){
  175. if (LightState == 0){
  176. Serial.println("nav lights");
  177. }
  178. if (LightState == 1){
  179. Serial.println("landing lights");
  180. }
  181. if (LightState == 2){
  182. Serial.println("strobe lights");
  183. }
  184. if (LightState == 3){
  185. Serial.println("battery alert");
  186. }
  187. if (LightState == 4){
  188. Serial.println("pewpewpew");
  189. }
  190. if (LightState == 5){
  191. Serial.println("critical flash");
  192. }
  193. }
  194. void loop() {
  195.  
  196. //serial console - used for debugging/testing porpoises
  197. // print the string when a newline arrives:
  198. if (stringComplete) {
  199. Serial.print("ACK ");
  200. Serial.print(inputString);
  201.  
  202.  
  203. //parser starts here
  204.  
  205. if (inputString == "test\n"){ //give some diagnostic info
  206. Serial.print("flightLights - Program version: ");
  207. Serial.println(VERSION);
  208. Serial.print("program run time: ");
  209. Serial.print(millis());
  210. Serial.println(" ms");
  211. Serial.print("Current Mode: ");
  212. lookupLightState();
  213. int pwmin = pulseIn(PWM_SOURCE, HIGH, 20000); //read the PWM line (default pin 5)
  214. Serial.print("PWM input: ");
  215. Serial.println(pwmin);
  216.  
  217. }else if(inputString == "change\n"){ //iterate through states
  218. LightState++;
  219. if (LightState >= MaxLightState) {
  220. LightState = 0;
  221. }
  222. Serial.print("New Mode: ");
  223. lookupLightState();
  224.  
  225. }else if(inputString == "reboot\n"){
  226. Serial.println("rebooting");
  227. delay(1000);
  228. resetFunc();
  229. }else{
  230. Serial.println("unknown command, valid commands are:"); //return "huh?" catch any non-defined commands this way
  231. Serial.println("test - show diagnostic data");
  232. Serial.println("change - change mode");
  233. Serial.println("reboot - reboot light controller");
  234. }
  235. // clear the string:
  236. inputString = "";
  237. stringComplete = false;
  238. }
  239. //end of parser
  240.  
  241.  
  242. //debug output starts here
  243. //Serial.println(millis()); //show the counter for rapid pass/fail check that program is running
  244. //uncomment for debugging otherwise the console is spammmmmm
  245. /*
  246. Serial.print("PWM: ");
  247. Serial.print(pwmin);
  248. if (LightState == 0){
  249. Serial.print(" - nav lights - ");
  250. }
  251.  
  252. if (LightState == 1){
  253. Serial.print(" - landing lights - ");
  254. }
  255.  
  256. if (LightState == 2){
  257. Serial.print(" - strobe lights - ");
  258. }
  259. Serial.print(" time: ");
  260. Serial.println(millis());
  261. delay(100);
  262. */
  263. //uncomment for debugging otherwise the console is spammmmmm
  264. //debug output stops here, actual loop code begins
  265.  
  266.  
  267.  
  268. // how were the lights commanded within this program?
  269. // LightState is an iterable int that represents individual functions
  270. // that the lights can perform. It is used to snap quickly back to FAA red/green
  271. // after a fast strobe, and just kind of grew from there.
  272.  
  273. if (LightState == 0){
  274. nav_lights();
  275. }
  276. if (LightState == 1){
  277. landing_lights();
  278. }
  279. if (LightState == 2){
  280. strobe_loop();
  281. }
  282. if (LightState ==3){
  283. battery_timer();
  284. }
  285. if (LightState ==4){ //add extra functions like this.
  286. machine_gun_loop(); //they can point to any function you want
  287. } //muzzle-flash here
  288. if (LightState ==5){
  289. critical_flash(); //alert that something is amiss.
  290. }
  291.  
  292.  
  293.  
  294.  
  295. //running timer to check for strobe interval
  296. //blatantly borrowed from "blink without delay" example for Arduino
  297. unsigned long currentMillis = millis();
  298. //someone else's work, from blinkwithoutdelay example.
  299. if (currentMillis - previousMillis >= interval) { //has the delay between strobes happened?
  300. Serial.print("program run time: ");
  301. Serial.println(millis());
  302. if (millis() >= battery_timeout){ //battery timer check
  303. LightState =3;
  304. return; //ymmv, comment out?
  305. }
  306. previousMillis = currentMillis;
  307. if (LightState != 0){ //strobe check, if we're not using nav lights, don't bother
  308. return;
  309. }
  310. LastLightState = LightState; //var to return to nav lights after strobe
  311. strobe(); //give us a quick flash
  312. }
  313.  
  314.  
  315.  
  316.  
  317. // below this is the PWM input from the hobby receiver
  318. // any modes you want reachable via your radio must be mapped here.
  319. // simply add a new case X: / break; statement as shown below
  320. // and create the function at the top of this program
  321.  
  322. int pwmin = pulseIn(PWM_SOURCE, HIGH, 20000); //read the PWM line (default pin 5)
  323.  
  324. int selection = map(pwmin, 1000,1900,0,4); //map the value to however many choices you want to have
  325. //from your radio. this should match the code below
  326.  
  327. switch (selection) { //what mode are we in according to pwm?
  328. case 0:
  329. LightState = 0; //nav lights
  330. break;
  331. case 1:
  332. LightState = 1; //landing lights
  333. break;
  334. case 2:
  335. LightState = 2; // strobes
  336. break;
  337. case 3:
  338. LightState = 3; //battery flasher
  339. break;
  340. case 4: //add extra functions by adding case 5, case 6, etc. be sure to change the mapping above.
  341. LightState = 4; //machine_gun_loop
  342. break;
  343. }
  344.  
  345.  
  346. }
  347.  
  348. /*
  349. SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  350. routine is run between each time loop() runs, so using delay inside loop can
  351. delay response. Multiple bytes of data may be available.
  352. */
  353. void serialEvent() {
  354. while (Serial.available()) {
  355. // get the new byte:
  356. char inChar = (char)Serial.read();
  357. // add it to the inputString:
  358. inputString += inChar;
  359. // if the incoming character is a newline, set a flag so the main loop can
  360. // do something about it:
  361. if (inChar == '\n') {
  362. stringComplete = true;
  363. }
  364. }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement