leds.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const byte Blue_Led = 2;
  2. bool Blue_Led_State = 1 ;
  3. Ticker tkBlue_Led ;
  4. const byte Red_Led = 0;
  5. bool Red_Led_State = 0;
  6. Ticker tkRed_Led ;
  7. /*Faire des fonctions simples à appeler depuis autres functions callbacks, les fonctions locales attachent et detachent les tkLeds
  8. fonctions extérieures appellent blueLedState (0 off, 1 on, -1 blink; time_ms)
  9. blueStateLed attache/détache les tickers sur fonction invert
  10. */
  11. void blinkLed(int Led) {
  12. if (Led==0) {Red_Led_State = !Red_Led_State; digitalWrite(Red_Led, !Red_Led_State) ;}
  13. if (Led==1) {Blue_Led_State = !Blue_Led_State; digitalWrite(Blue_Led, !Blue_Led_State);}
  14. }
  15. void redLedState ( int state, int time_ms) { // -1 inverts, 0 off, 1 on
  16. if (state == -1) {
  17. tkRed_Led.attach_ms( time_ms, blinkLed, 0);
  18. }
  19. else {
  20. tkRed_Led.detach();
  21. Red_Led_State = state ;
  22. digitalWrite(Red_Led, !Red_Led_State) ; // inverted, 1 is off and 0 is on
  23. }
  24. //Serial.println("Red Led "+(String)Red_Led_State);
  25. }
  26. void blueLedState ( int state, int time_ms) { // -1 inverts, 0 off, 1 on
  27. if (state == -1) {
  28. tkBlue_Led.attach_ms( time_ms, blinkLed, 1);
  29. }
  30. else {
  31. tkBlue_Led.detach();
  32. Blue_Led_State = state ;
  33. digitalWrite(Blue_Led, !Blue_Led_State) ;
  34. }
  35. // inverted, 1 is off and 0 is on
  36. //Serial.println("Blue Led "+(String)Blue_Led_State);
  37. }
  38. void setupLeds() {
  39. pinMode(Blue_Led, OUTPUT);
  40. digitalWrite(Blue_Led, Blue_Led_State);
  41. pinMode(Red_Led, OUTPUT);
  42. digitalWrite(Red_Led, Red_Led_State);//red led on at boot
  43. delay(2000);
  44. blueLedState(-1, 100);
  45. redLedState(-1, 100);
  46. delay (500); // blink fast during 0.5 seconds
  47. redLedState(0, 100);
  48. blueLedState(0, 100); // then switch all leds off
  49. }