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

How to make this send only once unless the server time is changed?

Started by gknova61, 13 November 2012 - 03:15 PM
gknova61 #1
Posted 13 November 2012 - 04:15 PM
Here is my code: http://pastebin.com/D3rTVXPn
All it is is a simple monitor for day/night changes and to update a frame door based on those changes. The only problems with it currently is:

1. It spams the hell outta rednet

2. I have a separate computer for manually opening/closing the door but it won't work because every like 0.1 second this code is
sending the door to close, so this separate computer can't open the door while the day night monitor is spamming the door to close. You can see why this would be a bit troublesome at times

3. Yes, I am aware that i can just remove the > and < signs to make it work but I use this on a server and my #1 concern is the admins changing the time all willinilly and the day/night handler not kicking in.

So all I'm looking for is a way to get this program to work the way it does, that being working even when the server time suddenly changing but not have it spam rednet. My general solution that I can't workout is to have it send to rednet only once if the conditional is true and not keep sending if it runs through the loop 0.1 sec later and find the same conditional true
dissy #2
Posted 13 November 2012 - 04:35 PM
Create two variables at the top, stateLast=0 and stateCur=0
In your code where it figures out the night/day, instead of firing the door signal, have it ust change stateCur
At the end, compare stateCur with stateLast and if they are different, set stateLast to stateCur and change the redstone signal then.

Edit: Check over these changes - http://pastebin.com/CDgJXPiy
I haven't tested it but hopefully the concept is at least clear.
Edited on 13 November 2012 - 03:44 PM
pruby #3
Posted 13 November 2012 - 04:59 PM
Untested suggestion - modelled as a state machine. Each state (daytime, nighttime) has an update function

Spoiler

daytime = {
  update = function()
	if MChours >= 18 and MCminutes >= 10 then
	  daytime.transitionNight()
	elseif MChours >= 0 and MChours < 6 then
	  daytime.transitionNight()
	end
  end,
  transitionNight = function()
	rednet.send(379,"Door-Close")
	currentState = nighttime
  end
}

nighttime = {
  update = function()
	if MChours >= 6 and MChours < 18 and MCminutes >= 10 then
	  nighttime.transitionDay()
	end
  end,
  transitionDay = function()
	 rednet.send(379,"Door-Open")
	 currentState = daytime
  end
}

currentState = daytime
while true do
  currentState.update()
  sleep(60.0)
end
gknova61 #4
Posted 15 November 2012 - 04:21 PM
The ^ has a problem with your code. It says attempt to call nil at line 29. Anyone else got ideas? :3
Grim Reaper #5
Posted 15 November 2012 - 04:50 PM
Here is an untested and rather lengthy solution, but you can clean it up if you'd like.
http://pastebin.com/jSSDaEyG
dissy #6
Posted 15 November 2012 - 04:54 PM
Strange, the version I posted worked for me, although i replaced the "rednet.send" with "term.write" to test it. With only 25 lines, I don't know why it would error at line 29
gknova61 #7
Posted 18 November 2012 - 11:43 AM
Here is an untested and rather lengthy solution, but you can clean it up if you'd like.
http://pastebin.com/jSSDaEyG

Works perfect, thanks! Only thing i changed in the code was the rednet open to one of the things in my api to search for a modem ( gbang.rednetOpen() )