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
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.
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.computerc...1156#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.
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!
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
--[[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!
wow that code just looks amazing 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?
wow that code just looks amazing 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".
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 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
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 ?
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
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
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!
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.
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
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).
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).