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

Power program (resonant cells)

Started by Dustmuz, 03 October 2014 - 06:04 PM
Dustmuz #1
Posted 03 October 2014 - 08:04 PM
Hello there again, im back once more :D/>

now, i have thrown myself into making yet another power program, this time, is should also manage the powerflow a little :D/>

i have 3 resonant cells, connected with modems.

code so far
Spoilermon = peripheral.wrap("top")
modem = peripheral.wrap("left")

modem.open(1)

mon.setBackgroundColor(colors.Blue)
mon.clear()
mon.setCursorPos(1,1)

cell1 = peripheral.wrap("cofh_thermalexpansion_energycell_237")
cell2 = peripheral.wrap("cofh_thermalexpansion_energycell_238")
cell3 = peripheral.wrap("cofh_thermalexpansion_energycell_239")

cellp1 = cell1.getEnergyStored("front")
cellp2 = cell2.getEnergyStored("front")
cellp3 = cell3.getEnergyStored("front")

cellmax1 = cell1.getMaxEnergyStored("front")
cellmax2 = cell2.getMaxEnergyStored("front")
cellmax3 = cell3.getMaxEnergyStored("front")

celltotal =

at the end, where i have celltotal and cellmax
could i do the following:
Math("cellp1 + cellp2 + cellp3")
for both energy stored and energy max

so the celltotal line would look like this
celltotal = Math(cellp1 + cellp2 + cellp3)

after this then do
cellpercent = math((celltotal / cellmax) * 100)

and later in an IF loop
make something like this

If cellpercent <= 10 then
rs.setOutput("right",true)
sleep(120)
else
rs.setOutput("right",false)
end



just want to know if i can do it like this, or get directions on how to do :D/>

Thanks alot in advance :)/>
Edited on 03 October 2014 - 06:07 PM
Bomb Bloke #2
Posted 04 October 2014 - 02:29 AM
You don't need to wrap or open the modem in order to use it to work with remote peripherals. The modem's functions are only of use for communicating with other modems attached to remote computers.

You don't need to call functions to perform basic mathematical operations (and in the case you're suggesting, "Math" isn't a valid function anyway). This sort of thing is perfectly valid:

celltotal = cellp1 + cellp2 + cellp3
cellpercent = celltotal / cellmax * 100

You might use a function to perform specific operations, such as rounding down to the nearest integer:

cellpercent = math.floor(celltotal / cellmax * 100)

See these notes on the math library for more details.

Once you've got your variables set, you can indeed use them within "if" statements.
Edited on 04 October 2014 - 12:30 AM
Dustmuz #3
Posted 05 October 2014 - 01:27 PM
You don't need to wrap or open the modem in order to use it to work with remote peripherals. The modem's functions are only of use for communicating with other modems attached to remote computers.

You don't need to call functions to perform basic mathematical operations (and in the case you're suggesting, "Math" isn't a valid function anyway). This sort of thing is perfectly valid:

celltotal = cellp1 + cellp2 + cellp3
cellpercent = celltotal / cellmax * 100

You might use a function to perform specific operations, such as rounding down to the nearest integer:

cellpercent = math.floor(celltotal / cellmax * 100)

See these notes on the math library for more details.

Once you've got your variables set, you can indeed use them within "if" statements.


okay, thanks for that knowlegde :)/>

is there a "page" that goes through all the different "functions" that can be used. i hardly find any when searching google (might just be me, who is using the wrong search parameters)
and thanks for the link, keep saving those, so i can pull them up, whenever i need them :Ds


so once more, thanks alot for the help here :)/>
love this great forum and community, very helpfull i must say :)/>
Dustmuz #4
Posted 05 October 2014 - 02:15 PM
now i worked a little with it, and got the it to work

but ran into a "small problem"

if i put my monitor updating, in a while loop, the redstone signal isnt sent when it drops below 10%
how do i get about that??

can i put the if statement, into a function and then call the function in the while loop.
would that work?

code so far
Spoilermon = peripheral.wrap("top")

mon.setBackgroundColor(colors.Blue)
mon.clear()

cell1 = peripheral.wrap("cofh_thermalexpansion_energycell_237")
cell2 = peripheral.wrap("cofh_thermalexpansion_energycell_238")
cell3 = peripheral.wrap("cofh_thermalexpansion_energycell_239")

cellp1 = cell1.getEnergyStored("front")
cellp2 = cell2.getEnergyStored("front")
cellp3 = cell3.getEnergyStored("front")

celltotal = cellp1 + cellp2 + cellp3

cellmax1 = cell1.getMaxEnergyStored("front")
cellmax2 = cell2.getMaxEnergyStored("front")
cellmax3 = cell3.getMaxEnergyStored("front")

cellmax = cellmax1 + cellmax2 + cellmax3

cellpercent = math.floor(celltotal / cellmax * 100)


if cellpercent < 10 then
rs.setOutput("right",true)
sleep(120)
else
rs.setOutput("right",false)
end

while true do
mon.clear()
mon.setCursorPos(1,1)
mon.write("Energy Control")
mon.setCursorPos(1,3)
mon.write("Energy stored: "..cellpercent)
sleep(2)
end


and on a sidenote..
how do you make those white "boxes" for the code??
so that it keep its indentation
Edited on 05 October 2014 - 12:22 PM
KingofGamesYami #5
Posted 05 October 2014 - 03:12 PM

The code
Without spaces

The code
Dustmuz #6
Posted 05 October 2014 - 03:14 PM

The code
Without spaces

The code


Thanks :D/>
then i know that for next time..
Bomb Bloke #7
Posted 06 October 2014 - 03:15 AM
is there a "page" that goes through all the different "functions" that can be used.

"A" page? Not likely. Lua has loads of functions, too many for a page!

It's usually best to hunt down something in regards to the sort of functions you want - for example, mathematical functions are in the "math" library, text manipulation functions are in the "string" library, and so on.

If you haven't spotted it, this page in the ComputerCraft wiki is your hub for all commands which're ComputerCraft-specific.

if i put my monitor updating, in a while loop, the redstone signal isnt sent when it drops below 10%
how do i get about that??

can i put the if statement, into a function and then call the function in the while loop.
would that work?

Yes, that'd do the trick. Personally I'd just stick the "if" block into the "while" loop. Bear in mind that to get updated data on the cell levels, you need to continuously call the getEnergyStored() functions.

You would be better of having the loop also check for when the energy total hits 90% or something before turning the engines off, rather than just sleeping for two minutes while recharging.
Dustmuz #8
Posted 06 October 2014 - 06:20 AM
is there a "page" that goes through all the different "functions" that can be used.

"A" page? Not likely. Lua has loads of functions, too many for a page!

It's usually best to hunt down something in regards to the sort of functions you want - for example, mathematical functions are in the "math" library, text manipulation functions are in the "string" library, and so on.

If you haven't spotted it, this page in the ComputerCraft wiki is your hub for all commands which're ComputerCraft-specific.

if i put my monitor updating, in a while loop, the redstone signal isnt sent when it drops below 10%
how do i get about that??

can i put the if statement, into a function and then call the function in the while loop.
would that work?

Yes, that'd do the trick. Personally I'd just stick the "if" block into the "while" loop. Bear in mind that to get updated data on the cell levels, you need to continuously call the getEnergyStored() functions.

You would be better of having the loop also check for when the energy total hits 90% or something before turning the engines off, rather than just sleeping for two minutes while recharging.

ohh thanks for that page, another one saved :D/>
im getting quite alot of saved pages from this forum..

so something like this might be what i should do:


while true do
 if cellpercent < 10 then
  rs.setOutput("right",true)
   if cellpercent > 90 then
    rs.setOutput("right", false
   end
  else
   mon.clearLine()
   mon.setCursorPos(1,3)
   mon.write("Energy level: "..cellpercent)
  end
end


would it be something like that i could use?
Edited on 06 October 2014 - 04:23 AM
Dragon53535 #9
Posted 06 October 2014 - 06:42 AM
No, because if the first is true, then the second never will be. You would need

while true do
  if cellpercent < 10 then
	rs.setOutput("right",true)
  elseif cellpercent > 90 then
	rs.setOutput("right",false) --#Btw you forgot the ) for this line :P/>/>
  end
  mon.clearLine() --#This happens no matter the outcome of the above if statement
  mon.setCursorPos(1,3)
  mon.write("Energy level: "..cellpercent)
end
The way you have it is, IF the cell has < 10 % energy then it turns it on, and at the same time if the cell has < 10% and > 90% then it turns it off.
Edited on 06 October 2014 - 04:43 AM
Dustmuz #10
Posted 06 October 2014 - 06:46 AM
No, because if the first is true, then the second never will be. You would need

while true do
  if cellpercent < 10 then
	rs.setOutput("right",true)
  elseif cellpercent > 90 then
	rs.setOutput("right",false) --#Btw you forgot the ) for this line :P/>/>/>
  end
  mon.clearLine() --#This happens no matter the outcome of the above if statement
  mon.setCursorPos(1,3)
  mon.write("Energy level: "..cellpercent)
end
The way you have it is, IF the cell has < 10 % energy then it turns it on, and at the same time if the cell has < 10% and > 90% then it turns it off.


arhh :D/> learned something new once again :D/>
thanks alot yet another time :)/>
Dragon53535 #11
Posted 06 October 2014 - 06:51 AM
Quick thing, i just realized you're getting the cellpercent only once ever and then comparing that to the rest. You would need to add a copy of your whatever cellpercent is. such as

local cellpercent = getEnergyWhatever --#Lets say at the time this runs it's at 5
while true do
  if cellpercent > 10 then --#If 5 is greater than 10, which it never will be and since we've only set the value once, it doesn't auto update, so this cellpercent will always be 5
  end
end
You would need to do

local cellpercent = getMeEnergy --#Grab energy here, lets say it's 5 again
while true do
  cellpercent = getMeEnergy --#Updates it to whatever it is at the time.
  --[[#Lets say that your energy cell is getting more energy, well then whenever the while loop goes back to the top,
#it grabs the current energy again, so at some other point it could be 12 or whatever]]
Edited on 06 October 2014 - 05:06 AM
Dustmuz #12
Posted 06 October 2014 - 07:03 AM
Quick thing, i just realized you're getting the cellpercent only once ever and then comparing that to the rest. You would need to add a copy of your whatever cellpercent is. such as

local cellpercent = getEnergyWhatever --#Lets say at the time this runs it's at 5
while true do
  if cellpercent > 10 then --#If 5 is greater than 10, which it never will be and since we've only set the value once, it doesn't auto update, so this cellpercent will always be 5
  end
end
You would need to do

local cellpercent = getMeEnergy --#Grab energy here, lets say it's 5 again
while true do
  cellpercent = getMeEnergy --#Updates it to whatever it is at the time.
  --[[#Lets say that your energy cell is getting more energy, well then whenever the while loop goes back to the top,
#it grabs the current energy again, so at some other point it could be 12 or whatever]]

if i get you right, i should actually put this into the while loop

cellp1 = cell1.getEnergyStored("front")
cellp2 = cell2.getEnergyStored("front")
cellp3 = cell3.getEnergyStored("front")

celltotal = cellp1 + cellp2 + cellp3

cellmax1 = cell1.getMaxEnergyStored("front")
cellmax2 = cell2.getMaxEnergyStored("front")
cellmax3 = cell3.getMaxEnergyStored("front")

cellmax = cellmax1 + cellmax2 + cellmax3

cellpercent = math.floor(celltotal / cellmax * 100)
Dragon53535 #13
Posted 06 October 2014 - 07:07 AM
Just read your actual code, and you could easily turn your cellpercent calculations into a function like so.

local function getPercent() --#Declares that this is a new function to make.
  local cellp1 = cell1.getEnergyStored("front")
  local cellp2 = cell2.getEnergyStored("front")
  local cellp3 = cell3.getEnergyStored("front")
  local celltotal = cellp1 + cellp2 + cellp3
  local cellmax1 = cell1.getMaxEnergyStored("front")
  local cellmax2 = cell2.getMaxEnergyStored("front")
  local cellmax3 = cell3.getMaxEnergyStored("front")
  local cellmax = cellmax1 + cellmax2 + cellmax3
  return math.floor(celltotal / cellmax * 100) --#Says hey, if you use this for anything, this is a value that this will be.
end --#Ends the function
This would cause it so that whenever you did

local cellpercent = getPercent() --#This calls the function and makes the code run that's inside the function.
It would grab the current energy percent and store it in the cellpercent variable.

Coppied this from an edit on my previous post :P/>
Edited on 06 October 2014 - 05:10 AM
Dustmuz #14
Posted 06 October 2014 - 07:09 AM
Just read your actual code, and you could easily turn your cellpercent calculations into a function like so.

local function getPercent()
  local cellp1 = cell1.getEnergyStored("front")
  local cellp2 = cell2.getEnergyStored("front")
  local cellp3 = cell3.getEnergyStored("front")
  local celltotal = cellp1 + cellp2 + cellp3
  local cellmax1 = cell1.getMaxEnergyStored("front")
  local cellmax2 = cell2.getMaxEnergyStored("front")
  local cellmax3 = cell3.getMaxEnergyStored("front")
  localcellmax = cellmax1 + cellmax2 + cellmax3
  return math.floor(celltotal / cellmax * 100)
end
This would cause it so that whenever you did

local cellpercent = getPercent()
It would grab the current energy percent and store it in the cellpercent variable.

Coppied this from an edit on my previous post :P/>

ohhhh.. im gonna "steal" that function right there.. *tries to run away with it*
Dragon53535 #15
Posted 06 October 2014 - 07:12 AM
Go right ahead :P/> it's good to use. (it wasn't earlier there was a space missing somewhere :P/>) I would look into a tutorial about variable scope so that you don't clutter up the global space with unneeded variables and functions. Here.
Dustmuz #16
Posted 06 October 2014 - 07:13 AM
Go right ahead :P/> it's good to use. (it wasn't earlier there was a space missing somewhere :P/>) I would look into a tutorial about variable scope so that you don't clutter up the global space with unneeded variables and functions. Here.

yay another link :D/> like those :P/>
got more to share?? hehe

thanks alot for the help here..
i usually just clutter up everything, as i then have it easier to see where i did something wrong :P/> hehe
Dragon53535 #17
Posted 06 October 2014 - 07:17 AM
I've got a few personal links i like to keep that other's probably wouldn't want :P/> However for future programs, you could implement file saving. Here. Have another link
Edited on 06 October 2014 - 05:17 AM
Dustmuz #18
Posted 06 October 2014 - 07:31 AM
I've got a few personal links i like to keep that other's probably wouldn't want :P/> However for future programs, you could implement file saving. Here. Have another link

ohhh.. this looks fancy :P/>
thanks for that link :D/>
Dragon53535 #19
Posted 06 October 2014 - 07:34 AM
It's great to use for config files and such.