/*
 * Board:   Arduino Uno
 * Target:  PE Series on LED Effects
 * Author:  Clive "Max" Maxfield (max@clivemaxfield.com) 
 * License: The MIT License (See full license at the bottom of this file)
 *
 * Notes:   Using a 2-pin bicolor (yellow-green) LED
 *          This program is total overkill for what it ends up doing, but it was
 *          based on the earlier program for the 3-pin bicolor LED because I wanted
 *          to experiment to see if I could achieve any useful intermediate colors
 *          (I couldn't -- but maybe I'd have had more luck with a red-green LED).
 */


#define SWITCH_ON          LOW
#define SWITCH_OFF        HIGH

enum SwitchStates {
    SWITCH_NC_ACTIVE,
    SWITCH_NO_ACTIVE,
    SWITCH_CENTER
};

enum SwitchStates NewSwitchState;
enum SwitchStates OldSwitchState;


int PinSwitchNc = 3;
int PinSwitchNo = 4;

int PinLed2PinYellow = 6;
int PinLed2PinGreen  = 5;


void setup ()
{
//    Serial.begin (9600);

    int switchNc;
    int switchNo;
  
    pinMode(PinSwitchNc, INPUT);
    pinMode(PinSwitchNo, INPUT);

    switchNc = digitalRead(PinSwitchNc);
    switchNo = digitalRead(PinSwitchNo);

    // The following is overkill on the conditions to make it clear what's going on
    if      ( (switchNc == SWITCH_ON)  && (switchNo == SWITCH_OFF) ) OldSwitchState = SWITCH_NC_ACTIVE;
    else if ( (switchNc == SWITCH_OFF) && (switchNo == SWITCH_ON)  ) OldSwitchState = SWITCH_NO_ACTIVE;
    else if ( (switchNc == SWITCH_OFF) && (switchNo == SWITCH_OFF) ) OldSwitchState = SWITCH_CENTER;

    // The following is overkill on the conditions to make it clear what's going on
    if (OldSwitchState == SWITCH_NC_ACTIVE)      // Green
    {
        analogWrite(PinLed2PinGreen,  255);
        analogWrite(PinLed2PinYellow,   0);
    }
    else if (OldSwitchState == SWITCH_NO_ACTIVE) // Yellow
    {
        analogWrite(PinLed2PinGreen,    0);
        analogWrite(PinLed2PinYellow, 255);
    }
    else if (OldSwitchState == SWITCH_CENTER)    // Neither
    {
        analogWrite(PinLed2PinGreen,    0);
        analogWrite(PinLed2PinYellow,   0);
    }
}

void loop ()
{
    int switchNc;
    int switchNo;

    switchNc = digitalRead(PinSwitchNc);
    switchNo = digitalRead(PinSwitchNo);

    // The following is overkill on the conditions to make it clear what's going on
    if      ( (switchNc == SWITCH_ON)  && (switchNo == SWITCH_OFF) ) NewSwitchState = SWITCH_NC_ACTIVE;
    else if ( (switchNc == SWITCH_OFF) && (switchNo == SWITCH_ON)  ) NewSwitchState = SWITCH_NO_ACTIVE;
    else if ( (switchNc == SWITCH_OFF) && (switchNo == SWITCH_OFF) ) NewSwitchState = SWITCH_CENTER;

    if (NewSwitchState != OldSwitchState)
    {
        // The following is overkill on the conditions to make it clear what's going on
        if (NewSwitchState == SWITCH_NC_ACTIVE)      // Green
        {
            analogWrite(PinLed2PinGreen,  255);
            analogWrite(PinLed2PinYellow,   0);
        }
        else if (NewSwitchState == SWITCH_NO_ACTIVE) // Yellow
        {
            analogWrite(PinLed2PinGreen,    0);
            analogWrite(PinLed2PinYellow, 255);
        }
        else if (NewSwitchState == SWITCH_CENTER)    // Neither
        {
            analogWrite(PinLed2PinGreen,    0);
            analogWrite(PinLed2PinYellow,   0);
        }
        
        OldSwitchState = NewSwitchState;
    }
}


/*
 * Copyright (c) 2020 Clive "Max" Maxfield
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and any associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHOR(S) OR COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */