←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Elevator, need help

Desert_foxhole's Photo Desert_foxhole 27 Jul 2015

so i have an elevator that i made in tekkit (from technic launcher)

so far this is what i have :

while true do
function writee(tText)
local j = fs.open("base/elevator/stat","w")
  j.write(tText)
  j.close()
end
function scan()

peripheral.call("left","setBackgroundColor", "colors.blue")
peripheral.call("left","setCursorPos", "1,1")
peripheral.call("left","write"," LOBBY ")
peripheral.call("left","setCursorPos", "1,2")
peripheral.call("left","write","   UP   ")
peripheral.call("left","setCursorPos", "1,3")
peripheral.call("left","write","  DOWN  ")
peripheral.call("left","setCursorPos", "1,4")
peripheral.call("left","write"," GROUND ")
peripheral.call("left","setCursorPos", "1,5")
peripheral.call("left","write","		")
end
function blank()
peripheral.call("left","setBackgroundColor", "colors.red")
peripheral.call("left","setCursorPos", "1,1")
peripheral.call("left","write","	   ")
peripheral.call("left","setCursorPos", "1,2")
peripheral.call("left","write","	   ")
peripheral.call("left","setCursorPos", "1,3")
peripheral.call("left","write","	   ")
peripheral.call("left","setCursorPos", "1,4")
peripheral.call("left","write","	   ")
peripheral.call("left","setCursorPos", "1,5")
peripheral.call("left","write","	   ")
end
function wait()
while t == 0 do
  scan()
   local t = 0
   local event, side, xPos ,yPos = os.pullEvent("monitor_touch")
	if yPos == "1" then
	 writee("top")
	 peripheral.call("bottom","move",1,false,false)
	 t = 1
	end
	if yPos == "2" then
	 writee("up")
	 peripheral.call("bottom","move",1,false,false)
	 t = 1
	end
	if yPos == "3" then
	 writee("down")
	 peripheral.call("bottom","move",0,false,false)
	 t = 1
	end
	if yPos == "4" then
	 writee("bottom")
	 peripheral.call("bottom","move",0,false,false)
	 t = 1
	end
end  
end
function run()
blank()
local g = fs.open("base/elevator/stat","r")
  local h = fs.readAll()
   if h == "down" then
	peripheral.call("bottom","move",0,false,false)
   end
   if h == "up" then
	peripheral.call("bottom","move",1,false,false)
   end
   h.close()
end
function check()
local var1 = redstone.getInput("back")
local var2 = redstone.getInput("right")
  if var1 == true then
   wait()
  else
   if var2 == true then
	clean()
	wait()
   else
	run()
   end
  end
end
check()
end

the issue is that i have two peripherals that are connected to one computer(one monitor, and one carriage controller (id:1124:2)), so i used peripheral.call to call to the two peripherals the only problem is that it doesnt show or do anything, it starts up normally and doesnt throw any errors, is there something wrong with my code?

if you have any suggestions or modifications that NEED to be made to the code please post it with your comment, ive read too many comments that the user has to ask how the suggested code needs to be implemented
Quote

Lyqyd's Photo Lyqyd 27 Jul 2015

Moved to Ask a Pro.
Quote

KingofGamesYami's Photo KingofGamesYami 27 Jul 2015

Ok, I'm just going to list some things I notice

1. Everything is wrapped in a loop, including the function declarations. It's better practice to define functions before a single, main loop.

2. That's a lot of peripheral.calls. I'd rather wrap the peripheral and use the calls indirectly, so it's easier to know what you're calling. It'll also error if you do something wrong, instead of simply failing.

Example:
peripheral.call( "left", "clear" )
peripheral.call( "left", "setCursorPos", 1, 1 )
peripheral.call9 "left", "write", "Hello World!" )

--#vs

local m = peripheral.wrap( "left" )
m.clear()
m.setCursorPos( 1, 1 )
m.write( "Hello World!" )

3."colors.blue" is not a color value, colors.blue is (Quotes make it a string, without them it's a number)
4. "1,1" is a string, and the function you're calling it with wants two numbers (not one string). See my above example code for correct usage
5. You can use the 'break' keyword instead of a variable to exit a loop. Not really a big thing, but you can do it.
6. The 'monitor_touch' event returns numbers, which will never be equal to a string.

print( 1 == "1" )

...remove the quotes from your numbers, because yPos will never equal "1".

As for the user having to ask how to implement things, we sometimes overestimate the ability of the asker to modify their code themselves. I believe my above statements are sufficient for you to understand what I found wrong with your code, and how to fix the things I stated. I do not claim to have fixed all of your mistakes, but I found a fair number.
Quote