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

twin bank emergency power system

Started by kain184, 19 July 2014 - 05:36 AM
kain184 #1
Posted 19 July 2014 - 07:36 AM
hello all my first public program i have been working on it for a while. i want every one to see it. it does require a api download from http://hawkee.com/snippet/10100/ so do go get it. it will be three programs in total. you will have to change the computer values for the most part i would call this done for now. i will be improving it as i learn. i have only been at this for about a week. but i am happy with how far i have come. so i want to share

--########################### gives data to monitor
function mon(disp,x,y)
m= peripheral.wrap("back")
m.setCursorPos(x,y)
m.write(disp)
end
--############################clears monitor
function monc()
m=peripheral.wrap("back")
m.clear()
end
--############################send message to other computers
function send(idnum, message)
rednet.send(idnum, message)
end
--############################tests for message of other computers and exicutes response
function value( messageReq)

event,id, message = os.pullEvent("rednet_message")
print("current level")print(message)
local n= tonumber(message)


if id==3 and messageReq ==value then
  ProgressBar.SetCurValue("cap2",n)
  ProgressBar.DrawToPeripheral()
  return
  elseif id==6 and messageReq==value then
   ProgressBar.SetCurValue("cap1",n)
   ProgressBar.DrawToPeripheral()
   return
    elseif id== 3 and messageReq== max then
	 ProgressBar.SetMaxValue("cap2", n)
	 ProgressBar.DrawToPeripheral()
	 return
	  elseif id== 6 and messageReq== max then
	   ProgressBar.SetMaxValue("cap1", n)
	   ProgressBar.DrawToPeripheral()
	 return
  
else
print("requirement error")
print(messageReq)
os.reboot()

end

end
--############################ checks for modem open
function isOpen()
    rednet.open("right")
    com = rednet.isOpen("right")
   if com== true then
    return true
   else
    print("communication error")
    sleep(20)
    os.reboot()
   end
end
--############################ recieves message
function isRec(idnum, messageReq)
  event,id, message = os.pullEvent("rednet_message")
if id ==idnum and message== messageReq then
  print("message recieved")
else
  print("message error")
  sleep(20)
  os.reboot()
end
end
--####################### triggers value with and encodes messages to send to other computers
function cap()
 
   send(6, "max")
   value(max)
   send(6, "value")
   value(value)
   send(3, "max")
   value(max)
   send(3, "value")
   value(value)
 
end
--########################was trying to print max and min data with this code but could not find a way to implement without rewriting
s=tostring(perct)
le=tostring(level)
lx=tostring(levelm)
lm=tostring(lx, "/" ,le)
--#########################gets percentage and sets weather output to rest of the system is accepted or if main power should take over
function main()
p=peripheral.wrap("top")
level=p.getEnergyStored("top")
levelm=p.getMaxEnergyStored("top")
perc=level/levelm
perct=perc*100
print(perct)
if perct == 100 then
  redstone.setOutput("bottom", false)
  print("emergency disengaged")
  print("sleeping for 20 seconds to test if system needed")
  monc()
  ProgressBar.DrawToPeripheral()
  mon("emergency disengaged",1,1)
  mon(s,23,1)
  mon( "capacitor bank1",5,4)
  mon(" capacitor bank2" ,5,9)
 
  sleep(20)
elseif perct < 75 then
redstone.setOutput("bottom", true)
print("emergency engaged")
monc()
ProgressBar.DrawToPeripheral()
mon("emergency engaged",1,1)
mon(s,23,1)
mon( "capacitor bank1",5,4)
mon(" capacitor bank2" ,5,9)
else
redstone.setOutput("bottom", false)
print("emergency disengaged")
  monc()
ProgressBar.DrawToPeripheral()
mon("emergency disengaged",1,1)
mon(s,23,1)
mon( "capacitor bank1",5,4)
mon(" capacitor bank2" ,5,9)
end
end
--########################test if other computers are able to talk back
function test()
if isOpen()==true then
send(6, "test")
isRec(6, "test")
send(3, "test")
isRec(3, "test")
end
end
--#######################order of program and repeat
os.loadAPI("/rom/apis/ProgressBar")
ProgressBar.SetPeripheral("back")
ProgressBar.SetTable("cap1", 10, 1, 5,71,5)
ProgressBar.SetTable("cap2", 10, 1, 5,71,10)
ProgressBar.DrawToPeripheral()
print("running")
test()
while isOpen() == true do
cap()
sleep(2)
print("returned from cap")
main()
sleep(2)
print("finished main")
end
the main computer program. talks and
gets info from the others.

w= peripheral.wrap("left")
--############################send message
function send(idnum, message)
rednet.send(idnum, message)
end
--############################check to see if modem is connected
function isOpen()
    rednet.open("right")
    com = rednet.isOpen("right")
   if com== true then
    return true
   else
    print("communication error")
    sleep(20)
    os.reboot()
   end
end
--############################ checks for message
function isRec(idnum, messageReq)
  event,id, message = os.pullEvent("rednet_message")
if id ==idnum and message== messageReq then
  print("message recieved")
else
  print("message error")
  sleep(20)
  os.reboot()
end
end
--#######################check for power cell
function con()
local con=peripheral.isPresent("left")
if con == true then

print("peripheral connected")
else
print("powersource not found")
end
end
--#######################check if communication is good between main computer and capacitor computer
function test()
isOpen()
print("waiting for signal")
event,id, message = os.pullEvent("rednet_message")
if id ==8 and message== "test" then
  print("message recieved")
  print("sending response")
  send(id, message)
else
  print("message error")
  print(id)
  print(message)
  sleep(20)
  os.reboot()
end
end
--#######################wait for request then send requested info
function cap()
print("waiting on request")
event,id, message = os.pullEvent("rednet_message")
if id ==8 and message== "max" then --id of main computer
  print("message recieved")
  print("testing powersource")
 
  maxen= w.getMaxEnergyStored("left")
  print(maxen)
  print("sending response")
  send(id, maxen)
 
elseif id ==8 and message== "value" then

  print("message recieved")
  print("testing powersource")
  valueen= w.getEnergyStored("left")
 
  print("sending response")
  send(id, valueen)
else
print("message error")
print(id)
print(message)
return false
end
end
--#######################program order and repeat.
con()
test()
while isOpen()== true do
cap()
if cap== false then
os.reboot()
else
end
end
is the cap bank program. duplicate this for the second computer changing sides and computer values.


have fun and if you have anyway to improve this and make it better please tell me.
Mr. Bateman #2
Posted 19 July 2014 - 09:18 AM
You should put the programs in pastebin so it's easier to install.
"APIs and Utilities" technically is the right category, but you'd get more views if you ask a mod to move this to the normal "Programs" category.
kain184 #3
Posted 19 July 2014 - 04:29 PM
i see and i do have the pastebin links if anyone wants them i can put them up as well. but you do have to do a little editing of the program for each person to use them as i have no way of getting the remote id's without editing so i wanted it to be visible how everything was set up.
Mr. Bateman #4
Posted 19 July 2014 - 05:11 PM
You could save and load the IDs in a separate file (eg. 'ids.settings'), and the user can edit that file if they want to change the remote IDs used (which is easier than needing to edit the program)
kain184 #5
Posted 19 July 2014 - 05:39 PM
how do i do that. i have been looking for a way to load settings from and to a file. i have also wanted output log if you know of any way to do that.
Molinko #6
Posted 19 July 2014 - 07:35 PM
how do i do that. i have been looking for a way to load settings from and to a file. i have also wanted output log if you know of any way to do that.

try looking into the fs api, its all about file reading and writing.
kain184 #7
Posted 28 July 2014 - 09:17 PM
good to know i will do that
LeonTheMisfit #8
Posted 29 July 2014 - 02:54 AM
Also look at textutils. There's a bit in there about serializing and unserializing tables which works great for loading settings from a file.