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
gets info from the others.
have fun and if you have anyway to improve this and make it better please tell me.
--########################### 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 andgets 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.