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

Correct my programs.

Started by avatar5284, 08 August 2012 - 02:27 AM
avatar5284 #1
Posted 08 August 2012 - 04:27 AM
Ok, first off i'm new to lua coding so i'm not super good at it and want some tips and some people better at lua to check my programs to make sure they will run.

I know that there is a loop with the "And-" programs going to the core program then going to the next "And-' program i plan on doing a second program for the core programs that have no 'shell.run("****")' at the end of the program.

PROGRAMS
Spoilerprogram name: StartSorting

print("Processing Command…")
sleep(2)
print("Starting Sorting Machine…")
sleep(1)
redstone.setOutput("…", true)
shell.run("AndStartQuarry")

——————————————————

Program name: AndStartSorting

pass = Yes, YEs, YES, yES, yeS, yes

write"Would you like to start Sorting?: "
input = read()
if input == pass then
print ("OK running StartSorting program…")
sleep(1)
shell.run("StartSorting")
else
print ("OK skipping StartSorting program.")

——————————————————

Program name: StopSorting

print("Processing Command…")
sleep(2)
print("Stopping Sorting Machine…")
sleep(1)
redstone.setOutput("…", true)
shell.run("AndStopQuarry")

——————————————————

Program name: AndStopSorting

pass = Yes, YEs, YES, yES, yeS, yes

write"Would you like to stop Sorting?: "
input = read()
if input == pass then
print ("OK running StopSorting program…")
sleep(1)
shell.run("StopSorting")
else
print ("OK skipping StopSorting program.")

——————————————————

Program name: StartQuarry

print("Processing Command…")
sleep(2)
print("Starting Quarry Machine…")
sleep(1)
redstone.setOutput("…", true)
shell.run("AndStartSorting")

——————————————————

Program name: AndStartQuarry

pass = Yes, YEs, YES, yES, yeS, yes
write "Would you like to start the Quarry?: "
input = read()
if input == pass then
print ("OK Starting StartQuarry Program…")
sleep(1)
shell.run("StartQuarry")
else
print("OK Skipping StartQuarry Program.")

——————————————————

Program name: StopQuarry

print("Processing Command…")
sleep(2)
print("Stopping Quarry Machine…")
sleep(1)
redstone.setOutput("…", true)
shell.run("AndStopSorting")

——————————————————

Program name: AndStopQuarry

pass = Yes, YEs, YES, yES, yeS, yes
write "Would you like to stop the Quarry?: "
input = read()
if input == pass then
print ("OK Starting StopQuarry Program…")
sleep(1)
shell.run("StopQuarry")
else
print("OK Skipping StopQuarry Program.")
Pharap #2
Posted 08 August 2012 - 05:16 AM
Quite a large chunk of that is wrong.
And for one thing, you are splitting into programs what could be done with functions.

Instead of correcting all this (since it's a tiny bit all over the place and I'm guessing you have about 1 week of programming skill gained from flicking through computercraft examples) how about you tell me what you want the program to do, I'll code it, and then explain it to you so you understand what's going on?
Luanub #3
Posted 08 August 2012 - 06:26 AM
One thing i would definitely change is running these all as programs. It looks like that all could be separate functions within one program. By using shell.run() the way you are you are bound to end up with some program nesting which is going to cause you some undesirable effects.

Here's an example of what the functions would look like.

function startSorting()
--insert start sorting code
end

function stopSorting
--insert stop soring code
end


--example on how to call them
if input == "stop" then
  stopSorting()
elseif input == "start" then
  startSorting()
end

You also need to list a side in your redstone commands the … will not work so

redstone.setOutput("back", true) -- if redstone is on the back of the computer

You can also use a var which will make it easier to change your code if you are using a lot of rs commands.

local sSide = "back"
redstone.setOutput(sSide, true)

Other then that nothing stands out as being horribly wrong with the code. Make the updates and let me know if you need any further help with it.
avatar5284 #4
Posted 08 August 2012 - 07:12 AM
One thing i would definitely change is running these all as programs. It looks like that all could be separate functions within one program. By using shell.run() the way you are you are bound to end up with some program nesting which is going to cause you some undesirable effects.

Here's an example of what the functions would look like.

function startSorting()
--insert start sorting code
end

function stopSorting
--insert stop soring code
end


--example on how to call them
if input == "stop" then
  stopSorting()
elseif input == "start" then
  startSorting()
end

You also need to list a side in your redstone commands the … will not work so

redstone.setOutput("back", true) -- if redstone is on the back of the computer

You can also use a var which will make it easier to change your code if you are using a lot of rs commands.

local sSide = "back"
redstone.setOutput(sSide, true)

Other then that nothing stands out as being horribly wrong with the code. Make the updates and let me know if you need any further help with it.
Thanks for the help as i said i'm new to coding so i don't know a lot about lua.
i knew that the redstone.setOutput needed a side i just put … cuase i didnt know what side i was going to use after i built my machines. I think i can figure out the functions with how you explained them. (No one explained them as if they were talking to an idiot) And i foud a Menu tut. so this will end up being, working, and looking a lot simpler! Thank you so much! i'll have to send you a video of the finished project when i'm done ;)/>/>

Do you know how to use the rednet? so like say i want a computer room but don't want wires running everywhere. am i able to use the rednet to send a command from COM A to COM B and have COM B run the program and have COM B send info to COM A on if i want to run the "And-" functions? I hope you can understand what I am trying to say with that hehe.
Luanub #5
Posted 08 August 2012 - 07:34 AM
Rednet is a little intimidating at first but once you get into it you will realize that it's a lot easier then what you thought.

Here's an example

COM A

rednet.open("right") -- change this to the side of the computer the modem is on, will turn on the modem

rednet.send(2, "start")  -- 2 is COM B's ID.

COM B

rednet.open("right")

local id, msg = rednet.receive() -- waits infinitely for a message
if msg == "start" then
  startSort()
end
avatar5284 #6
Posted 08 August 2012 - 07:47 AM
Sweet thank you this will save a lot of space. With the VAR that you explained earlier where would I put the:
local sSide = "back"
And am I able to change sSide to whatever i want like say my sorter is on the left side of the com, could i do:

local sorter = "left"
redstone.setOutput(sorter, true)
avatar5284 #7
Posted 08 August 2012 - 08:18 AM
So i know i'm on the right page with functions would this be a working code using functions?
Spoiler

function StartSorter
     print ("Starting Sorter...")
     sleep(1)
     print("Sorter ON")
     redstone.setOutput(--, true)
end

function StopSorter
     print ("Stopping Sorter...")
     sleep(1)
     print ("Sorter OFF")
     redstone.setOutput(--, false)
end

function StartQuarry
     print("Starting Quarry...")
     sleep(1)
     print("Quarry ON")
     restone.setOutput(---, true)
end

function StopQuarry
     print("Stopping Quarry...")
     sleep(1)
     print("Quarry OFF")
     redstone.setOutput(---, false)
end

if input == "StartSorter" then
     StartSorter
else
if input == "StopSorter" then
     StopSorter
else
if input == "StartQuarry" then
     StartQaurry
else
if input == "StopQuarry" then
     StopQuarry
end
Luanub #8
Posted 08 August 2012 - 08:22 AM
Very close. add ()'s after your functions calls and it should work.


if input == "StartSorter" then
     StartSorter()
else
if input == "StopSorter" then
     StopSorter()
else
if input == "StartQuarry" then
     StartQaurry()
else
if input == "StopQuarry" then
     StopQuarry()
end
avatar5284 #9
Posted 08 August 2012 - 08:26 AM
So my post before the code i asked about Var's could you answer that for me then ill tweek my code and post it again.
then i will work on the RedNet stuff after i get this part figured out.
Luanub #10
Posted 08 August 2012 - 08:31 AM
Sorry I over looked that post. Yes since it is a var you can name it whatever you like. Do what works best for you.
avatar5284 #11
Posted 08 August 2012 - 08:34 AM
Does it matter where i put the
local --- = "---"
Luanub #12
Posted 08 August 2012 - 08:40 AM
Best to put it at the top of the code. That way the rest of the code knows what — =='s
avatar5284 #13
Posted 08 August 2012 - 08:46 AM
Ok, I went in added the () to the function calls and added the Var's in.
Spoiler

local Sorter = "left"
local Quarry = "back"

function StartSorter
     print ("Starting Sorter...")
     sleep(1)
     print("Sorter ON")
     redstone.setOutput(Sorter, true)
end

function StopSorter
     print ("Stopping Sorter...")
     sleep(1)
     print ("Sorter OFF")
     redstone.setOutput(Sorter, false)
end

function StartQuarry
     print("Starting Quarry...")
     sleep(1)
     print("Quarry ON")
     restone.setOutput(Quarry, true)
end

function StopQuarry
     print("Stopping Quarry...")
     sleep(1)
     print("Quarry OFF")
     redstone.setOutput(Quarry, false)
end

if input == "StartSorter" then
     StartSorter ()
else
if input == "StopSorter" then
     StopSorter ()
else
if input == "StartQuarry" then
     StartQaurry ()
else
if input == "StopQuarry" then
     StopQuarry ()
end
avatar5284 #14
Posted 08 August 2012 - 09:08 AM
Here is my COM A code for sending the commands to the COM B.

Spoiler

print ("Quarry And Sorter v.1")
print ("Commands:")
print ("StartQ = Start Quarry - StopQ = Stop Quarry")
print ("----------------------------------------------")
print ("StartS = Start Sorter - StopS = Stop Sorter")
write "Command: "
end

function StartQuarry
     rednet.open("right")
     rednet.send(2, "StartQuarry")
end

function StopQuarry
     rednet.open("right")
     rednet.send(2, "StopQuarry")
end

function StartSorter
     rednet.open("right")
     rednet.send(2, "Startsorter")
end

function StopSorter
     rednet.open("right")
     rednet.send(2, "Stopsorter")
end

if input == "StartQ" then
     StartQuarry ()
else
if input == "StopQ" then
     StopQuarry ()
else
if input == StartS then
     StartSorter ()
else
if input == StopS then
     StopSorter ()
end
avatar5284 #15
Posted 08 August 2012 - 09:18 AM
Here is my COM B code.

Spoiler

local Sorter = "left"
local Quarry = "back"

rednet.open("right")

local id, msg = rednet.recieve ()

if msg == StartSorter then
     StartSorter ()
else
if msg == StopSorter then
     StopSorter ()
else
if msg == StartQuarry then
     StartQuarry ()
else
if msg == StopQuarry then
     StopQuarry ()

function StartSorter
     print ("Starting Sorter...")
     sleep(1)
     print("Sorter ON")
     redstone.setOutput(Sorter, true)
     os.restart ()
end

function StopSorter
     print ("Stopping Sorter...")
     sleep(1)
     print ("Sorter OFF")
     redstone.setOutput(Sorter, false)
     os.restart ()
end

function StartQuarry
     print("Starting Quarry...")
     sleep(1)
     print("Quarry ON")
     restone.setOutput(Quarry, true)
     os.restart ()
end

function StopQuarry
     print("Stopping Quarry...")
     sleep(1)
     print("Quarry OFF")
     redstone.setOutput(Quarry, false)
     os.restart ()
end
Luanub #16
Posted 08 August 2012 - 10:29 AM
Here is my COM A code for sending the commands to the COM B.

Spoiler

print ("Quarry And Sorter v.1")
print ("Commands:")
print ("StartQ = Start Quarry - StopQ = Stop Quarry")
print ("----------------------------------------------")
print ("StartS = Start Sorter - StopS = Stop Sorter")
write "Command: "
end

function StartQuarry
	 rednet.open("right")
	 rednet.send(2, "StartQuarry")
end

function StopQuarry
	 rednet.open("right")
	 rednet.send(2, "StopQuarry")
end

function StartSorter
	 rednet.open("right")
	 rednet.send(2, "Startsorter")
end

function StopSorter
	 rednet.open("right")
	 rednet.send(2, "Stopsorter")
end

if input == "StartQ" then
	 StartQuarry ()
else
if input == "StopQ" then
	 StopQuarry ()
else
if input == StartS then
	 StartSorter ()
else
if input == StopS then
	 StopSorter ()
end

Okay for this one there is a couple of things.
-First off on line 7 you have an end. Everything above it is print or writes, none of them need and end so it should be removed.
-Second you're using incorrect if/elseif block syntax. else if in Lua is one word elseif. Proper block syntax is

if a == b then
--do stuff
elseif a == c then
--do someting different
else
--since nothing matches do this
end
You can use as many elseif's as you want, but only 1 if per statement, 1 else(it must be the last one), and 1 end.

-You're missing a read() to capture the users input
-Last you will want to place your prints and if statements in a loop.

COM A Corrected Code:
Spoiler

function StartQuarry() -- added ()'s to the functions
	 rednet.open("right")
	 rednet.send(2, "StartQuarry")
end

function StopQuarry()
	 rednet.open("right")
	 rednet.send(2, "StopQuarry")
end

function StartSorter()
	 rednet.open("right")
	 rednet.send(2, "Startsorter")
end

function StopSorter()
	 rednet.open("right")
	 rednet.send(2, "Stopsorter")
end

local function clear() -- added clear function
  term.clear()    
  term.setCursorPos(1,1)
end

while true do -- infinite loop
clear()
print ("Quarry And Sorter v.1")
print ("Commands:")
print ("StartQ = Start Quarry - StopQ = Stop Quarry")
print ("----------------------------------------------")
print ("StartS = Start Sorter - StopS = Stop Sorter")
write "Command: "
local input = read()
if input == "StartQ" then
	 StartQuarry() -- removed space inbetween function name and ()'s
elseif input == "StopQ" then
	 StopQuarry()
elseif input == "StartS" then
	 StartSorter()
elseif input == "StopS" then
	 StopSorter()
elseif input == "exit" then -- added a break to exit the loop and close the program. I'll let you add it to your menu. You can change the command too.
  break
end
end

Here is my COM B code.

Spoiler

local Sorter = "left"
local Quarry = "back"

rednet.open("right")

local id, msg = rednet.recieve ()

if msg == StartSorter then
	 StartSorter ()
else
if msg == StopSorter then
	 StopSorter ()
else
if msg == StartQuarry then
	 StartQuarry ()
else
if msg == StopQuarry then
	 StopQuarry ()

function StartSorter
	 print ("Starting Sorter...")
	 sleep(1)
	 print("Sorter ON")
	 redstone.setOutput(Sorter, true)
	 os.restart ()
end

function StopSorter
	 print ("Stopping Sorter...")
	 sleep(1)
	 print ("Sorter OFF")
	 redstone.setOutput(Sorter, false)
	 os.restart ()
end

function StartQuarry
	 print("Starting Quarry...")
	 sleep(1)
	 print("Quarry ON")
	 restone.setOutput(Quarry, true)
	 os.restart ()
end

function StopQuarry
	 print("Stopping Quarry...")
	 sleep(1)
	 print("Quarry OFF")
	 redstone.setOutput(Quarry, false)
	 os.restart ()
end

Pretty much the same here. One thing that will be an issue with this one is you have your if statement above your functions. Lua reads from the top down just like we do, if it reaches a function call or a variable that isn't declared until later in the code it does not know it exists and will error.

I fixed that and a couple of other things. Read the comments in both corrected codes for more info.

COM B Corrected Code:
Spoiler

local Sorter = "left"
local Quarry = "back"

rednet.open("right")

function StartSorter() -- added ()'s
	 print("Starting Sorter...")
	 sleep(1)
	 print("Sorter ON")
	 redstone.setOutput(Sorter, true)
	 --os.reboot() -- changed os.restart() to os.reboot() and actually going to remove these. Leaving the computer in a loop is probably better then constant rebooting. The state that you set your redstone will be set to off on the reboot.
end

function StopSorter()
	 print("Stopping Sorter...")
	 sleep(1)
	 print("Sorter OFF")
	 redstone.setOutput(Sorter, false)
end

function StartQuarry()
	 print("Starting Quarry...")
	 sleep(1)
	 print("Quarry ON")
	 restone.setOutput(Quarry, true)
end

function StopQuarry()
	 print("Stopping Quarry...")
	 sleep(1)
	 print("Quarry OFF")
	 redstone.setOutput(Quarry, false)
end

while true do -- loop it so it will always been listening, will need to ctrl+t to exit program
local id, msg = rednet.receive()
if msg == "StartSorter" then -- added "(double quotes) around the strings
	 StartSorter() -- removed spaces
elseif msg == "StopSorter" then -- elseif
	 StopSorter()
elseif msg == "StartQuarry" then
	 StartQuarry()
elseif msg == "StopQuarry" then
	 StopQuarry()
end
end

If you use Redpower and have bundled cables I can show you how to use that so you only have to run 1 cable out of the computer and will help clean up your physical setup. Send me a PM if you do and I will help you get it going.
avatar5284 #17
Posted 08 August 2012 - 04:29 PM
Ok i think i'm starting to understand the functions. with the COM A i was planning on doing it as the startup program. will the break close the program if its the startup program?
Cranium #18
Posted 08 August 2012 - 05:10 PM
No, break just stops an infinite loop. like this:

while true do --starts infinite loop
  if I == "awesome" then
    print("I am awesome")
  else
    break --ends infinite loop because I is no longer "awesome"
  end --ends the if/then statement
end --ends the while/do loop
As long as I am awesome, this will always print that I am awesome! Otherwise, if I somehow am no longer awesome, it stops.
avatar5284 #19
Posted 08 August 2012 - 05:20 PM
So if i were to make another function on COM A that stops the program?
is that possible?
Cranium #20
Posted 08 August 2012 - 05:27 PM
Sorry, I did not look at your code, just the question. In this instance, since there is no other code after the break on COM A, then the program WILL end after that. If you want it to startup again, and wait for your input, you can just create a startup(labelled "startup") program on the computer that just has one line saying shell.run("COM A program here"), and instead of break, replace it with os.reboot()
avatar5284 #21
Posted 08 August 2012 - 11:32 PM
Ok somewhere ether COM A isn't sending the command, or COM B isn't receiving the command.

COM A Code:
Spoiler

function StartQuarry()
	 rednet.open("right")
	 rednet.send(2, "StartQuarry")
end

function StopQuarry()
	 rednet.open("right")
	 rednet.send(2, "StopQuarry")
end

function StartSorter()
	 rednet.open("right")
	 rednet.send(2, "StartSorter")
end

function StopSorter()
	 rednet.open("right")
	 rednet.send(2, "StopSorter")
end

local function clear()
  term.clear()	
  term.setCursorPos(1,1)
end

while true do
clear()
print ("Quarry And Sorter v.1")
print ("Commands:")
print ("StartQ = Start Quarry - StopQ = Stop Quarry")
print ("----------------------------------------------")
print ("StartS = Start Sorter - StopS = Stop Sorter")
write "Command: "
local input = read()
if input == "StartQ" then
	 StartQuarry()
elseif input == "StopQ" then
	 StopQuarry()
elseif input == "StartS" then
	 StartSorter()
elseif input == "StopS" then
	 StopSorter()
elseif input == "exit" then
  break
end
end

COM B Code:
Spoiler

local Sorter = "left"
local Quarry = "back"

rednet.open("right")

function StartSorter()
	 print("Starting Sorter...")
	 sleep(1)
	 print("Sorter ON")
	 redstone.setOutput(Sorter, true)
end

function StopSorter()
	 print("Stopping Sorter...")
	 sleep(1)
	 print("Sorter OFF")
	 redstone.setOutput(Sorter, false)
end

function StartQuarry()
	 print("Starting Quarry...")
	 sleep(1)
	 print("Quarry ON")
	 restone.setOutput(Quarry, true)
end

function StopQuarry()
	 print("Stopping Quarry...")
	 sleep(1)
	 print("Quarry OFF")
	 redstone.setOutput(Quarry, false)
end

while true do
local id, msg = rednet.receive()
if msg == "StartSorter" then
	 StartSorter()
elseif msg == "StopSorter" then
	 StopSorter()
elseif msg == "StartQuarry" then
	 StartQuarry()
elseif msg == "StopQuarry" then
	 StopQuarry()
end
end
Cranium #22
Posted 09 August 2012 - 01:24 AM
Are you using the correct ID's of your computers? Is COM A 1 or 2, and same for COM B?
edit: also noticed your commands. Lua is case sensitive, so make sure you are entering it in right.
Edited on 08 August 2012 - 11:25 PM