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

Power Managment problems

Started by Vicirga, 22 March 2014 - 01:11 PM
Vicirga #1
Posted 22 March 2014 - 02:11 PM
I am having problems writing a program to manage 4 redstone energy cells at once. The goal here is to have each redstone energy (each powered by its own series of engines) be charged when it is NOT full and turn the engines off when it IS full. I have figured out how to communicate with the colored cables as well as a separate monitor (via wireless router) however, I cannot seem to get all 4 while loops running concurrently. Either they run one after the other or they will cycle on for a few ticks and then turn off.

Keep in mind: Everything I know about Lua i have either taught myself or have seen in tutorials. I am very new to the language.

Here is my setup:
Spoiler
Here is my code:
Spoiler

local gatereader1 = peripheral.wrap("right")
	
local gatereader2 = peripheral.wrap("front")
	
local gatereader3 = peripheral.wrap("left")
	
local gatereader4 = peripheral.wrap("back")
	
local sRedstoneOutSide = "bottom"
	
local data
	
local bfull
	
rednet.open("top")
	
term.clear()
	
term.setCursorPos(1,1)
	
term.write("eMonitor v2.2")
	
term.setCursorPos(1,2)
	
term.write("Status: Running")
	
term.setCursorPos(1,3)
	
term.write("Hold Ctrl+T to terminate")
	
while(true) do
	
  data = gatereader1.get()  
	
  bFull = data["Full Energy"]
	
  term.setCursorPos(1,5)
	
  if(bFull) then
	
	term.write("Module 1: FULL")
	
	rednet.send(24, "1 is full")
	
	redstone.setBundledOutput("bottom", 0)
	
  else
	
	term.write("Module 1: CHARGING")
	
	rednet.send(24, "1 is NOT full")
	
	redstone.setBundledOutput("bottom", 1)
	
  end
	
  sleep(2)
	
end
	
while(true) do  
	
  data = gatereader2.get()  
	
  bFull = data["Full Energy"]
	
  term.setCursorPos(1,6)
	
  if(bFull) then
	
	term.write("Module 2: FULL")
	
	rednet.send(24, "2 is full")
	
	redstone.setBundledOutput("bottom", 0)
	
  else
	
	term.write("Module 2: CHARGING")
	
	rednet.send(24, "2 is NOT full")
	
	redstone.setBundledOutput("bottom", 2)
	
  end
	
  sleep(2)
	
end
	
while(true) do  
	
  data = gatereader3.get()  
	
  bFull = data["Full Energy"]
	
  term.setCursorPos(1,7)
	
  if(bFull) then
	
	term.write("Module 3: FULL")
	
	rednet.send(24, "3 is full")
	
	redstone.setBundledOutput("bottom", 0)
	
  else
	
	term.write("Module 3: CHARGING")
	
	rednet.send(24, "3 is NOT full")
	
	redstone.setBundledOutput("bottom", 3)
	
  end
	
  sleep(2)
	
end
	
while(true) do  
	
  data = gatereader4.get()  
	
  bFull = data["Full Energy"]
	
  term.setCursorPos(1,8)
	
  if(bFull) then
	
	term.write("Module 4: FULL")
	
	rednet.send(24, "4 is full")
	
	redstone.setBundledOutput("bottom", 0)
	
  else
	
	term.write("Module 4: CHARGING")
	
	rednet.send(24, "4 is NOT full")
	
	redstone.setBundledOutput("bottom", 4)
	
  end
	
  sleep(2)
	
end
Edited on 22 March 2014 - 04:35 PM
Yevano #2
Posted 22 March 2014 - 06:02 PM
You could wrap each of those while loops inside their own functions and make a call to parallel.waitForAll. This will run all the functions cooperatively as long as they all pull events or yield in some other way. (They all sleep, so it looks like you're good.) Note however that you probably don't have to do it this way. It looks like you could perhaps condense it all into one while loop, reading from each gate reader at each iteration.
CometWolf #3
Posted 22 March 2014 - 06:19 PM
This sounds like an awefully redundant program, considering what the gate reader does in the first place… Anyways, yeah the current setup won't work since

while true do
  --code
end
means it will loop forever, until it hits a break statement. So your program would get suck in the first loop. The easy way out would be the parallel API like Yevano suggested. Personally i would set it all up as one loop in the following matter.

local tGate = { --store all the gate readers in the same table, for easier acess
  [1] = peripheral.wrap("right"),
  [2] = peripheral.wrap("front"),
  [3] = peripheral.wrap("left"),
  [4] = peripheral.wrap("back")
}
rednet.open("top")
term.clear()
term.setCursorPos(1,1)
term.write("eMonitor v2.2")
term.write("Status: Running")
term.setCursorPos(1,3)
term.write("Hold Ctrl+T to terminate")
while(true) do
  for number,gate in pairs(tGate) do -- loop through the gate table
  local data = gate.get()
  local bFull = data["Full Energy"]
  term.setCursorPos(1,4+number)
  if(bFull) then
		term.write("Module "..number..": FULL")
		rednet.send(24, number.." is full")
		redstone.setBundledOutput("bottom", colors.subtract(rs.getBundledInput"bottom",number ^ 2))
  else
		term.write("Module "..number..": CHARGING")
		rednet.send(24, number.." is NOT full")
		redstone.setBundledOutput("bottom", colors.combine(rs.getBundledInput"bottom",number ^ 2))
  end
  sleep(2)
end
Based on the way you had your redstone bundle code set up, im gonna go out on a limb and say you have no idea how it works. So… read this
http://computercraft...etBundledOutput
When you set it to 0, you clear all the colors.
When you set it to 3, it activates the colors 1 and 2, whatever they may be. To get the color values easily, use

colors.colorName
http://computercraft...olors_%28API%29
Edited on 22 March 2014 - 05:20 PM
Vicirga #4
Posted 23 March 2014 - 01:11 AM
Yea, like I said in my post I am totally new to lua. Everything I have learned, I have learned while writing this or some other minuscule programs. Thank you so much for the help. I will try this out and post the results. :)/>
CometWolf #5
Posted 23 March 2014 - 02:01 AM
The code i posted still won't work, because like i said the colors are not correct. Your image dosen't work so i dunno what colors you use.