Advertisement
carlosjuero

ccSensors Automated Lights application - documented

Sep 13th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. os.unloadAPI("sensors")
  2. os.loadAPI("rom/apis/sensors")
  3.  
  4. local receiverID = 1 --Change this to the id of the computer controlling your lighting system
  5.  
  6. scontrol = sensors.getController()
  7.  
  8. print("Attaching Sensor Controller on "..scontrol.." side")
  9. print("Press X to exit application")
  10.  
  11. avSensors = sensors.getSensors(scontrol)
  12.  
  13. sUseSensor = avSensors[1] -- You need to change the index here to match the index of the sensor containing the World Sensor
  14.                           -- Module Card. SMP ComputerCraft is a bit glitched in that all sensors are named 'Sensor' even if
  15.                           -- manually changed. Use the /ccSensors/console application to find the id of the sensor to use
  16.  
  17. avProbes = sensors.getProbes(scontrol, sUseSensor)
  18.  
  19. sWorldProbe = avProbes[1]  -- Probe index numbers are static, you can either assign variables like this or just go straight to
  20. sAreaProbe = avProbes[2]   -- the area probe index (which is 2) as the Area Probe contains the light level data.
  21. sBiomeProbe = avProbes[3]
  22.  
  23. do while true
  24. --[[ Since I use a timer (set at 1 minute interval) to pulse the sensor 'controller' computer, I use the os.pullEvent() to
  25. detect the redstone signal (or the exit keystroke) and then run the sensor routine. You could also use the built in Timer
  26. function to simulate the same sort of thing (though it is slightly less reliable imo) ]]--
  27.     local event,param1 = os.pullEvent()
  28.     if event == "redstone" then
  29.         if rs.getInput("back") == true then -- Change "back" to whatever side you have the redstone pulse coming in
  30.             spReading = sensors.getReading2(scontrol,sUseSensor,sAreaProbe)
  31.             sprLightLevel = spReading["LightLevel"]
  32. --[[ Change the rednet.open("right") statement to reflect what side the modem is on your computer ]]--
  33.             if tonumber(sprLightLevel) <= 8 then -- the getReading2 function returns all values as strings, you need to
  34.                 rednet.open("right")             -- convert the string to a number to properly compare the light level
  35.                 rednet.send(receiverID, "lightson")
  36.                 rednet.close("right")
  37.             else
  38.                 rednet.open("right")
  39.                 rednet.send(receiverID, "lightsoff")
  40.                 rednet.close("right")
  41.             end
  42.         end
  43.     elseif event == "char" then -- This just detects key presses and, if the proper key is pressed, exits the application
  44.         if (param1 == "x") or (param1 == "X")
  45.             term.clear()
  46.             term.setCursorPos(1,1)
  47.             print("Bye")
  48.             return
  49.         end
  50.     end
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement