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

Automatic running programs

Started by xTheChosen0ne, 18 June 2015 - 06:07 PM
xTheChosen0ne #1
Posted 18 June 2015 - 08:07 PM
Hey guys. I made a program, which sends per RedNet a message to all computers when a mfe have less than 10000 EU. Now my question: What i have to do, that the programs (sending message and receiving message) work automatically?
Dog #2
Posted 18 June 2015 - 08:38 PM
name the programs startup on each computer and they will automatically start running when the computers boot
xTheChosen0ne #3
Posted 18 June 2015 - 09:15 PM
name the programs startup on each computer and they will automatically start running when the computers boot
This dont works, because it should use a loop. so its needed to reboot everytime. but thats the problem: if i make rs.setOutput("left", true) for example, the redstone signal goes off in the time, when the pc is rebooting
Dog #4
Posted 18 June 2015 - 09:31 PM
This dont works…
Why does naming the programs 'startup' not work? Are they not starting automatically?

because it should use a loop. so its needed to reboot everytime. but thats the problem: if i make rs.setOutput("left", true) for example, the redstone signal goes off in the time, when the pc is rebooting
If your program requires a loop, then that's what you should use. You shouldn't need to reboot each time. It sounds like the problem is how you've structured your code. Post your code and I (or somebody) will help you rework the code to use a loop instead of requiring a reboot each time.
Edited on 18 June 2015 - 07:37 PM
xTheChosen0ne #5
Posted 18 June 2015 - 10:13 PM
This dont works…
Why does naming the programs 'startup' not work? Are they not starting automatically?

because it should use a loop. so its needed to reboot everytime. but thats the problem: if i make rs.setOutput("left", true) for example, the redstone signal goes off in the time, when the pc is rebooting
If your program requires a loop, then that's what you should use. You shouldn't need to reboot each time. It sounds like the problem is how you've structured your code. Post your code and I (or somebody) will help you rework the code to use a loop instead of requiring a reboot each time.

When i simply make "shell.run(program name)" at the end of the loop, it works fine first, but after a few minutes there is a java problem. I "solved" the problem like this, with reboot:
Computer at MFE:

os.loadAPI("ocs/apis/sensor")
mySensor = sensor.wrap("top")
position = "0,0,-1"
details = mySensor.getTargetDetails(position)
kapazitaet = details.Capacity
stand = details.Stored
grenze = 30000
schleife = 1
while schleife > 0 do
  rednet.open("right")
  if stand <= grenze then
	rednet. broadcast("alarm")
  elseif stand > grenze then
	rednet.broadcast("ok")
  end
sleep(1)
shell.run("reboot")
end

My Computer, which receive the messages and react on:


rednet.open("bottom")
id, message = rednet.receive()
write ("Status:  ")
if id == 15 and message == "alarm" then
  rs.setOutput("back", true)
  print("Kritisch. Eigenversorgung aktiviert.")
end
if id == 15 and message == "ok" then
  rs.setOutput("back", false)
  print("OK")
end
sleep(3)
shell.run("reboot")

Both programs are made as startup
Edited on 18 June 2015 - 08:21 PM
Dog #6
Posted 18 June 2015 - 10:57 PM
OK, so what you need to do is encapsulate your 'repeating' code in an infinite loop for each program so you don't need to reboot. You have an infinite loop in your MFE program (while schleife > 0 do), but it's in the wrong place and not really helping.

Let's start with the MFE program. First, you don't need to specify a value and compare that value to create an infinite loop. Instead of this…

schleife = 1
while schleife > 0 do
  --# code...
end

You can just do this

while true do
  --# code
end

Next all the code that needs to repeat should be moved inside that loop. Think about which lines of code need to only be run once, and which ones need to repeat over and over. You want to put the non-repeating code above the loop and all the repeating code inside the loop. Try making those changes to your MFE program (we'll get to the second program after we finish the first) and post your results. We'll go from there.
Edited on 18 June 2015 - 09:01 PM
xTheChosen0ne #7
Posted 19 June 2015 - 09:53 AM
OK, so what you need to do is encapsulate your 'repeating' code in an infinite loop for each program so you don't need to reboot. You have an infinite loop in your MFE program (while schleife > 0 do), but it's in the wrong place and not really helping.

Let's start with the MFE program. First, you don't need to specify a value and compare that value to create an infinite loop. Instead of this…

schleife = 1
while schleife > 0 do
  --# code...
end

You can just do this

while true do
  --# code
end

Next all the code that needs to repeat should be moved inside that loop. Think about which lines of code need to only be run once, and which ones need to repeat over and over. You want to put the non-repeating code above the loop and all the repeating code inside the loop. Try making those changes to your MFE program (we'll get to the second program after we finish the first) and post your results. We'll go from there.

Well, first, it seemed to work. But there is following problem: He just test the status, when i activate the program. So, when the MFE has more than 30000 EU at the moment, when i start the program, it ALWAYS send "ok" (also, when he gets under 30000 meanwhile). And when the MFE has equal or less than 30000 EU at the moment, when i start the program, it ALWAYS send "alarm" (also, when he gets 30000+ meanwhile).
That's not what i want to reach. The loop itself seems to work.
My code:

os.loadAPI("ocs/apis/sensor")
mySensor = sensor.wrap("top")
position = "0,0,-1"
details = mySensor.getTargetDetails(position)
kapazitaet = details.Capacity
stand = details.Stored
grenze = 30000
rednet.open("right")
while true do
  rednet.open("right")
  if stand <= grenze then
	 rednet. broadcast("alarm")
	 sleep(1)
  end
  if stand > grenze then
	rednet.broadcast("ok")
	sleep(1)
  end
end
Edited on 19 June 2015 - 08:31 AM
xTheChosen0ne #8
Posted 19 June 2015 - 11:25 AM
I founded the problem! I started the infinite loop at the wrong place, so he just checked 1 time, how much EU was Stored. Sorry for beeing noob in LUA :D/> And i want to study IT… that will be fun lol.
However, thats the working code for the MFE-Computer now!:


os.loadAPI("ocs/apis/sensor")
mySensor = sensor.wrap("top")
position = "0,0,-1"
while true do
  details = mySensor.getTargetDetails(position)
  kapazitaet = details.Capacity
  stand = details.Stored
  grenze = 30000
  rednet.open("right")
  if stand <= grenze then
		 rednet. broadcast("alarm")
		 sleep(1)
  end
  if stand > grenze then
		rednet.broadcast("ok")
		sleep(1)
  end
end

How's about the 2nd program now? :)/>
Edited on 19 June 2015 - 10:00 AM
Dog #9
Posted 19 June 2015 - 03:49 PM
Great job! A couple of small changes: move rednet.open("right") above the loop - you only need to do that once. You could also move grenze = 300000 above the loop since the value never changes.

Now, the same applies to the second program. You don't want it to reboot, but you want it to run forever. So, think about which code needs to run once and which code needs to be in the loop. You'll use the same thing (while true do) you did last time.
xTheChosen0ne #10
Posted 19 June 2015 - 06:32 PM
Great job! A couple of small changes: move rednet.open("right") above the loop - you only need to do that once. You could also move grenze = 300000 above the loop since the value never changes.

Now, the same applies to the second program. You don't want it to reboot, but you want it to run forever. So, think about which code needs to run once and which code needs to be in the loop. You'll use the same thing (while true do) you did last time.

Alright, big thank you to you :D/> All is working like it should, now, thank you :)/>
Dog #11
Posted 19 June 2015 - 07:37 PM
You're very welcome - glad to help :)/>