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

Intruder Alert Monitor Program

Started by Raptus_, 20 December 2013 - 04:15 PM
Raptus_ #1
Posted 20 December 2013 - 05:15 PM
Okay, so I'm trying to make a computercraft monitor program that will display "INTRUDER ALERT" on the monitor when it receives a redstone signal from the back of the computer operating the monitor. When it isn't receiving a signal, it should display "No Emergency Signals". I'm not very good with building programs at all..the most I can do normally are basic ones like locking programs. All I want is some suggestion on how to make this work. I don't need it to be complicated at all.

Error I'm recieving:

(program name):2: bad argument: string expected, got nil



mon = peripheral.wrap("left")
if redstone.getInput(back) == true then
mon.clear()
mon.setCursorPos(2,2)
mon.setTextScale(1)
mon.write('INTRUDER ALERT")
mon.sleep(60)
else
mon.clear()
mon.setCursorPos(2,2)
mon.setTextScale(1)
mon.write("No Emergency Signals")
end
Bomb Bloke #2
Posted 20 December 2013 - 11:10 PM
Without quotes around it, the word "back" you've got on line two is treated as a variable. Since you've never assigned a value to the variable called "back", that means it's nil. You can't pass nil to the redstone.getInput() function.

That is to say, what you meant was 'if redstone.getInput("back") == true then'.

A few other points: "If" statements are already checking to see if your conditional resolves to "true" or not. Checking to see if the function returns "true" yourself is hence redundant. 'if redstone.getInput("back") then' is perfectly valid.

There is no 'mon.sleep(60)'. Just use 'sleep(60)'.

Calling os.pullEvent() causes the problem to wait until an event occurs, and you can specify certain types of events too - such as changes in redstone input. 'os.pullEvent("redstone")' will cause the program to wait until any of the redstone inputs attached to the computer changes state.
Raptus_ #3
Posted 21 December 2013 - 02:12 AM
Well adding the quotes in helped so thank you :)/>

But unfortunately, now I have a second problem after messing with the code because I was receiving a bunch of random errors which I managed to fix. This time I finally got the program to run, but it isn't running correctly. When I look at the monitor without the redstone signal, it says "No Emergency Signals" like it should. But then when I activate the redstone signal, it doesn't change, rebooting the computer doesn't fix this. :(/>

New Code:


mon = peripheral.wrap("left")
if redstone.getInput("back") == "true" then
mon.clear()
mon.setCursorPos(2,2)
mon.setTextScale(1)
mon.write("INTRUDER ALERT")
sleep(60)
else
mon.clear()
mon.setCursorPos(2,2)
mon.setTextScale(1)
mon.write("No Emergency Signals")
end
Lyqyd #4
Posted 21 December 2013 - 03:06 AM
The "true" you're comparing to shouldn't be in quotes. You also don't even need the == true part of that comparison, as Bomb Bloke mentioned above. If you want the code to check for changes in the redstone, you'll need it to loop:


local mon = peripheral.wrap("left")
while true do
	if redstone.getInput("back") then
		mon.clear()
		mon.setCursorPos(2,2)
		mon.setTextScale(1)
		mon.write("INTRUDER ALERT")
		sleep(60)
	else
		mon.clear()
		mon.setCursorPos(2,2)
		mon.setTextScale(1)
		mon.write("No Emergency Signals")
	end
	os.pullEvent("redstone")
end

Note that I also added os.pullEvent("redstone") at the end so it would wait for the redstone to actually change before continuing to loop. Of course, that means that if the redstone signal resets during that sleep(60), the notice might never change back.
Raptus_ #5
Posted 21 December 2013 - 05:42 PM
It works :D/> thank you to all who helped.