Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save futureshocked/a1ee2cef8c4081a65d39802c22152112 to your computer and use it in GitHub Desktop.
Save futureshocked/a1ee2cef8c4081a65d39802c22152112 to your computer and use it in GitHub Desktop.
This program demonstrates how to use digital lines as simple counters.
--[[
LJ - 09.80 - counter_no_debouncing.lua
This program demonstrates how to use digital lines as simple counters.
This script is a simplified version of the one at
https://labjack.com/support/software/examples/lua-scripting/counter-examples/23-counters
This script demonstrates how to create a simple counter on FIO4.
Components
----------
- LabJack T4
- Momentary button
- Pin 1 to GND
- Pin 2 to FIO4
- Wires
- Breadboard
Course
------
Data acquisition and automation with LabJack
https://app.techexplorations.com/courses/labjack/
--]]
print("Create and read a counter on FIO4.")
print("Connect a button to FIO4 and GND.")
local mbRead = MB.R --Local functions for faster processing
local mbWrite = MB.W
--1 = Rising edge, 0 = falling
local edge = 0
local bits = 0
local bits_new = 99
local count = 0
--The throttle setting can correspond roughly with the length of the Lua
--script. A rule of thumb for deciding a throttle setting is
--throttle = (3*NumLinesCode) + 20
local throttleSetting = 100 --Default throttle setting is 10 instructions
LJ.setLuaThrottle(throttleSetting)
local throttleSetting = LJ.getLuaThrottle()
print ("Current Lua Throttle Setting: ", throttleSetting)
while true do
bits_new = mbRead(2004, 0) -- Read FIO4, this will return 0 or 1
--Compare bits_new to bits
if bits ~= bits_new then
if edge == 1 then
if bits == 0 then
count = count + 1
print (" Rising total: ", count)
end
else
if bits == 1 then
count = count + 1
print (" Falling total: ", count)
end
end
bits = bits_new
mbWrite(46000, 3, count) --Save in User RAM
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment