Skip to content

Instantly share code, notes, and snippets.

@johnosullivan
Created May 15, 2017 03:20
Show Gist options
  • Save johnosullivan/78af43d812fb317b8c10ec887502407c to your computer and use it in GitHub Desktop.
Save johnosullivan/78af43d812fb317b8c10ec887502407c to your computer and use it in GitHub Desktop.
BotLib Demo1
#!/usr/bin/lua
--- Imports the botlib libraries that support this demo.
lib = require("botlibsensor")
GPIO = require("botlibgpio")
--- Creates a simple function to sleep before each sensor read.
function sleep(n)
os.execute("sleep " .. tonumber(n))
end
--- Configures the hardware.
sensor = lib.new("Range#1","HC-SR04",18,24)
pin = GPIO(25,"out")
--- Logic of the simple demo.
while (true) do
--- Thread sleep for 05 ms.
sleep(0.05)
--- Gets the distance value from component read.
distance = sensor:read()
--- If statement to check if distance is greater or less than 10 cm.
if (distance > 10) then
--- Turns the LED off.
pin:write(0)
else
--- Turns the LED on.
pin:write(1)
end
--- print(distance)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment