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

solar generator loop doubts

Started by hunter239833, 20 March 2014 - 12:15 AM
hunter239833 #1
Posted 20 March 2014 - 01:15 AM
hi, i am playing the direwolf20 ftb pack, and i wanted to control a solar generator with a computer. the generator has two modes: charging and releasing, and it switches between them on a redstone signal. i wrote a program that i hope will take care of this for me, but i am unsure if it will run like i want it to. i want it to check if the energy is full (500000) then set the redstone output on the generators side to true, then print the energy and wait a bit before trying agian. then when it is fully drained i want it to turn off the redstone output and do the same "print, wait" setup as the other loop. my problem is i dont know if it will keep looping like i want it to, but i dont have the time to fully test this as it takes a long time, so i was wondering if you could take a look at it and tell me if it wold work and if you see any glaring errors, thanks!
also please tell me how my indenting is, i think i have it right finally


local m = peripheral.wrap("top")
local solar = peripheral.wrap("right")
while true do
local energy = solar.getEnergyStored("1")
  if energy == 500000 then
	while energy > 0 do
	  m.write(solar.getEnergyStored("1"))
	  sleep(5)
	  redstone.setOutput("right", true)
	end
  else
	m.write(solar.getEnergyStored("1"))
	redstone.setOutput("right", false)
	sleep(5)
  end
end

edit: also sorry if my terminology is incorrect anywhere, im mostly self taught so i may have picked up the wrong terms here or there.
Edited on 20 March 2014 - 12:36 AM
Bomb Bloke #2
Posted 20 March 2014 - 01:39 AM
When you do this:

local energy = solar.getEnergyStored("1")

… the "energy" variable gets set to whatever the solar's current energy level is (assuming you've performed that peripheral call correctly). Say the level was 500000 at the time that line was processed - from that point on, the "energy" variable will be equal to 500000. It won't change as the solar's energy level changes.

Because your script always needs to know what the "current" energy level is, I would ditch the "energy" variable altogether and just use "solar.getEnergyStored("1")" in its place.
hunter239833 #3
Posted 20 March 2014 - 01:43 AM
yea, i noticed that, and i am not sure why i left it in there at all, you can see u use the whole "solar.get…) command later on. do you think that it would work if i completely change it? also comments on the indenting? just want to make sure ive got it right
hunter239833 #4
Posted 20 March 2014 - 01:55 AM
it seems to be working, thanks for helping me find my dumb lazy mistake, lol
Bomb Bloke #5
Posted 20 March 2014 - 02:23 AM
Well done. :)/>

Other then for the line I pointed out, your indentation looks ok.
hunter239833 #6
Posted 20 March 2014 - 02:25 AM
Thanks! And it works nicely!
tommydrum #7
Posted 04 June 2014 - 02:16 AM
So.. Hunter, may I ask you to post your finalized program? I really want to use your program, and use those solars to power my base
flaghacker #8
Posted 04 June 2014 - 06:22 AM
And why would you want to turn solar generators off? To spare sunlight?
Lyqyd #9
Posted 04 June 2014 - 03:53 PM
Did you read the first post in the topic? Within the first two lines, it explains why you'd need something like this.
flaghacker #10
Posted 04 June 2014 - 08:48 PM
Sorry, my mistake.