--

local delta =   4*60+20  --   HERE  IS THE VALUE 

local owdata = S.BaHi_T6
local numval = 5
local switchbox_cp = { x=-2221,y=14,z=297}

function save_delay(d)
     if not owndata then
         owndata = { false, false, false, false, false, false, false, false, }
     end
     for i=numval,2,-1 do
          owndata[i] = owndata[i-1]
     end
     owndata[1] = d 
end

function delay_text()
     if not owndata then
          return ""
     end
     local rg = ""
     for i=1,numval do
          h = owndata[i]
          if not h then h = "" end
          rg = rg ..  h .." "
     end
     return rg
end

if event.type == "train" then
     if atc_arrow then
          atc_send("B0WOL")   -- D20OCD3S2")
--          setstate("BaHi_T5_S2","st")
--          setstate("BaHi_T5_S1","st")
          
          if not S.BaHi_T6 then
               S.BaHi_T6 = os.time()
          end
--               interrupt(1,"departure")
--          else
               now = os.time()
               tempdist = now-S.BaHi_T6
               save_delay(tempdist)
               delaystr =  delay_text()
                output =  "last:  "..S.BaHi_T6 .. "\nnow:   " .. now .. "\ndelta: " .. tempdist.. "\n"..delta .. "  " .. delaystr
               digiline_send("lcd",output)
               interrupt_pos( switchbox_cp, { action = "display", data = output } )
               if tempdist > delta then
                    interrupt(15,"departure")
               else
                    interrupt(-tempdist+delta,"departure")
               end
--          end
     end
     return
end

if event.type == "int" then
     if event.msg == "departure" then 
 --         setstate("BaHi_T5_S2","st")
 --         setstate("BaHi_T5_S1","st")
           atc_set_text_inside("S23 -> Redwood")
           atc_send("ROCD3S2")
               now = os.time()
              delaystr =  delay_text()
--               digiline_send("lcd","last:  \nnow:   " .. now .. "\ndelta: " .. tempdist.. "\n"..delta .. "  " .. delaystr)
          output = "last:  " .. now .. "  \n   \n  \n"..delta.."  "..delaystr 
          digiline_send("lcd",output )
          interrupt_pos( switchbox_cp, { action = "display", data = output } )
          S.BaHi_T6 = now
     end
     return
end