Sequentially Flashing Sign?

Post Reply
BentonBuilder3011
New member
Posts: 7
Joined: Sat May 15, 2021 16:09

Sequentially Flashing Sign?

by BentonBuilder3011 » Post

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:

Code: Select all

    
       -- 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.

User avatar
PolySaken
Member
Posts: 817
Joined: Thu Nov 09, 2017 05:18
GitHub: PolySaken-I-Am
In-game: PolySaken
Location: Wānaka, Aotearoa
Contact:

Re: Sequentially Flashing Sign?

by PolySaken » Post

should be x==1 not x=1 in the if statement.
Guidebook Lib, for in-game docs | Poly Decor, some cool blocks | Vision Lib, an all-purpose library.

BentonBuilder3011
New member
Posts: 7
Joined: Sat May 15, 2021 16:09

Re: Sequentially Flashing Sign?

by BentonBuilder3011 » Post

Okay, fixed the if statement, thx for the catch

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.

User avatar
Desour
Member
Posts: 1469
Joined: Thu Jun 19, 2014 19:49
GitHub: Desour
IRC: Desour
In-game: DS
Location: I'm scared that if this is too exact, I will be unable to use my keyboard.

Re: Sequentially Flashing Sign?

by Desour » Post

Use the global `mem` variable to store state.
he/him; Codeberg; GitHub; ContentDB; public personal TODO list; "DS" is preferred (but often too short)

BentonBuilder3011
New member
Posts: 7
Joined: Sat May 15, 2021 16:09

Re: Sequentially Flashing Sign?

by BentonBuilder3011 » Post

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?

User avatar
PolySaken
Member
Posts: 817
Joined: Thu Nov 09, 2017 05:18
GitHub: PolySaken-I-Am
In-game: PolySaken
Location: Wānaka, Aotearoa
Contact:

Re: Sequentially Flashing Sign?

by PolySaken » Post

Code: Select all

-- 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
Needs to be like this. X gets erased each cycle.
Guidebook Lib, for in-game docs | Poly Decor, some cool blocks | Vision Lib, an all-purpose library.

BentonBuilder3011
New member
Posts: 7
Joined: Sat May 15, 2021 16:09

Re: Sequentially Flashing Sign?

by BentonBuilder3011 » Post

WOOT That got it, thanks much!

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)"

BentonBuilder3011
New member
Posts: 7
Joined: Sat May 15, 2021 16:09

Re: Sequentially Flashing Sign?

by BentonBuilder3011 » Post

Not sure if it will help, but here's a drawing of the sign's wiring, just in case I messed that part up.
blinky sign diag.PNG
blinky sign diag.PNG (87.74 KiB) Viewed 586 times
The sequential sub-controllers at the top are the ones I'm working on now. Master is working.

BentonBuilder3011
New member
Posts: 7
Joined: Sat May 15, 2021 16:09

Re: Sequentially Flashing Sign?

by BentonBuilder3011 » Post

So just to bump this up a bit,

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?

User avatar
PolySaken
Member
Posts: 817
Joined: Thu Nov 09, 2017 05:18
GitHub: PolySaken-I-Am
In-game: PolySaken
Location: Wānaka, Aotearoa
Contact:

Re: Sequentially Flashing Sign?

by PolySaken » Post

mem table is per-controller, so it's better to have each controller update when it receives a signal from the master controller

alternatively you could use digilines but that's a bit out of the scope of what you're doing
Guidebook Lib, for in-game docs | Poly Decor, some cool blocks | Vision Lib, an all-purpose library.

BentonBuilder3011
New member
Posts: 7
Joined: Sat May 15, 2021 16:09

Re: Sequentially Flashing Sign?

by BentonBuilder3011 » Post

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

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests