r/ComputerCraft • • Jul 29 '14

Help with a program.

Hi, have posted here before but that was a while ago. Basically haven't used CC in a while and am having a very large brain fart. I'm looking to have a computer read the amount of energy in a redstone energy cell and if it hits like a 75% threshold turn my power gen on. And when it gets full shut it off. I know it would be an if then statement but I'm not sure how to write it :(

Any help would be great thanks

1 Upvotes

6 comments sorted by

View all comments

3

u/sjonky Jul 31 '14
energycell = peripheral.wrap("right") -- change side
amount = energycell.getEnergyStored("unknown")
capacity = energycell.getMaxEnergyStored("unknown")
energyPercent = (amount/capacity)*100
if energyPercent > 75 then
    -- turn power gen off
else
    -- turn power gen on
end

If you want to check out some of this in a video format, ive made some tutorials on openPeripheral stuff, https://www.youtube.com/watch?v=MAkKKDqYM3o in specific covers openPeripheral and energy storage stuff

1

u/Warmasher Jul 31 '14

ok, i feel like a noob about 3 months ago i wrote a program for big reactors. basically it was a if then statement saying if the inside reactor power hit 5,000,000 rf turn on and then turn off when it hit 9,500,000 rf. i want to do something similar with my energy cells but i have NO idea how to write it for some reason. Your videos where helpful but i am so lost right now :(

2

u/O_Reezy Aug 01 '14

For a short explanation:

OpenPeripherals gives you functions that return information about objects. getEnergyStored() and getMaxEnergyStored() return the amount of energy currently in a cell and the max it can hold.

Now to get a percent you just divide the current energy by the max energy.

local cell = peripheral.wrap("side or name")
--This line wraps the peripheral to a variable

local current = cell.getEnergyStored("")
--This stores the current amount of energy in the cell

local max = cell.getMaxEnergyStored("")
--Gets the max possible energy that can be stored

if current/max >= .75 then
--If the current energy is greater than 75% of the total
  --This is where you code to turn off energy generation
else
  --turn power on if current energy is less than 75%
end

1

u/Warmasher Aug 01 '14

i actually figured it out about 10 mins after i replyed back lol, thank you again for the help, and the youtube videos are great!