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

Elevator

Started by Livebait, 06 April 2013 - 10:22 AM
Livebait #1
Posted 06 April 2013 - 12:22 PM
I made a little elevator using frame motors and wanted the up down feature to be controled by a computer. i have all the logic circuits set up and working i basically am needing a program that….
A: Sends a redstone signal to a remote computer (i put modems on both cause i think i had to)
B: perhaps since im using advanced computers if we could haev it setup to boot and have a little GUI with pretty much a floor selection (via button preferred not typing)
and C: if possible play elevator music :P/>
QuantumGrav #2
Posted 06 April 2013 - 01:09 PM
This is quite a big program, designing a nice looking GUI with mouse support that transmits the selection to another computer. I'd rather not just design one for you, since I don't know your particular setup and all that other jazz (and also because I'm slightly lazy and selfish with my time ;)/>), but I'll give you the resources to build your own. With a little effort, and some trial and error, I'm sure you can get it to work.

First, here is a short guide on menu making. These concepts can be used to help create your program.
http://www.computercraft.info/forums2/index.php?/topic/744-a-quick-guide-through-menu-making/

Second, to get mouse support, you'll need to learn how to use 'os.pullEvent()', one of the most useful commands in ComputerCraft.
http://www.computercraft.info/forums2/index.php?/topic/1516-ospullevent-what-is-it-and-how-is-it-useful/page__p__11156#entry11156
Let's say you want a button that is the dead center of the screen. To use it, you'll use something like:

local w,h = term.getSize()  -- Gets the size of the screen and sets the variables w and h to it.
rednet.open("right") -- Opens the modem that you attached to the computer.  Change 'right' to whatever side the modem's on.

while true do  -- Repeat forever
  event, id, x, y = os.pullEvent()
  if event == "mouse_click" then -- If the mouse was clicked...
    if x == w/2 and y == h/2 then -- if the cursor was at the middle of the screen...
	  -- You've Succesfully hit that button!  Do stuff with it like:
	 rednet.broadcast("button_hit") -- Send out a message to another computer, for instance commanding it to start the elevator.
    end
  end
end
(Code not tested, but it should work.)

I'd also highly recommend checking out nitrogenfinger's youtube video on GUI's.
http://www.youtube.com/watch?v=r6Jqnj7xA7U

For the music playing, hook up a disk drive to your computer, put in a music disk, and tell it to play with 'disk.playAudio(string of drive's side)'.

It's a demanding project, and you might want to do something simpler if your not already pretty confident in Lua. That said, if you work through it, it's a fun and rewarding experience!

Hope that helps!
Livebait #3
Posted 06 April 2013 - 02:07 PM
theres too much to read through… how bout a more basic program… triggers redstone signal at another computer …. simple gui maybe 1 for first floor 2 for second floor :P/>
QuantumGrav #4
Posted 06 April 2013 - 05:11 PM
Sure! I would use something like this:

Spoiler

--[[Preferences]]--
local bColor = colors.cyan -- Background Color
local tColor = colors.gray  -- Text Color.  Change colors to preference.
rednet.open("right") -- Change side to whichever your modem is on

--[[Code]]--
local w,h = term.getSize() -- Gets size of screen and sets it to w and h
local selection = 1  -- Used to keep track of the menu
local string, ypos, event, id, x, y, result -- Stating variables
local function printCentered(string, ypos)
  term.setCursorPos(w/2-#string/2, ypos) -- Sets cursor to print the string, centered and at the given y position.
  term.write(string)
end
local function drawGUI()
  term.setBackgroundColor(bColor)
  term.setTextColor(tColor)  -- These might error, I've never done this before.  If they do, just change bColor and tColor to whatever you would like. 
  term.clear()
  printCentered("Elevator Control", 1)
  printCentered(string.rep("-", w), 2) -- string.rep repeats the string a number of times, in this case, w, the width of the screen.
  printCentered("Floor 1", 8)
  printCentered("Floor 2", 13)  --Change the strings to whatever you would like for your options.
  if selection == 1 then ypos = 9 else ypos = 14 end -- Depending on the selection, print a line beneath an option.
  printCentered("---------", ypos)
end
local function main()
  while true do do -- Repeat forever
   result = nil -- Resetting the result of the operation
    drawGUI()
    event, id, x, y = os.pullEvent() -- Wait for something to happen, then record it.
    if event == "mouse_click" then -- If the mouse clicked...
	  if x >= w/2-3 and x <= w/2+3 then -- if it was within the x of the buttons...
	    if y == 8 then -- if it was option 1...
		  if selection ~= 1 then selection = 1 else result = 1 end -- If it wasn't selected, select it.  If it was, choose it.
	    end
	  else
		 if y == 13
		   if selection ~= 2 then selection = 2 else result = 2 end -- Same as above.
	    end
	  end
    elseif event == "key" then  -- If a key was pressed...
	  if id == 28 then  -- If it was enter...
	    result = selection
	  elseif id == 200 then  -- If it was down arrow...
	    if selection ~= 2 then selection = 2 end
	   elseif id == 208 then  -- If it was up arrow...
	    if selection ~= 1 then selection = 1 end
	   end
    end
    if result ~= nil then rednet.broadcast(tostring(result)) end  -- Broadcast the result of the menu.
  end
end
main()

I was able to cut a few corners since you only wanted two options. Unfortunately, I don't have access to ComputerCraft right now, so I can't test this. Should work. The receiving computer should look like this:

Spoiler

rednet.open("right") -- Again, change side to whichever your modem is on.
local sender, message, distance -- Declaring variables

while true do
   sender, message, distance = os.pullEvent("rednet_message") -- Wait for a message
   if message == "1" then
	 -- Command for floor 1
   elseif message == "2" then
	  -- Command for floor 2
   end
end

Again, this is untested.

If you get any errors when running these programs, fix it yourself or let me know, I'd be glad to fix my mistake! This system is (probably) overloadable, so don't spam commands, or it might break. This is a pretty good base for the program, so feel free to edit and change to your heart's content!

Hope that helps!
Livebait #5
Posted 07 April 2013 - 12:40 PM
wow that code just looks amazing :P/> man if you typed all that kudos i appreciate the little bit of teaching done at the end . if you did it some other way thanks still. got 2nd computer all set up first is giving me an error at line 22 for end function somethin?
SadKingBilly #6
Posted 07 April 2013 - 03:35 PM
wow that code just looks amazing :P/> man if you typed all that kudos i appreciate the little bit of teaching done at the end . if you did it some other way thanks still. got 2nd computer all set up first is giving me an error at line 22 for end function somethin?
If you just copied his code, then I'm pretty sure the problem is the second 'do' on the line "while true do do – Repeat forever".
Livebait #7
Posted 07 April 2013 - 04:06 PM
hmm that fixxed that… now getting – "Elevator:4: attempt to call nil – hate to keep asking for help but this stuff might as well be…. well id say a different language but technically it is :P/> heh either way i cant do much i fixxed some bugs i had myself from copying it but when the code itself seems to have an error i cant even come up with a half brained idea of what to do
Livebait #8
Posted 07 April 2013 - 04:19 PM
im not sure what nil is only in lines 24/46 and the one result says its to reset it seems pretty basic… im guessing its the line 46? should i break that up or ?
Livebait #9
Posted 07 April 2013 - 04:28 PM
ok playing around with it i think imnot supposed to copy it verbatim on line 46 the tostring is where i put the computer im sending the signal to? least thats what my head is coming up with
Livebait #10
Posted 07 April 2013 - 05:17 PM
ok no ill give up and wait for next advice :P/>
SadKingBilly #11
Posted 07 April 2013 - 05:26 PM
As far as I can tell, the only thing that would prevent QuantumGrav's code from executing at all is the duplicate "do" that I mentioned. Otherwise, you may have just copied it incorrectly.

Honestly, it would be far easier to just use a command-line interface:

-- Elevator interface
rednet.open("right")
while true do
	term.clear()
	term.setCursorPos(1, 1)
	print("What floor would you like to go to?")
	selection = read()
	if tonumber(selection) == 1 then
		rednet.broadcast("1")
	elseif tonumber(selection) == 2 then
		rednet.broadcast("2")
	else
		print("Floor not valid.")
		os.sleep(3)
	end
end

-- Elevator controller
rednet.open("right")
while true do
	_, message = rednet.receive()
	if tonumber(message) == 1 then
		-- floor 1 code goes here
	elseif tonumber(message) == 2 then
		-- floor 2 code goes here
	end
end
QuantumGrav #12
Posted 07 April 2013 - 06:16 PM
Sorry about the extra 'do' in the code! Stuff like that happens when you code that much and can't test.

I double checked my code, and I can't see why it's not working for you. I would double check in both codes that the "right" in rednet.open("right") is changed to whatever side your modem is on. The sides that you can change it to are: top, bottom, left, right, front, and back. If that's all good, I'm not sure what's causing your error!

TheCoryKid is right, something like he has made would be simpler, so use that if you'd prefer! I hope you can get your Elevator working!
theoctagon #13
Posted 10 April 2013 - 09:54 AM
I can actually help with this. I wrote a VERY basic elevator program that I'll share. It is set as the startup for each adv. comp on each floor. Each floor has the blocks above and below the floor/ceiling as blocks that are pushed out to stop the traveler at the floor requested. Obviously its set up for my particular situation and mechanics, but perhaps some of the coding will help you figure it out a little easier.

Enjoy!

flr = 0
curflr = 3
outputside = "back"
modemside = "bottom"
chk = false
comp1 = 131
comp2 = 127
comp3 = 128
comp4 = 130
comp5 = 133
comp6 = 137
comp7 = 139
comp8 = 142
comp9 = 144
comp10 = 146

local self = os.getComputerID()

function chgflr(flr,chk)
– chk is for ensuring activation
print(flr," = floor passed")
if flr == 1 then
flr = tonumber(comp1)
elseif flr == 2 then
flr = tonumber(comp2)
elseif flr == 3 then
flr = tonumber(comp3)
elseif flr == 4 then
flr = tonumber(comp4)
elseif flr == 5 then
flr = tonumber(comp5)
elseif flr == 6 then
flr = tonumber(comp6)
elseif flr == 7 then
flr = tonumber(comp7)
elseif flr == 8 then
flr = tonumber(comp8)
elseif flr == 9 then
flr = tonumber(comp9)
elseif flr == 10 then
flr = tonumber(comp10)
end
– print("Passed the if's")
rednet.send(flr,"Activate")
print("Message sent to the ", flr, " floor")
– print("You have 10 seconds to arrive.")
os.sleep(300)
term.clear()
end

function scrnmsg(flr)
term.clear()
term.setCursorPos(1,1)
print("Hello, I am ", self)
print("Please select floor: ")
print("1: Bridge ")
print("2: Bridge - Lower level")
print("3: 10 - forward")
print("4: Transporters")
print("5: Sick Bay")
–print("6: Engineering")
–print("7: Empty Deck")
–print("8: Crew Deck")
–print("9: Biosphere")
print(" ")
flr = read()

flr = tonumber(flr)
print()
print("You've selected: ", flr)

if flr == curflr then
print("Same floor")
flr = 0
return
end
if flr <= curflr then
dir = "up"
if flr <= 1 then
flr = 1
else
flr = flr -1
end

print(flr, "Floor")
end
– print("Flooring")
– print(flr)
chgflr(flr)
– print("back")

return
end

function listen(sender, message)
rednet.open(modemside)
sender, message = rednet.receive()
print("Sender: ", sender, " Sent the following message: ",message)
if message == "Activate" then
rs.setOutput(outputside, true)
os.sleep(5)
rs.setOutput(outputside, false)
end

return sender, message
end

— Begin program run
while true do
term.clear()
print("Starting …..")
parallel.waitForAny(scrnmsg, listen)
– print("next …")
– print(flr)
– print("Done")
end
Smiley43210 #14
Posted 10 April 2013 - 10:24 AM
I've actually started working on an elevator script a month or two ago, so if I'm feeling helpful soon, I might post bits of it to help, the GUI creation in particular (auto generates according to monitor size, has a configureable "floor list" table that you put your floor names and such in).
theoctagon #15
Posted 10 April 2013 - 10:41 AM
I've actually started working on an elevator script a month or two ago, so if I'm feeling helpful soon, I might post bits of it to help, the GUI creation in particular (auto generates according to monitor size, has a configureable "floor list" table that you put your floor names and such in).

Sounds awesome!
Smiley43210 #16
Posted 10 April 2013 - 07:19 PM
Yea, this is going to lead to spoon feeding…

But how do you want it to look?
Something like this?
http://imgur.com/QOppk9N
http://imgur.com/AtdzdEt
QuantumGrav #17
Posted 11 April 2013 - 06:08 AM
@theoctagon: I really recommend putting code that large into a code and a spoiler, it's just nicer for others to read that way.

@Smiley43210: I'd also enjoy seeing that program! Sounds neat.

@Livebait: Have you gotten any of these programs to work how you want them to yet?
Smiley43210 #18
Posted 13 April 2013 - 01:32 PM
Ok, I'll post it when school is over.