Advertisement
Guest User

blinky

a guest
Apr 29th, 2016
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. // NavLights.ino  
  2. // Arduino based remote controlled navigation lights  
  3. //  
  4. // By Daniel van den Ouden, 2014  
  5. //  
  6.  
  7. // configuration for input  
  8. uint8_t cfg_inputPin;  
  9.  
  10. // 5 output channels  
  11. #define CHANNELS 5  
  12.  
  13. uint8_t cfg_blinks[CHANNELS];            // how often a channel will blink in a sequence, 8 bit, so value is 0 - 255, 0 means no blinking  
  14. uint16_t cfg_onDuration[CHANNELS];        // how long the channel will be on in milliseconds, 16 bit, so value is 0 - 65535  
  15. uint16_t cfg_offDuration[CHANNELS];        // how long the channel will be off between blinks in milliseconds, 16 bit  
  16. uint16_t cfg_pauseDuration[CHANNELS];    // how long the channel will be off between the last and first blink of a sequence, 16 bit  
  17. uint8_t cfg_pins[CHANNELS];            // which pin to use for which channel  
  18.  
  19. bool   state_switch[CHANNELS];        // state of the channel's "switch" (true = on, false = off)  
  20. bool   state_onOff[CHANNELS];            // the output state of the channel (true = high, false = low)  
  21. uint8_t state_currentBlink[CHANNELS];    // how often this channel has blinked in the current sequence  
  22. uint16_t state_nextChange[CHANNELS];    // number of milliseconds until next change  
  23.  
  24. unsigned long loop_lastMillis;            // time of last update  
  25. bool     loop_lastSwitchPosition;    // last measured switch position  
  26. uint8_t    loop_switchCount;            // number of switches in current sequence  
  27. unsigned long loop_lastSwitchTime;        // time at which the switch was flipped last  
  28.  
  29. void setChannelPin(uint8_t p_channel, uint8_t p_pin)  
  30. {  
  31.     digitalWrite(p_pin, LOW);  
  32.     pinMode(p_pin, OUTPUT);  
  33.     cfg_pins[p_channel] = p_pin;  
  34. }  
  35.  
  36.  
  37. void configureChannelBlink(uint8_t p_channel, uint8_t p_blinks, uint16_t p_onDuration, uint16_t p_offDuration, uint16_t p_pauseDuration)  
  38. {  
  39.     cfg_blinks[p_channel]    = p_blinks;  
  40.     cfg_onDuration[p_channel]  = p_onDuration;  
  41.     cfg_offDuration[p_channel]  = p_offDuration;  
  42.     cfg_pauseDuration[p_channel] = p_pauseDuration;  
  43.     setChannelSwitch(p_channel, true);  
  44. }  
  45.  
  46.  
  47. void configureChannelConstant(uint8_t p_channel)  
  48. {  
  49.     cfg_blinks[p_channel] = 0;  
  50.     setChannelSwitch(p_channel, true);  
  51. }  
  52.  
  53.  
  54. void setup()  
  55. {  
  56.     cfg_inputPin = 9;  
  57.      
  58.     setChannelPin(0, 4);  
  59.     setChannelPin(1, 5);  
  60.     setChannelPin(2, 6);  
  61.     setChannelPin(3, 7);  
  62.     setChannelPin(4, 8);  
  63.      
  64.     configureChannelConstant(0);  
  65.     configureChannelConstant(1);  
  66.     configureChannelConstant(2);  
  67.      
  68.     configureChannelBlink(3, 1, 50, 0, 950);  
  69.     configureChannelBlink(4, 2, 50, 50, 1850);  
  70.      
  71.     loop_lastMillis = millis();  
  72.     loop_lastSwitchPosition = pulseIn(cfg_inputPin, HIGH) > 1500;  
  73. }  
  74.  
  75.  
  76. void loop()  
  77. {  
  78.     unsigned long duration = pulseIn(cfg_inputPin, HIGH);  
  79.     while ( duration == 0 )  
  80.     {  
  81.         duration = pulseIn(cfg_inputPin, HIGH);  
  82.     }  
  83.      
  84.     bool newSwitchPosition = duration > 1500;  
  85.     if ( newSwitchPosition != loop_lastSwitchPosition )  
  86.     {  
  87.         loop_lastSwitchPosition = newSwitchPosition;  
  88.         if ( newSwitchPosition )  
  89.         {  
  90.             ++loop_switchCount;  
  91.             loop_lastSwitchTime = millis();  
  92.         }  
  93.     }  
  94.     else  
  95.     {  
  96.         if ( loop_lastSwitchTime != 0 && millis() - loop_lastSwitchTime > 1000 )  
  97.         {  
  98.             uint8_t channel = loop_switchCount - 1;  
  99.             if ( channel < CHANNELS )  
  100.             {  
  101.                 toggleChannelSwitch( channel );  
  102.             }  
  103.             loop_switchCount = 0;  
  104.             loop_lastSwitchTime = 0;  
  105.         }  
  106.     }  
  107.      
  108.     unsigned long now = millis();  
  109.     uint16_t deltaMillis = now - loop_lastMillis;  
  110.     loop_lastMillis = now;  
  111.      
  112.     if ( deltaMillis > 0 )  
  113.     {  
  114.         update( deltaMillis );  
  115.     }  
  116. }  
  117.  
  118.  
  119. void update(uint16_t p_delta)  
  120. {  
  121.     for ( uint8_t channel = 0; channel < CHANNELS; ++channel )  
  122.     {  
  123.         if ( cfg_blinks[channel] == 0 )  
  124.         {  
  125.             continue;  
  126.         }  
  127.         if ( state_switch[channel] == false )  
  128.         {  
  129.             continue;  
  130.         }  
  131.          
  132.         if ( p_delta >= state_nextChange[channel] )  
  133.         {  
  134.             state_onOff[channel] = ! state_onOff[channel];  
  135.              
  136.             if ( state_onOff[channel] )  
  137.             {  
  138.                 state_nextChange[channel] = cfg_onDuration[channel];  
  139.                 digitalWrite(cfg_pins[channel], HIGH);  
  140.             }  
  141.             else  
  142.             {  
  143.                 state_currentBlink[channel]++;  
  144.                 if ( state_currentBlink[channel] == cfg_blinks[channel] )  
  145.                 {  
  146.                     state_nextChange[channel] = cfg_pauseDuration[channel];  
  147.                     state_currentBlink[channel] = 0;  
  148.                 }  
  149.                 else  
  150.                 {  
  151.                     state_nextChange[channel] = cfg_offDuration[channel];  
  152.                 }  
  153.                 digitalWrite(cfg_pins[channel], LOW);  
  154.             }  
  155.         }  
  156.         else  
  157.         {  
  158.             state_nextChange[channel] = state_nextChange[channel] - p_delta;  
  159.         }  
  160.     }  
  161. }  
  162.  
  163.  
  164. void setChannelSwitch(uint8_t p_channel, bool p_on)  
  165. {  
  166.     state_switch[p_channel] = p_on;  
  167.     if ( p_on == false )  
  168.     {  
  169.         digitalWrite(cfg_pins[p_channel], LOW);  
  170.     }  
  171.     else  
  172.     {  
  173.         state_onOff[p_channel]    = true;  
  174.         state_currentBlink[p_channel] = 0;  
  175.         state_nextChange[p_channel]  = cfg_onDuration[p_channel];  
  176.         digitalWrite(cfg_pins[p_channel], HIGH);  
  177.     }  
  178. }  
  179.  
  180.  
  181. void toggleChannelSwitch(uint8_t p_channel)  
  182. {  
  183.     setChannelSwitch(p_channel, !state_switch[p_channel]);  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement