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

Problems with two of my programs.

Started by Ceirenthir, 26 April 2012 - 04:57 PM
Ceirenthir #1
Posted 26 April 2012 - 06:57 PM
I have two programs, one is a "monitor" program that receives status updates from the other, the "control" program.
The control program regulates a nuclear reactor from IC2. The problem is kinda minor, I just can't seem to figure out why when the monitor is updated, the "Reactor Control Connected" variable seems to flash "Not Connected" and "Connected" multiple times, problem is, the code shouldn't allow it to do that so quickly.

Oh and a pretty big problem is that I run these two programs along with a main control program and the server gives overflow warnings. :/ So someone please look these two over and point out any problems, please.

I've attached the two programs to this post as well.
I'm also fairly certain the control program is not the culprit, nor do I have any problems with it, it's fairly simple.


Reactor Control
—————————–
Spoiler

-- Reactor Control v2.1b
local version = "v2.1b"
local computerId = 0
local mainControlId = 2
local monitorId = 4
local keepRunning = "False"
local screenMessage = "0"
local screenWarning = "0"
local screenStatus = "0"
local emergencyShutdown = "Off"
local modemState = "Off"
local reactorRunning = false
local reactorCooling = false
local mainControlConnected = "Not Connected"
local monitorConnected = "Not Connected"
local reactorRunningTimer = false
local reactorCoolingTimer = false
local mainControlPingBack = true
local r1 = false
local r2 = false
local r3 = false
local r4 = false
local r5 = false
local r6 = false
local r7 = false
local r8 = false
local r9 = false
local r10 = false
local r11 = false
local r12 = false
function getRedstoneStateBottom()
local state = redstone.getInput("bottom")
if state == true then
  emergencyShutdown = "On"
else
  emergencyShutdown = "Off"
end
return state
end
function forceReactorStart()
r1 = false
r2 = false
r3 = false
r4 = false
r5 = false
r6 = false
r7 = false
r8 = false
r9 = false
r10 = false
r11 = false
r12 = false
reactorCoolintg = false
reactorRunning = true
redstone.setOutput("right", false)
screenMessage = "Reactor Started"
screenStatus = "Running"
sreenWarning = "Reactor forced to start"
reactorRunningTimer = os.startTimer(60)
end
function startReactor()
if r1 == true or r12 == true then
  screenWarning = "WARNING: Reactor appears to not have been properly cooled down."
  startCooldownPhase()
  update()
  return true
elseif emergencyShutdown == "On" then
  screenWarning = "WARNING: Tried to start while Emergency Shutdown is on."
  screenStatus = "Off"
  reactorRunning = false
  update()
  return true
end
redstone.setOutput("right", false)
screenMessage = ("Reactor started.")
reactorRunningTimer = os.startTimer(60)
reactorRunning = true
screenStatus = "Running"
update()
end
function stopReactor()
redstone.setOutput("right", true)
if reactorRunning == true then
  screenMessage = ("Reactor stopped.")
  reactorRunning = false
  screenStatus = "Off"
  update()
end
end
function ping()
if mainControlPingBack == false then
  mainControlConnected = "Not Connected"
end

if monitorPingBack == false then
  monitorConnected = "Not Connected"
end

if rednet.isOpen("top") == true then
  modemState = "On"
  rednet.send(mainControlId, "ping")
  mainControlPingBack = false

  rednet.send(monitorId, "ping")
  monitorPingBack = false
else
  modemState = "Off"
  keepRunning = "False"
end

if reactorRunning == true then
  screenStatus = "Running"
elseif reactorCooling == true then
  screenStatus = "Cooling"
else
  screenStatus = "Off"
end
update()
wScreen()
end
function getEvent()
local eName, eArg1, eArg2 = os.pullEvent()
return eName, eArg1, eArg2
end
function startCooldownPhase()
screenMessage = "Cooldown phase started."
reactorCooling = true
screenStatus = "Cooling"
c1 = false
c2 = false
c3 = false
c4 = false
reactorCoolingTimer = os.startTimer(60)
screenMessage = "Cooldown phase over in 3 minutes."
update()
end
function wScreen()
shell.run("clear")
spacerB = "				  "
spacerM = "		 "
print(spacerB, "Reactor Control")
print(" ")
print(" ")
print("Computer Id: ", computerId)
print("Version:", version)
print("Modem State:",modemState)
print("Control Connected:", mainControlConnected)
print("Monitor Connected:", monitorConnected)
print("Keep Running:", keepRunning)
print("Status:", screenStatus)
print("Emergency Shutdown:", emergencyShutdown)
print("Warning: ", screenWarning)
print("Message: ", screenMessage)
end
function update()
rednet.send(monitorId, "update")
rednet.send(monitorId, keepRunning)
rednet.send(monitorId, screenStatus)
rednet.send(monitorId, emergencyShutdown)
rednet.send(monitorId, screenWarning)
rednet.send(monitorId, screenMessage)
end
function standby()
while pRunning == true do
  local eName, eArg1, eArg2 = getEvent()
  if eName == "timer" then
   if reactorRunning == true then
	if r1 == false then
	 screenMessage = "Reactor cooldown phase in 11 minutes."
	 r1 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r2 == false then
	 screenMessage  = "Reactor cooldown phase in 10 minutes."
	 r2 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r3 == false then
	 screenMessage  = "Reactor cooldown phase in 9 minutes."
	 r3 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r4 == false then
	 screenMessage  = "Reactor cooldown phase in 8minutes."
	 r4 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r5 == false then
	 screenMessage  = "Reactor cooldown phase in 7 minutes."
	 r5 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r6 == false then
	 screenMessage  = "Reactor coldown phase in 6 minutes."
	 r6 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r7 == false then
	 screenMessage  = "Reactor coldown phase in 5 minutes."
	 r7 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r8 == false then
	 screenMessage = "Reactor coldown phase in 4 minutes."
	 r8 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r9 == false then
	 screenMessage = "Reactor coldown phase in 3 minutes."
	 r9 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r10 == false then
	 screenMessage = "Reactor cooldown phase in 2 minutes"
	 r10 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r11 == false then
	 screenMessage = "Reactor cooldown phase in 1 minute."
	 r11 = true
	 reactorRunningTimer = os.startTimer(60)
	 update()
	elseif r12 == false then
	 screenMessage = "Cooldown phase started."
	 r12 = true
	 stopReactor()
	 startCooldownPhase()
	end

   elseif reactorCooling == true then
	if c1 == false then
	 screenMessage = "Cooling phase over in 3 minutes."
	 c1 = true
	 r12 = false
	 r11= false
	 r10 = false
	 reactorCoolingTimer = os.startTimer(60)
	 update()
	elseif c2 == false then
	 screenMessage = "Cooling phase over in 2 minutes."
	 c2 = true
	 r9 = false
	 r8 = false
	 r7 = false
	 reactorCoolingTimer = os.startTimer(60)
	 update()
	elseif c3 == false then
	 screenMessage = "Cooling phase over in 1 minute."
	 c3 = true
	 r6 = false
	 r5 = false
	 r4 = false
	 reactorCoolingTimer = os.startTimer(60)
	 update()
	elseif c4 == false then
	 screenMessage = "Cooling phase complete."
	 c4 = true
	 r3 = false
	 r2 = false
	 r1 = false
	 reactorCooling = false
	 screenStatus = "Off"
	 update()
	 if keepRunning == "True" and emergencyShutdown == "Off" then
	  startReactor()
	 end
	
	end
   end

  elseif eName == "redstone" then
   if redstone.getInput("bottom") == true then
	emergencyShutdown = "On"
	keepRunning = "False"
	stopReactor()
	startCooldownPhase()
   elseif redstone.getInput("bottom") == false then
	emergencyShutdown = "Off"
  
   end
  
   if redstone.getInput("left") == true then
	ping()
   end

  elseif eName == "char" then
   print(eArg1)
  
   if eArg1 == "o" then
	print("Enter Override Command: ")
	local input = read()
	if input == "q" then
	 pRunning = false
	 rednet.close("top")
	 print("Quitting..")
	end
   end

  elseif eName == "rednet_message" then
   if eArg1 == mainControlId then
	if eArg2 == "ping" then
	 rednet.send(mainControlId, "pingBack")
  
	elseif eArg2 == "pingBack" then
	 mainControlConnected = "Connected"
	 wScreen()
	 mainControlPingBack = true
	
	elseif eArg2 == "Start Reactor" then
	 startReactor()
	 rednet.send(mainControlId, "Reactor Started")
  
	elseif eArg2 == "Force Reactor Start" then
	 forceReactorStart()
	 rednet.send(mainControlId, "Reactor Forced to start.")
	
	elseif eArg2 == "Stop Reactor" then
	 stopReactor()
	 keepRunning = "False"
	 startCooldownPhase()
	 rednet.send(mainControlId, "Reactor Stopped. Starting cooldown phase.")
	
	elseif eArg2 == "Run Reactor" then
	 keepRunning = "True"
	 startReactor()
	 rednet.send(mainControlId, "Reactor main sequence started.")
	
	elseif eArg2 == "Keep Running" then
	 if reactorRunning == true then
	  keepRunning = "True"
	  rednet.send(mainControlId, "Main sequence initiated.")
	 elseif reactorCooling == true then
	  keepRunning = "True"
	  rednet.send(mainControlId, "Main sequence will start after cooldown phase is complete.")
	
	 else
	  rednet.send(mainControlId, "Reactor is not active. Send command to start sequence if you wish.")
	 end
	else
	 print(eArg2)
	 rednet.send(mainControlId, "That is not a valid command.")
	end
   elseif eArg1 == monitorId then
	if eArg2 == "pingBack" then
	 monitorConnected = "Connected"
	 monitorPingBack = true
	elseif eArg2 == "ping" then
	 rednet.send(monitorId, "pingBack")
	end
   end
  end
end
end

redstone.setOutput("right", true)
getRedstoneStateBottom()
rednet.open("top")
ping()
pRunning = true
standby()

Reactor Control Monitor
———————
Spoiler

-- Reactor Control Monitor v0.8b
local computerId = 4
local monitorPingTimer
local reactorControlId = 0
local reactorMonitorVersion = "v0.6a"
local modemState = "Off"
local updating = "Not Updating"
local reactorControlConnectedToMonitor = "Not Connected"
local reactorKeepRunning = ""
local reactorEmergencyShutdown = ""
local reactorStatus = ""
local reactorWarningMessage = ""
local reactorMessage = ""
local pRunning = true
local reactorControlPingBack
function writeScreen()
shell.run("clear")
print("   Reactor Control Monitor")
print(" ")
print(" ")
print("Id: ", computerId)
print("Version: ", reactorMonitorVersion)
print("Modem State: ", modemState)
print("")
print("Control: ", reactorControlConnectedToMonitor)
print("Keep Running: ", reactorKeepRunning)
print("E. Shutdown: ", reactorEmergencyShutdown)
print("Status: ", reactorStatus)
print("Warning Msg: ", reactorWarningMessage)
print("Message: ", reactorMessage)
end
function ping()
if rednet.isOpen("right") == true then
  modemState = "On"
  if reactorControlPingBack == false then
   reactorControlConnectedToMonitor = "Not Connected"
  elseif reactorControlPingBack == true then
   reactorControlConnectedToMonitor = "Connected"

  end

  if reactorControlId ~= false then
   rednet.send(reactorControlId, "ping")
   reactorControlPingBack = false
  else
   reactorControlConnectedToMonitor = "Id not set"
  end
else
  shell.run("clear")
  print("Modem is off or missing..")
end
monitorPingTimer = os.startTimer(10)
end
function standby()
while pRunning == true do
  local eName, eArg1, eArg2 = os.pullEvent()
  if eName == "rednet_message" then
   if eArg1 == reactorControlId then
	if eArg2 == "pingBack" then
	 reactorControlPingBack = true
	
	elseif eArg2 == "update" then
	 local eArg1, eArg2 = rednet.receive(2)
	 if eArg2 == "True" or eArg2 == "False" then
	  reactorKeepRunning = eArg2
	 end
	
	 local eArg1, eArg2 = rednet.receive(2)
	 if eArg2 == "Off" or eArg2 == "Running" or eArg2 == "False" then
	  reactorStatus = eArg2
	 end
	
	 local eArg1, eArg2 = rednet.receive(2)
	 if eArg2 == "Off" or eArg2 == "On" then
	  reactorEmergencyShutdown = eArg2
	 end
	
	 local eArg1, eArg2 = rednet.receive(2)
	 if eArg2 ~= "pingBack" then
	  reactorWarningMessage = eArg2
	 end
	
	 local eArg1, eArg2 = rednet.receive(2)
	 if eArg2 ~= "pingBack" then
	  reactorMessage = eArg2
	 end
	 writeScreen()
  
	elseif eArg2 == "ping" then
	 rednet.send(reactorControlId, "pingBack")
	end
   end

  elseif eName == "timer" then
   ping()
   writeScreen()
  end
end
end
pRunning = true
rednet.open("right")
print("Reactor Control Monitor Initializing..")
monitorPingTimer = os.startTimer(3)
standby()
OmegaVest #2
Posted 26 April 2012 - 07:24 PM
AHH! Spoiler tags, please.

Also, a few notes while I look at the code: When you have a "var == true", you can usually just use "var".


Annnnnd. . . doen reading. Can't tell without putting this in my comm. test world, but it looks like the if statements are firing out of sequence. I can't tell why though.

Also, does the flickering affect anything else? Or is is, as far as you can tell, just an update issue?

Lastly: TABLES! Make a table, reduce variable spam. Might help.

Other than that, my rather noobish eyes see nothing else until I can put it to my Cable Array. And mod the code to use said cables.
Ceirenthir #3
Posted 26 April 2012 - 07:41 PM
AHH! Spoiler tags, please.

Also, a few notes while I look at the code: When you have a "var == true", you can usually just use "var".


Annnnnd. . . doen reading. Can't tell without putting this in my comm. test world, but it looks like the if statements are firing out of sequence. I can't tell why though.

Also, does the flickering affect anything else? Or is is, as far as you can tell, just an update issue?

Lastly: TABLES! Make a table, reduce variable spam. Might help.

Other than that, my rather noobish eyes see nothing else until I can put it to my Cable Array. And mod the code to use said cables.

Sorry, I don't really frequent forums, didn't occur to me to use spoiler tags. Took me a mintue to figure out how to do that too. :)/>/>

Anyways as for the "variable spam", you mentioned tables, which I have no idea what that is. I quickly googled it and it seems to be like an array though?

As for the executing out of sequence, yes, that does appear to be what they're doing, the conditions that is. It does only seem to affect the display, but from messing with the code for hours yesterday, the update appeared to have been updating FAR faster than it should have been. It doesn't seem to do it in this version of the program though.

The programs have gotten very messy and possibly even more buggy because of me trying to fix this damn error, by me just trying random ass stuff.
OmegaVest #4
Posted 26 April 2012 - 08:51 PM
Hmm. Okay, tables and arrays are. . . more or less the same thing in lua. That being, an array is just a table whose indices are numbers instead of random crap. However, tables can have whatever you want as indices. So, you can have

local table = {}
table["reactorWarning"] = ""
table["reactorMessage"] = ""
and so on. I don't know how much that will actually change, though, but makes memory a little cleaner (if I can remember correctly). As I said, I will have to run this through my comm. test world. And watch all the flashing colors, beeps and lag spam that entails, to see if anything is coming up other than an actual bug. Also, will look at making the monitor run statuses on an actual monitor and having the terminal output debug information in a box. Don't know if I will get to that part, though. Actually, doubtful if I will. But, maybe can use debug terminal. . .no. . . ah, will find some way.




Umm. To expedite some things, have you thought about having each terminal dump to a further terminal code when each 'if' fires? Or, in this case, have them send a line when these specific lines fire. May help find cause, may help find cur- I mean fix.
Ceirenthir #5
Posted 26 April 2012 - 10:11 PM
So they're kinda like dictionaries in python then, awesome. I'll use those from now on.

And yeah I thought about trying to print some lines when each condition executes, I'll try that later on, kinda busy right now. Just need to remove the writeScreen() calls because that function clears the display and redraws the updated vars to the screen.

This actually does run on a monitor by the way, I have that in the startup program.

By the way, you mentioned running on a monitor AND outputting info onto the terminal at the same time. How do you do that?
OmegaVest #6
Posted 26 April 2012 - 10:23 PM
To use both displays, use term.redirect(varMonitor). You will need to wrap the monitor though. Check the peripheral api, it will help a little. Just, whenever you bounce back, use term.restore. It will get a bit hairy doing all this, but if you use functions on the monitors and have them restore at the end, it should work.

So, have at the top of your program

screen = peripheral.wrap(monitorSide)
and at the front of your status printer

term.redirect(screen)
and then end that function with

term.restore()
it should just leave whatever you are printing on the monitor, and use the terminal for everything else. Just make sure you have measurements for everything, yada yada, standard logistical and warning BS, yada. Once you've written a 1000-line program, this should be fairly unnecessary, but I find myself typing it anyway. I guess that is a product of time, as it's about quitting time where I am, and I'm really tired.
louisrocks40 #7
Posted 27 April 2012 - 02:11 AM
reactorCoolintg = false

–In function forceReactorStart is spelled wrong. Get rid of the "t"