<?xml version="1.0" encoding="UTF-8"?>
<scxml xmlns="http://www.w3.org/2005/07/scxml" xmlns:editor="http://vscxml.com/editor" version="1.0" datamodel="ecmascript" initial="normal" name="StateMachine">
  <datamodel>
    <data id="cycle_count" expr="0"/>
  </datamodel>
  <state id="normal" initial="red" editor:x="113" editor:y="93" editor:width="564" editor:height="313" editor:label="normal" editor:color="#e0f2fe" editor:zIndex="206">
    <transition target="emergency" event="emergency" editor:waypoints="747.5,298" editor:labelAnchor="0,-1.5,-20" editor:id="t_01KEEHWWF8HAT79Q00G885XA7E"/>
    <state id="red" editor:x="64" editor:y="35" editor:width="120" editor:height="80" editor:label="red" editor:color="#ffffff" editor:zIndex="207">
      <onentry>
        <log label="LIGHT" expr="'RED - Stop'"/>
        <send event="timer" delay="5s"/>
      </onentry>
      <transition target="red_yellow" event="timer" editor:id="t_01KEEHWWF8RXJ3079SWHZSQQHW"/>
    </state>
    <state id="red_yellow" editor:x="64" editor:y="205" editor:width="120" editor:height="80" editor:label="red_yellow" editor:color="#ffffff" editor:zIndex="208">
      <onentry>
        <log label="LIGHT" expr="'RED+YELLOW - Prepare to go'"/>
        <send event="timer" delay="1500ms"/>
      </onentry>
      <transition target="green" event="timer" editor:id="t_01KEEHWWF9D0HZDQ6T7ME289MC"/>
    </state>
    <state id="green" editor:x="378" editor:y="205" editor:width="120" editor:height="80" editor:label="green" editor:color="#ffffff" editor:zIndex="209">
      <onentry>
        <log label="LIGHT" expr="'GREEN - Go'"/>
        <send event="timer" delay="5s"/>
      </onentry>
      <transition target="yellow" event="timer" editor:id="t_01KEEHWWF99N3V3PVW69SH879T"/>
    </state>
    <state id="yellow" editor:x="378" editor:y="35" editor:width="120" editor:height="80" editor:label="yellow" editor:color="#ffffff" editor:zIndex="210">
      <onentry>
        <log label="LIGHT" expr="'YELLOW - Prepare to stop'"/>
        <send event="timer" delay="2s"/>
      </onentry>
      <transition target="red" event="timer" editor:id="t_01KEEHWWF9EWGE7A3GCYEGABNE">
        <assign location="cycle_count" expr="cycle_count + 1"/>
        <log label="CYCLE" expr="'Completed cycle ' + cycle_count"/>
      </transition>
    </state>
  </state>
  <state id="emergency" initial="blink_on" editor:x="818" editor:y="149.5" editor:width="446" editor:height="200" editor:label="emergency" editor:color="#e0f2fe" editor:zIndex="273">
    <onentry>
      <log label="MODE" expr="'EMERGENCY - Yellow blinking'"/>
    </onentry>
    <transition target="normal" event="normal" editor:waypoints="747.5,208" editor:labelAnchor="0,0.5,-25" editor:id="t_01KEEHWWF9KRA0BJP864HD0241"/>
    <state id="blink_on" editor:x="28" editor:y="60" editor:width="120" editor:height="80" editor:label="blink_on" editor:color="#ffffff" editor:zIndex="275">
      <onentry>
        <log label="BLINK" expr="'YELLOW ON'"/>
        <send event="blink" delay="500ms"/>
      </onentry>
      <transition target="blink_off" event="blink" editor:waypoints="1041,272" editor:labelAnchor="0,1,22" editor:id="t_01KEEHWWF90NPWDDFG4RJRM3D8"/>
    </state>
    <state id="blink_off" editor:x="296" editor:y="60" editor:width="120" editor:height="80" editor:label="blink_off" editor:color="#ffffff" editor:zIndex="274">
      <onentry>
        <log label="BLINK" expr="'OFF'"/>
        <send event="blink" delay="500ms"/>
      </onentry>
      <transition target="blink_on" event="blink" editor:waypoints="1041,231" editor:labelAnchor="0,-1,-15" editor:id="t_01KEEHWWF9QZBCCTM0RTD1Q66R"/>
    </state>
  </state>
  <editor:notes>
    <editor:note id="n_01KEEJ32PG3CDY7M4Z61R0FNP4" x="113" y="462" width="564" height="185" color="#fef3c7" zIndex="283" wordWrap="false" >
      <![CDATA[TRAFFIC LIGHT STATE MACHINE
═══════════════════════════════════════════════════════════════

NORMAL OPERATION (counter-clockwise cycle):
  red (5s) → red_yellow (1.5s) → green (5s) → yellow (2s) → red

  • red:        Stop - vehicles must wait
  • red_yellow: Prepare to go (European style)
  • green:      Go - vehicles may proceed
  • yellow:     Prepare to stop - clear intersection

EMERGENCY MODE (blinking):
  blink_on (500ms) ↔ blink_off (500ms)

  • Yellow light flashes continuously
  • Used for maintenance or low-traffic hours

EVENTS:
  • timer      (internal)  Automatic timed transitions
  • blink      (internal)  Emergency blink toggle
  • emergency  (public)    Switch to emergency mode
  • normal     (public)    Return to normal operation]]>
    </editor:note>
  </editor:notes>
  <editor:events>
    <editor:event name="emergency" visibility="public"/>
    <editor:event name="normal" visibility="public"/>
    <editor:event name="blink" visibility="internal"/>
    <editor:event name="timer" visibility="internal"/>
  </editor:events>
</scxml>