%=========================================================
%=====          The "TRAFFIC LIGHTS" Routine         =====
%=========================================================
%===== The "Traffic Lights" Algorithm Is An FSM      =====
%===== With Four Internal States. The Input Stimulus =====
%===== (Signal To Change) Produces Different Results =====
%===== (Pattern of Lights) For Each of The States,   =====
%===== And Determines The Next State Entered.        =====
%=========================================================
%===== State 1: RED = off, AMBER = off, Green = on   =====
%===== State 2: RED = off, AMBER = on, Green = off   =====
%===== State 3: RED = on, AMBER = off, Green = off   =====
%===== State 4: RED = on, AMBER = on, Green = off    =====
%=========================================================

function TrafficLightsFSM()

clear
clc

Input =  input('Input Value: 1 For Going To The Next State: ');

if (Input == 1)
    State = 1;
    while (Input ~= 9)
        if (State == 1)
            display('=======');
            display('STATE 1');
            display('=======');
            display('RED = off');
            display('AMBER = off');
            display('GREEN = on');
            State = State + 1;
        elseif (State == 2)
            display('=======');
            display('STATE 2');
            display('=======');
            display('RED = off');
            display('AMBER = on');
            display('GREEN = off');
            State = State + 1;
        elseif (State == 3)
            display('=======');
            display('STATE 3');
            display('=======');
            display('RED = on');
            display('AMBER = off');
            display('GREEN = off');
            State = State + 1;
        elseif (State == 4)
            display('=======');
            display('STATE 4');
            display('=======');
            display('RED = on');
            display('AMBER = on');
            display('GREEN = off');
            State = 1;
        end
        Input =  input('Input Value: 1 For Going To The Next State, 9 For Exit: ');
    end
else
    display('Valid Initial Input Is 1');
end
end