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

if /elseif problem

Started by matt2887, 05 June 2014 - 09:05 AM
matt2887 #1
Posted 05 June 2014 - 11:05 AM
I am having an issue with a program I'm writing.
The main point it's getting stuck is after the read. I can input anything I want but it never prints any of the 3 text options. Can someone tell me what i'm missing here?

Spoilerfloor1="1"
floor2="2"
floor3="3"
while true do
term.clear()
term.setCursorPos(1,1)
print("This is Floor 1")
print("Which floor would you like?")
print("Please enter 1, 2, or 3.")
floor = read()
if floor == floor1 then
print("You are on Floor 1!")
elseif floor == floor2 then
print("Going to Floor 2!")
elseif floor == floor3 then
print("Going to Floor 3")
end
end
Bomb Bloke #2
Posted 05 June 2014 - 01:31 PM
Code looks ok to me. Are you sure the file you saved this to is the file you're actually trying to run?

A common mistake is to compare strings to numbers, but you don't appear to be doing that here. I suspect you did do something like that, then saved your correction to a different file.
CometWolf #3
Posted 05 June 2014 - 02:00 PM
You're thinking too hard, Bomb :P/>

You've got a term.clear in your loop, which is called right after your print because it's in the loop. It then prints the "Your on the 1 floor" part again, and we're back to square one.
Saldor010 #4
Posted 05 June 2014 - 02:24 PM
To add on to CometWolf's insight, all you have to do to fix it, is just add a sleep call in between your If statement, and the end of the loop, like this.


if floor == floor1 then
print("You are on Floor 1!")
elseif floor == floor2 then
print("Going to Floor 2!")
elseif floor == floor3 then
print("Going to Floor 3")
end
sleep(1)
end

Then your loop should have enough time to print "Going to Floor…" before it is cleared. ;)/>
Edited on 05 June 2014 - 12:25 PM
sEi #5
Posted 05 June 2014 - 02:37 PM
or like this, just inserting a sleep if same floor and some break commands:

floor1="1"
floor2="2"
floor3="3"
while true do
  term.clear()
  term.setCursorPos(1,1)
  print("This is Floor 1")
  print("Which floor would you like?")
  print("Please enter 1, 2, or 3.")
  floor = read()
  if floor == floor1 then
    print("You are already on Floor 1!")
    sleep(2)
  elseif floor == floor2 then
	print("Going to Floor 2!")
	break
  elseif floor == floor3 then
	print("Going to Floor 3")
	break
  end
end

/sEi
Edited on 05 June 2014 - 12:41 PM
Lyqyd #6
Posted 05 June 2014 - 03:29 PM
It'd be helpful if you provided a good explanation of why you've arranged it that way, as well as what the breaks do. I'm not really certain that that is what OP is looking to do, though.
matt2887 #7
Posted 05 June 2014 - 06:07 PM
Oh wow. Thanks so much guys, apparently the code was running too fast for the eye to see it was working. The sleep and breaks helped. Thanks again :)/>
matt2887 #8
Posted 06 June 2014 - 01:58 AM
OK guys, new problem. :)/>

here's another part of code I'm writing that i'm having a problem with. I guess it's time I explain my plans for all the code. I am building a frame elevator. one computer on each floor(3 floors total) will control the elevator on that floor to is destination. So far I have working code for the floors, now I'm working on the coding of the 4th computer which controls the frame motors based on bundled inputs. When the elevator is on floor 1 it has a constant signal to grey, on floor two constant signal to cyan, and floor three is purple To move the elevator from floor 2 to floor 1, so far my coding below checks the back input. When I tell the computer on floor two that I want to go to floor 1 it outputs a yellow signal to back which feeds to the back of the controlling computer. Since the elevator is on floor 2 which emits a constant cyan signal, and the computer on floor 2 is adding a yellow signal to that, that makes cyan + yellow or 512+16. I have the controlling computer detect the bundled input and if it reaches 528 it executes the next line, which I want it to repeat until it reaches floor 1. To detect when it reaches floor 1 the bundled input will change to 256(light grey). Now here's where the breakdown is currently happening. When the bundled signal reaches 528 it kicks on the left output to orange, but it does not turn it off and on. I have a testing lamp attached to orange and it is continuously lit which means it's getting to that part and turns on the orange wire, but it is not turning it off so the frame motor can reset and turning it back on to make the frame motor move. The rest of the code works, because when i manually move the elevator down to the first floor it does detect a 256 bundled input and shuts off the orange wire. Anyone have any ideas as to why it will not pulse the bundled output to bit 2?

note: I have already tried moving the sleep in the function i created, however I cannot think of anything else to try.

--computer 5 control computer


--bit 1 is up, bit 2 is down


function d1()
	 rs.setBundledOutput("left", 2)
	 sleep(1)
	 rs.setBundledOutput("left", 0)
end

function u1()
	 rs.setBundledOutput("left", 1)
	 sleep(1)
	 rs.setBundledOutput("left", 0)
end


while true do
   print(rs.getBundledInput("back"))
   os.pullEvent("redstone")
  if rs.getBundledInput("back")==528 then --528 is cyan and yellow
	  repeat
	   d1()
	   until rs.getBundledInput("back")==256
  end
end
Edited by
Bomb Bloke #9
Posted 06 June 2014 - 03:20 AM
CC code runs very fast - faster than most devices can notice changes in redstone signals.

Your loop does turn the orange output off, but with no explicit pause between doing that and re-enabling it again, it goes back on so quickly that neither you (nor whatever is listening to that signal) can spot the change.

Try it like this:

function d1()
         rs.setBundledOutput("left", 2)
         sleep(1)
         rs.setBundledOutput("left", 0)
         sleep(0.5)
end

You're thinking too hard, Bomb :P/>

Not hard enough, more like. :(/>
matt2887 #10
Posted 06 June 2014 - 03:55 AM
Thanks bomb, I figured that out like moments after i posted it. I just have to bear in mind when coding that I have to allow for appropriate pauses to allow it to function properly.