local monitor = peripheral.find("monitor")

local relays = {}
local names = {}
local state = {}

-- Reds relay 
for _, name in ipairs(peripheral.getNames()) do
    if peripheral.getType(name) == "redstone_relay" then
        table.insert(names, name)
    end
end

table.sort(names)

if #names < 4 then
    error("not faund reley")
end

for i = 1, 4 do
    relays[i] = peripheral.wrap(names[i])
    state[i] = false
end

-- Button

local function draw()
    monitor.clear()
    
    for i = 1, 4 do
        local y = i * 2 - 1
        monitor.setCursorPos(2, y)
        monitor.setTextColor(colors.white)
        monitor.write("Machine" .. i .. ":")
        
        monitor.setCursorPos(12, y)
        
        if state[i] then
            monitor.setTextColor(colors.red)
            monitor.write("Stop")
        else
            monitor.setTextColor(colors.green)
            monitor.write("Start")
            
-- Text 1-all 2-dust 3-sand 4-gravel
        monitor.setTextColor(colors.white)
        monitor.setCursorPos(20 , 1)
        monitor.write("null")
        
        monitor.setCursorPos(20, 3)
        monitor.write("dust")
        
        monitor.setCursorPos(20, 5)
        monitor.write("sand")
        
        monitor.setCursorPos(20, 7)
        monitor.write("gravel")
        
        
        end
    end
end

draw()
-- Roader
while true do
    local event, side, x, y = os.pullEvent("monitor_touch")
    
    for i = 1, 4 do
        local row = i * 2 - 1
        
        if x >= 12 and x <= 19 and y == row then
            state[i] = not state[i]
            relays[i].setOutput("top", state[i])
            draw()
         end
   end
end
