Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.loadAPI("button")
  2. m = peripheral.wrap("right")
  3. m.clear()
  4.  
  5. local time = 15
  6. local enabled = false
  7. local count = 0
  8. local dir = 0
  9.  
  10. function pulse(color)
  11.     rs.setBundledOutput("back", color)
  12.     sleep(1)
  13.     rs.setBundledOutput("back", 0)
  14. end
  15.  
  16. local function getDir()
  17.   if dir == 1 then
  18.     return "Forward"
  19.   elseif dir == 2 then
  20.     return "Backward"
  21.   elseif dir == 3 then
  22.     return "Left"
  23.   elseif dir == 4 then
  24.     return "Right"
  25.   else
  26.     return "None"
  27.   end
  28. end
  29.    
  30. function status()
  31.   if enabled then m.setBackgroundColor(colors.green)
  32.   else m.setBackgroundColor(colors.red) end
  33.    
  34.   m.setCursorPos(2,2)
  35.   m.write("                           ")
  36.   m.setCursorPos(2,3)
  37.   m.write("                           ")
  38.   m.setCursorPos(2,4)
  39.   m.write("                           ")
  40.  
  41.   w,h = m.getSize()
  42.  
  43.   if enabled then
  44.     m.setCursorPos((w-string.len("Status : Enabled"))/2+1, 2)
  45.     m.write("Status : Enabled")
  46.   else
  47.     m.setCursorPos((w-string.len("Status : Disabled"))/2+1, 2)
  48.     m.write("Status : Disabled")
  49.   end
  50.  
  51.   m.setCursorPos((w-string.len("Count : "..count))/2+1, 3)
  52.   m.write("Count : "..count)
  53.  
  54.   m.setCursorPos((w-string.len("Direction : "..getDir(dir)))/2+1, 4)
  55.   m.write("Direction : "..getDir(dir))
  56. end
  57.  
  58. function fillTable()
  59.    button.setTable("Forward", forward, 10,20,6,6)
  60.    button.setTable("Left", left, 2,12,8,8)
  61.    button.setTable("Right", right, 18,28,8,8)
  62.    button.setTable("Back", back, 10,20,10,10)
  63.    button.screen()
  64. end
  65.  
  66. function getClick()
  67.    event,side,x,y = os.pullEvent("monitor_touch")
  68.    button.checkxy(x,y)
  69. end
  70.  
  71. function forward()
  72.    button.toggleButton("Forward")
  73.    enabled = true
  74.    dir = 1
  75.    count = 10
  76.    
  77.    while count > 0 do
  78.      pulse(colors.red)
  79.    
  80.      count = count - 1
  81.      status()
  82.      sleep(time)
  83.    end
  84.    
  85.    button.toggleButton("Forward")
  86.    enabled = false
  87.    dir = 0
  88.    
  89.    status()
  90. end
  91.  
  92. function back()
  93.   button.toggleButton("Back")
  94.   enabled = true
  95.   dir = 2
  96.   count = 100
  97.  
  98.   while count > 0 do
  99.     pulse(colors.green)
  100.     count = count - 1  
  101.     status()
  102.     sleep(time)
  103.   end
  104.  
  105.   button.toggleButton("Back")
  106.   enabled = false
  107.   dir = 0
  108.  
  109.   status()
  110. end
  111.  
  112. function left()
  113.   button.toggleButton("Left")
  114.   enabled = true
  115.   dir = 3
  116.   count = 5
  117.  
  118.   while count > 0 do
  119.     pulse(colors.brown)
  120.     count = count - 1  
  121.     status()
  122.     sleep(3)
  123.   end
  124.  
  125.   button.toggleButton("Left")
  126.   enabled = false
  127.   dir = 0
  128.  
  129.   status()
  130. end
  131.  
  132. function right()
  133.   button.toggleButton("Right")
  134.   enabled = true
  135.   dir = 4
  136.   count = 5
  137.  
  138.   while count > 0 do
  139.     pulse(colors.blue)
  140.     count = count - 1  
  141.     status()
  142.     sleep(3)
  143.   end
  144.  
  145.   button.toggleButton("Right")
  146.   enabled = false
  147.   dir = 0
  148.  
  149.   status()
  150. end
  151.  
  152. fillTable()
  153. status()
  154.  
  155. while true do
  156.    getClick()
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement