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

Learning Basic Code, Redstone and Computers

Started by Nesbro, 14 May 2015 - 10:50 PM
Nesbro #1
Posted 15 May 2015 - 12:50 AM
Just wanted to know how to input in option into this code that would allow me to turn on the switch that says "hello" without needing the other switches off first. I would just like to learn how to put things in order and figure out functionality. Right now it works great but I want to be able to turn on the back and front switch at different times and it say "hello" instead of needing to turn the front and back on at the same time. http://pastebin.com/jCuTAUbL
KingofGamesYami #2
Posted 15 May 2015 - 01:01 AM
As I'm not exactly sure what you want to do, I think I'll comment your code as to what it is doing, and remove the parts that don't do anything.

Spoiler

local monitor = peripheral.wrap("top")
monitor.clear()
while true do --#forever do
  while rs.getInput("front") and rs.getInput("back") do --#I removed the ==true because comparing to true is redundant
    monitor.setTextScale(2) --#set the monitor scale
    monitor.setCursorPos(11,3) --#set the monitor cursor
    monitor.write("Hello") --#write hello
    sleep(5) --#do nothing for 5 seconds
    monitor.clear() --#clear the monitor
  end
  while rs.getInput("front") do --#removed the second parameter, as it is unnecessary and ignored.
    monitor.setTextScale(2) --#set the text scale
    monitor.setCursorPos(11,3) --#set the cursor pos
    monitor.write("Front") --#write front
    sleep(5) --#do nothing for 5 seconds
    monitor.clear() --#clear the monitor
  end
  while rs.getInput("back") do
    monitor.setTextScale(2)
    monitor.setCursorPos(11,3)
    monitor.write("Back")
    sleep(5)
    monitor.clear()
  end
  if not rs.getInput("front") then --#removed the second parameter ("back") as it was being ignored
    monitor.setTextScale(2)
    monitor.setCursorPos(9,3)
    monitor.write("No Signal")
    sleep (5)
    monitor.clear()  
  end
end
Nesbro #3
Posted 15 May 2015 - 01:27 AM
I figured I would be a bit hard to understand, I'm just playing around for fun to learn. Thanks for editing to help me understand, I'll just keep playing around with the code and learn more to figure out what i want.
Nesbro #4
Posted 15 May 2015 - 01:32 AM
How would i add it to where it detects redstone signal strength and determine what it says based on said signal strength?
KingofGamesYami #5
Posted 15 May 2015 - 01:36 AM
rs.getAnalogInput
Bomb Bloke #6
Posted 15 May 2015 - 01:43 AM
Sounds like you're wanting to ditch the cluster of "while" loops, and use "if" statements instead. Eg:

local monitor = peripheral.wrap("top")
monitor.setTextScale(2) --#No need to do this more than once.
monitor.clear()

while true do --#forever do
	if rs.getInput("front") and rs.getInput("back") then
		monitor.setCursorPos(11,3) --#set the monitor cursor
		monitor.write("Hello") --#write hello
	elseif rs.getInput("front") then
		monitor.setCursorPos(11,3) --#set the cursor pos
		monitor.write("Front") --#write front
	elseif rs.getInput("back") then
		monitor.setCursorPos(11,3)
		monitor.write("Back")
	else
		monitor.setCursorPos(9,3)
		monitor.write("No Signal")
	end
	
	os.pullEvent("redstone")  --# Wait until there's a change in the redstone input.
	monitor.clear() --#clear the monitor
end
Nesbro #7
Posted 15 May 2015 - 05:30 PM
Yea actually that looks about right! I'll test it here in a little bit. Thanks!
Nesbro #8
Posted 15 May 2015 - 08:50 PM
I tried your code hobby but it keeps saying " 'end' expected to close 'if' at line 6"

Why does "elseif" have to be together?
flaghacker #9
Posted 15 May 2015 - 08:58 PM
I tried your code hobby but it keeps saying " 'end' expected to close 'if' at line 6"

Why does "elseif" have to be together?

I'm guessing you forgot an end. Make sure you have the axact same code as he posted.

The "elseif" is one word because it is diffrent for "else if" in that it doesn't need an extra end.
Edited on 15 May 2015 - 07:00 PM
Bomb Bloke #10
Posted 15 May 2015 - 11:40 PM
… for example, accidentally typing "else if" instead of "elseif" would result in the exact error you got.