This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
gknova61's profile picture

Make a program send redstone signals and monitor for rs signals/rednet messages at the same time?

Started by gknova61, 23 November 2012 - 07:30 AM
gknova61 #1
Posted 23 November 2012 - 08:30 AM
Basically, i wana know how to run something like ospullEvent that would yield the computer and simultaneously run a program that would say, send redstone signals all on the same computer and like if the computer were to get a rednet message or redstone signal on another side that it's sending redstons signals, like have the ospullevent get it and have it like affect the program sending redstone signals. Would this have to do with something called goroutines or parallels?
Lyqyd #2
Posted 23 November 2012 - 09:24 AM
You could write a single loop to handle that task. A basic understanding of timers and correct event handling should make for a significantly simpler solution than some parallel monstrosity.
gknova61 #3
Posted 23 November 2012 - 09:29 AM
Well, it'll be constantly sending redstone signals. The program i have is actually a redstone timer ;)/>/>/>/> here is my program code:
http://pastebin.com/XR9C8z75

I'd like to be able to multitask in this case. If i were to use parallels, it'd only be running 2 functions at once anyways… One to wait and one to send redstone signals. I just read up on the parallels api and have an understanding of how it works but i'm sure that people can rewrite this program so it works effieciently and all that jazz -_-/>/>
Lyqyd #4
Posted 23 November 2012 - 09:37 AM
I'm not sure what your point is. That's a very simple loop. It would be extremely easy to convert that to an event-based loop and add other functionality to it.
gknova61 #5
Posted 23 November 2012 - 10:10 AM
Well, if it can send 0.1sec interval redstone signals, and wait for a rednet message simultaneously using a loop, by all means ;)/>/>
Lyqyd #6
Posted 23 November 2012 - 12:07 PM

local spaceTimerLength, spaceTimer, pulseTimer, pulseCount = 0.8, nil, nil, 0
local updateInterval, updateTarget = 10, 1
while true do
	e, p1, p2 = os.pullEvent()
	if e == "timer" then
		if p1 == spaceTimer then
			redstone.setOutput("back", true)
			pulseTimer = os.startTimer(0.1)
		elseif p1 == pulseTimer then
			redstone.setOutput("back", false)
			spaceTimer = os.startTimer(spaceTimerLength)
			pulseCount = pulseCount + 1
			if pulseCount % updateInterval == 0 then
				rednet.send(updateTarget, pulseCount / updateInterval)
			end
		end
	elseif e == "rednet_message" then
		if p1 == updateTarget and p2 == "reset" then
			pulseCount = 0
		elseif p1 == updateTarget and p2 == "stop" then
			spaceTimer = nil
			pulseTimer = nil
			redstone.setOutput("back", false)
		elseif p1 == updateTarget and p2 == "start" then
			spaceTimer = os.startTimer(0)
		end
	end
end
gknova61 #7
Posted 27 November 2012 - 08:27 PM
Like this, lyqyd?
http://pastebin.com/XR9C8z75