I'm trying to build a component light-up sign using mesecons, such that each component lights up individually and sequentially. My idea is to have a Lua controller generate/increment a variable, triggered by a blinky plant, and each component lights up when the variable matches it. There's a master Lua controller to generate/increment the variable, and one at each component to check the variable and activate (or not) the individual component.
I'm having trouble with the Lua code. This is what I have:
-- initialize the variable
if(event.type == "program") then
x = 0
end
-- "light up" loop, triggered by blinky plant
if(event.type == "on") then
x = x + 1
if x > 5 then x = 0
end
-- --
-- each component checks the variable
if(x = 1) then -- replace "1" with the individual component number
port.b = pin.d
else
port.b = not pin.d
end
I can't make this work, and since I'm not familiar with Lua I am not really able to troubleshoot. Any help would be appreciated.
But I keep getting an error message: load (9): attempt to perform arithmetic on global 'x' (a nil value)
I understand that the variable is initiated as a nil value and that the controller runs thru the whole code before actually doing anything (the reason why 'x' remains nil when it gets to the 'increment' part. What I'm looking for is a way to stuff a value into x (namely, 0) in such a way that it won't keep resetting x to 0 every time the code runs, and then the if statement can increment it as needed.
What's the syntax? I'm still getting the 'nil' error.
Here's what I have right now:
-- initialize the variable
if(event.type == "program") then
mem.var = 0
x = mem.var
end
-- "light up" loop, triggered by blinky plant
if(event.type == "on") then
digiline_send("sign", x) -- placeholder, to see if x is calculating
x = x + 1
if(x > 5) then
x = 0
end
end
Does "mem.var" need to be on a separate controller or something?
-- initialize the variable
if(event.type == "program") then
mem.x = 0
end
-- "light up" loop, triggered by blinky plant
if(event.type == "on") then
digiline_send("sign", mem.x) -- placeholder, to see if x is calculating
mem.x = mem.x + 1
if(mem.x > 5) then
mem.x = 0
end
end
I should be able to get the individual components going now, but I might be back. Very much appreciate the help.
EDIT: Not as clever as I thought I was. (sad trombone noise intensifies)
So I've got a Lua subcontroller at each sign component which is supposed to check mem.x and light up if it matches the specific value. Code:
--[[ COPIED FROM CLOCK
if event.type == "program" then
mem.accept = {[1]=true,[8]=true, [9]=true}
elseif event.type == "digiline" then
port.b = mem.accept[event.msg.mem.x]
-- port.b, port.d = port.a, port.a
end
]]
-- ORIGINAL
if(event.channel == "sign") then
--mem.accept[mem.x]
if(mem.x == 1) or
(mem.x >7) then
port.b = true
else
port.b = false
end
end
Note: the second block of code is my original (and preferred) attempt; the replacement code above it is copied from mesecons digital clock, which I have built and (mostly) works. In my original code, it gives no error messages but nothing happens (the commented out line gives an error "= expected near if"), and in the replacement code the error says "attempt to index field "msg" (a number value)"
Thru experimentation I've discovered where the problem lie: the value of mem.x is not being sent from the main controller to the sub controllers, but I can't figure out why not. Would it just be better to eliminate the central controller and have each subcontroller do its own counting?
I tried something like that, where the master sends a signal to the other controllers which then each count their own mem.x, but the controllers kept overheating.
I have found a bit of a working solution but it's a tad inelegant IMO: Just remove the master controller entirely and wire the subcontrollers directly to the blinky. Now I just gotta figure out how to make my mesecon wiring less cluttered. Talk about inelegant! :P
In any case, thanks for the help, it has been invaluable. :D