/*

 * 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 3-pin bicolor (red-green) LED

 *          I would have expected that:

 *              Red = 255 + Green = 255 -> Yellow

 *              Red = 255 + Green = 128 -> Orange

 *          

 *          But by testing with a real diode I found that:

 *              Red = 200 + Green = 181 -> Orange

 *              Red =  32 + Green = 255 -> Yellow 

 */





#define SWITCH_ON          LOW

#define SWITCH_OFF        HIGH



#define LED_ON            HIGH

#define LED_OFF            LOW



enum SwitchStates {

    SWITCH_NC_ACTIVE,

    SWITCH_NO_ACTIVE,

    SWITCH_CENTER

};



enum SwitchStates NewSwitchState;

enum SwitchStates OldSwitchState;



int PinSwitchNc = 3;

int PinSwitchNo = 4;



int PinLed3PinRed   =  9;

int PinLed3PinGreen = 10;





void setup ()

{

    int switchNc;

    int switchNo;

  

    pinMode(PinSwitchNc, INPUT);

    pinMode(PinSwitchNo, INPUT);



    pinMode(PinLed3PinRed,   OUTPUT);

    pinMode(PinLed3PinGreen, OUTPUT);



    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(PinLed3PinRed,     0);

        analogWrite(PinLed3PinGreen, 255);

    }

    else if (OldSwitchState == SWITCH_NO_ACTIVE) // Red

    {

        analogWrite(PinLed3PinRed,   255);

        analogWrite(PinLed3PinGreen,   0);

    }

    else if (OldSwitchState == SWITCH_CENTER)    // Yellow (see notes at beginning of program)

    {

        analogWrite(PinLed3PinRed,    32);

        analogWrite(PinLed3PinGreen, 255);

    }

}





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(PinLed3PinRed,     0);

            analogWrite(PinLed3PinGreen, 255);

        }

        else if (NewSwitchState == SWITCH_NO_ACTIVE) // Red

        {

            analogWrite(PinLed3PinRed,   255);

            analogWrite(PinLed3PinGreen,   0);

        }

        else if (NewSwitchState == SWITCH_CENTER)

        {

            if (OldSwitchState == SWITCH_NC_ACTIVE)       // Yellow (see notes at start of program)

            {

                analogWrite(PinLed3PinRed,    32);

                analogWrite(PinLed3PinGreen, 255);

            }

            else if (OldSwitchState == SWITCH_NO_ACTIVE)  // Orange (see notes at start of program)

            {

                analogWrite(PinLed3PinRed,   200);

                analogWrite(PinLed3PinGreen, 181);

            }

        }

        

        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.

 */