Guest User

test

a guest
Jul 21st, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.04 KB | None | 0 0
  1. local radar, currentSecond, lastInput = peripheral.wrap('radar_0'), 0, 0
  2.  
  3. function initiate()
  4.     while true do
  5.         loop()
  6.         currentSecond = currentSecond + 1
  7.         sleep(1)
  8.     end
  9. end
  10.  
  11. function loop()
  12.     -- Every 2 Seconds to reduce amount of sensor calls and potentially limit lag
  13.     if ( currentSecond % 1 == 0 ) then
  14.         -- Probe Sensors
  15. print('starting second')       
  16. local isInside
  17. print('checking radar')
  18.   local players = radar.getPlayers()
  19.   if ( type(players) ~= 'table' ) then
  20.   return
  21.   end
  22.         for index, player in ipairs(players) do
  23.             if ( player.distance < 5.5 ) then
  24. print('found player '..player.name)            
  25. isInside = true
  26.             end
  27.         end
  28.   local power = (isInside and 0 or math.random(5,15))
  29. print('ended radar')
  30. print('setting state of output to '..tostring(not isInside))
  31. if ( lastInput ~= not isInside ) then
  32.     rs.setAnalogOutput('right', power)
  33.   print('completed state change '..tostring(not isInside)..':'..tostring(rs.getOutput('right')))
  34.   lastInput = (not isInside)
  35. end
  36. term.setCursorBlink(not isInside)
  37.     end
  38. end
  39.  
  40. initiate()
Advertisement
Add Comment
Please, Sign In to add comment