NB: I'm french, and my english isn't perfect ... hope you will understand me ^^.
Hi everyone, i'm doing a digicode with touch screen, but i need a bit help to clean the code ^^.
Here's a screen of the final result:
and here's the code for that:
Spoiler
local m = peripheral.wrap("top") m.clear() local couleurtableau = colors.blue local couleurchiffre = colors.gray local couleurBG = colors.black local couleurcroix = colors.orange local pin = "2468V" local code1 = 0 local code2 = 0 local code3 = 0 local code4 = 0 function scanner() local scannerFrame = { ' ', ' _____ ', '| | | |', '|_|_|_|', '| | | |', '|_|_|_|', '| | | |', '|_|_|_|', '| | | |', '|_|_|_|', ' | | | ', '_|_|_|_'} for i = 1, #scannerFrame do m.setTextColor(couleurtableau) m.setCursorPos(1,i) m.write(scannerFrame[i]) m.setTextColor(couleurchiffre) end m.setBackgroundColor(couleurBG) m.setCursorPos(2,3) m.write("1") m.setCursorPos(4,3) m.write("2") m.setCursorPos(6,3) m.write("3") m.setCursorPos(2,5) m.write("4") m.setCursorPos(4,5) m.write("5") m.setCursorPos(6,5) m.write("6") m.setCursorPos(2,7) m.write("7") m.setCursorPos(4,7) m.write("8") m.setCursorPos(6,7) m.write("9") m.setTextColor(colors.lime) m.setCursorPos(2,9) m.write("V") m.setTextColor(couleurchiffre) m.setCursorPos(4,9) m.write("0") m.setTextColor(colors.red) m.setCursorPos(6,9) m.write("C") m.setBackgroundColor(colors.black) function Pass() code = code1..code2..code3..code4..code5 if pin == code then print("yeah") for i = 1,5 do if i == 1 then m.setTextColor(colors.green) end if i == 2 then m.setTextColor(colors.lime) end if i == 3 then m.setTextColor(colors.green) end if i == 4 then m.setTextColor(colors.lime) end if i == 5 then m.setTextColor(colors.green) end m.setCursorPos(1,11) m.write("X") m.setCursorPos(3,11) m.write("X") m.setCursorPos(5,11) m.write("X") m.setCursorPos(7,11) m.write("X") sleep(0.5) end else print("perdu") for i = 1,5 do if i == 1 then m.setTextColor(colors.red) end if i == 2 then m.setTextColor(colors.orange) end if i == 3 then m.setTextColor(colors.red) end if i == 4 then m.setTextColor(colors.orange) end if i == 5 then m.setTextColor(colors.red) end m.setCursorPos(1,11) m.write("X") m.setCursorPos(3,11) m.write("X") m.setCursorPos(5,11) m.write("X") m.setCursorPos(7,11) m.write("X") sleep(0.5) end os.reboot() end end function checkxy(x, y) if y == 3 then if x == 2 then code = 1 end if x == 4 then code = 2 end if x == 6 then code = 3 end end if y == 5 then if x == 2 then code = 4 end if x == 4 then code = 5 end if x == 6 then code = 6 end end if y == 7 then if x == 2 then code = 7 end if x == 4 then code = 8 end if x == 6 then code = 9 end end if y == 9 then if x == 2 then code = "V" end if x == 4 then code = 0 end if x == 6 then code = "C" end end end m.setTextColor(couleurcroix) code = 100 while code == 100 do e, side, x, y = os.pullEvent("monitor_touch") checkxy(x, y) if code == "C" then os.reboot() end code1 = code print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code1) end m.setCursorPos(1,11) m.write("X") code = 100 while code == 100 do e, side, x, y = os.pullEvent("monitor_touch") checkxy(x, y) if code == "C" then os.reboot() end code2 = code print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code2) end m.setCursorPos(3,11) m.write("X") code = 100 while code == 100 do e, side, x, y = os.pullEvent("monitor_touch") checkxy(x, y) if code == "C" then os.reboot() end code3 = code print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code3) end m.setCursorPos(5,11) m.write("X") code = 100 while code == 100 do e, side, x, y = os.pullEvent("monitor_touch") checkxy(x, y) if code == "C" then os.reboot() end code4 = code print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code4) end m.setCursorPos(7,11) m.write("X") code = 100 while code == 100 do e, side, x, y = os.pullEvent("monitor_touch") checkxy(x, y) if code == "C" then os.reboot() end code5 = code if code5 == "V" then Pass() end print("X: "..x.." Y: "..y.." Code: "..code.." Pin: "..code5) end Pass() print(code1.." "..code2.." "..code3.." "..code4.." "..code5) code = code1..code2..code3..code4..code5 print(code)
But, as you can see, the code is really ... disgusting? It work pretty fine but i wanna do better, if ever i wanna modify it later.
So i began an other code, trying to clean up this thing by using tables
Spoiler
----=== Initialisation ===---- m = peripheral.wrap("top") m.clear() m.setTextColor(colors.white) m.setBackgroundColor(colors.black) m.setTextScale(1) m.setCursorPos(1,1) local button = {} ----=== Scanner ===---- function scanner() local scannerFrame = { ' ', ' _____ ', '| | | |', '|_|_|_|', '| | | |', '|_|_|_|', '| | | |', '|_|_|_|', '| | | |', '|_|_|_|', ' | | | ', '_|_|_|_'} for i = 1, #scannerFrame do m.setCursorPos(1,i) m.write(scannerFrame[i]) end end ----=== Numbers ===---- function setTable(name, func, Posx, Posy) button[name]= {} button[name]["func"]= func button[name]["Posx"]= Posx button[name]["Posy"]= Posy end function fillTable() setTable("1", detect, 2, 3) setTable("2", detect, 4, 3) setTable("3", detect, 6, 3) setTable("4", detect, 2, 5) setTable("5", detect, 4, 5) setTable("6", detect, 6, 5) setTable("7", detect, 2, 7) setTable("8", detect, 4, 7) setTable("9", detect, 6, 7) setTable("V", detect, 2, 9) setTable("0", detect, 4, 9) setTable("C", detect, 6, 9) end function fillNumbers() for name, data in pairs(button) do local x = bData["Posx"] local y = bData["Posy"] m.setCursorPos(x,y) m.write(name) end end end end ----=== Prog ===---- scanner() fillNumbers()
But this time, no numbers appear =( (and haven't any error on screen ...). This is why i need some help to learn about table (if its table ^^), so ... please someone, tell me where i'm wrong =$