Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NavLights.ino
- // Arduino based remote controlled navigation lights
- //
- // By Daniel van den Ouden, 2014
- //
- // configuration for input
- uint8_t cfg_inputPin;
- // 5 output channels
- #define CHANNELS 5
- 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
- uint16_t cfg_onDuration[CHANNELS]; // how long the channel will be on in milliseconds, 16 bit, so value is 0 - 65535
- uint16_t cfg_offDuration[CHANNELS]; // how long the channel will be off between blinks in milliseconds, 16 bit
- uint16_t cfg_pauseDuration[CHANNELS]; // how long the channel will be off between the last and first blink of a sequence, 16 bit
- uint8_t cfg_pins[CHANNELS]; // which pin to use for which channel
- bool state_switch[CHANNELS]; // state of the channel's "switch" (true = on, false = off)
- bool state_onOff[CHANNELS]; // the output state of the channel (true = high, false = low)
- uint8_t state_currentBlink[CHANNELS]; // how often this channel has blinked in the current sequence
- uint16_t state_nextChange[CHANNELS]; // number of milliseconds until next change
- unsigned long loop_lastMillis; // time of last update
- bool loop_lastSwitchPosition; // last measured switch position
- uint8_t loop_switchCount; // number of switches in current sequence
- unsigned long loop_lastSwitchTime; // time at which the switch was flipped last
- void setChannelPin(uint8_t p_channel, uint8_t p_pin)
- {
- digitalWrite(p_pin, LOW);
- pinMode(p_pin, OUTPUT);
- cfg_pins[p_channel] = p_pin;
- }
- void configureChannelBlink(uint8_t p_channel, uint8_t p_blinks, uint16_t p_onDuration, uint16_t p_offDuration, uint16_t p_pauseDuration)
- {
- cfg_blinks[p_channel] = p_blinks;
- cfg_onDuration[p_channel] = p_onDuration;
- cfg_offDuration[p_channel] = p_offDuration;
- cfg_pauseDuration[p_channel] = p_pauseDuration;
- setChannelSwitch(p_channel, true);
- }
- void configureChannelConstant(uint8_t p_channel)
- {
- cfg_blinks[p_channel] = 0;
- setChannelSwitch(p_channel, true);
- }
- void setup()
- {
- cfg_inputPin = 9;
- setChannelPin(0, 4);
- setChannelPin(1, 5);
- setChannelPin(2, 6);
- setChannelPin(3, 7);
- setChannelPin(4, 8);
- configureChannelConstant(0);
- configureChannelConstant(1);
- configureChannelConstant(2);
- configureChannelBlink(3, 1, 50, 0, 950);
- configureChannelBlink(4, 2, 50, 50, 1850);
- loop_lastMillis = millis();
- loop_lastSwitchPosition = pulseIn(cfg_inputPin, HIGH) > 1500;
- }
- void loop()
- {
- unsigned long duration = pulseIn(cfg_inputPin, HIGH);
- while ( duration == 0 )
- {
- duration = pulseIn(cfg_inputPin, HIGH);
- }
- bool newSwitchPosition = duration > 1500;
- if ( newSwitchPosition != loop_lastSwitchPosition )
- {
- loop_lastSwitchPosition = newSwitchPosition;
- if ( newSwitchPosition )
- {
- ++loop_switchCount;
- loop_lastSwitchTime = millis();
- }
- }
- else
- {
- if ( loop_lastSwitchTime != 0 && millis() - loop_lastSwitchTime > 1000 )
- {
- uint8_t channel = loop_switchCount - 1;
- if ( channel < CHANNELS )
- {
- toggleChannelSwitch( channel );
- }
- loop_switchCount = 0;
- loop_lastSwitchTime = 0;
- }
- }
- unsigned long now = millis();
- uint16_t deltaMillis = now - loop_lastMillis;
- loop_lastMillis = now;
- if ( deltaMillis > 0 )
- {
- update( deltaMillis );
- }
- }
- void update(uint16_t p_delta)
- {
- for ( uint8_t channel = 0; channel < CHANNELS; ++channel )
- {
- if ( cfg_blinks[channel] == 0 )
- {
- continue;
- }
- if ( state_switch[channel] == false )
- {
- continue;
- }
- if ( p_delta >= state_nextChange[channel] )
- {
- state_onOff[channel] = ! state_onOff[channel];
- if ( state_onOff[channel] )
- {
- state_nextChange[channel] = cfg_onDuration[channel];
- digitalWrite(cfg_pins[channel], HIGH);
- }
- else
- {
- state_currentBlink[channel]++;
- if ( state_currentBlink[channel] == cfg_blinks[channel] )
- {
- state_nextChange[channel] = cfg_pauseDuration[channel];
- state_currentBlink[channel] = 0;
- }
- else
- {
- state_nextChange[channel] = cfg_offDuration[channel];
- }
- digitalWrite(cfg_pins[channel], LOW);
- }
- }
- else
- {
- state_nextChange[channel] = state_nextChange[channel] - p_delta;
- }
- }
- }
- void setChannelSwitch(uint8_t p_channel, bool p_on)
- {
- state_switch[p_channel] = p_on;
- if ( p_on == false )
- {
- digitalWrite(cfg_pins[p_channel], LOW);
- }
- else
- {
- state_onOff[p_channel] = true;
- state_currentBlink[p_channel] = 0;
- state_nextChange[p_channel] = cfg_onDuration[p_channel];
- digitalWrite(cfg_pins[p_channel], HIGH);
- }
- }
- void toggleChannelSwitch(uint8_t p_channel)
- {
- setChannelSwitch(p_channel, !state_switch[p_channel]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement