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

Need help with my frist program

Started by isee, 01 November 2012 - 08:14 PM
isee #1
Posted 01 November 2012 - 09:14 PM
Hi all.
I have built a ship using support frames that uses 8 engines to move in 4 directions, each pair of engines alternate in moving a piston-like frame inside.
In essence to move in the chosen direction i need to send a redstone signal to one engine, that would push out a frame, which the second engine rests on. then a second later i send a signal to the second engine wich pull the whole ship using the frame as anchor.

It sor of works now with just a timer sending both signals out simultaneously, but as you can imagine it is not too stable or robust.

This is what I have managed to write so far but i keep getting errors once i am past collecting the variables and integers from the user. I never really written anything before and this took me a good 2 hours. Rather than waste another 4 hours writing some clumsy code through trial and error, I was hoping someone could teach me the style of at least the first set of IF loop.

Any tips or ideas on how to handle unexpected inputs or unforseen scenarior are very welcome.

Lastly, due to possible blocks blocking the ship's path it is possible that it will get stuck with the engine's piston being offset? wich would prevent any movement sideways. I need to incorporate a reset command, to do that I simply need to send 2 signals to one engien and 1 signal to the opposing one for each axis (so twice). Perhaps I can just add a "reset" value "direction" string, but then i don't want the interface asking me how many blocks i would like the "reset" to move :P/>/>


term.clear()
textutils.slowPrint("Ship Navigation Interface")
print("Please State Direction")
print("F orward, B ack, L eft, R ight")
print ("use first letters only")
direction =  read()
print("")
print("now enter the number of blocks you wish to move")
print("between 1 and 15")
blocks = tonumber(read())
if blocks >15 then blocks = 15
if blocks <1 then blocks = 1
if direction == F then
for 1=1, blocks do
redstone.setBundledOutput("left", colors.blue)
sleep(1)
redstone.setBundledOutput("left", colors.red)
sleep(1)
end
esle print ("oopsie")

Thanks a lot for your help in advance
isee #2
Posted 01 November 2012 - 09:55 PM
Just added "" around F and it seems to now pulse the required number of times, but when the loop is finished it keeps the red wire on. any idea why it does that?
Luanub #3
Posted 01 November 2012 - 11:24 PM
When you turn on red it turns off blue since you only specified red, but after red is turned on you never turn it off or another color on. Add "rs.setBundledOutput("left", 0)" after the sleep for red and that will turn off all colors.

It looks like theres some issues with you if statements as well. I'm suprised the code is running, it should be erroring for missing ends and a misspelled else. See comments below.

if blocks >15 then blocks = 15
elseif blocks <1 then blocks = 1  --changed to elseif you do not need two if blocks for this
  if direction == "F" then
	for 1=1, blocks do
	  redstone.setBundledOutput("left", colors.blue)
	  sleep(1)
	  redstone.setBundledOutput("left", colors.red)
	  sleep(1)
	  rs.setBundledOutput("left",0) -- turn off red
	end -- end for for loop
	else print ("oopsie") --corrected else
  end -- added end for second if block
end -- added end for first if block

remiX #4
Posted 01 November 2012 - 11:27 PM
for 1=1, blocks must be for i= 1, blocks. and you're missing two ends.

edit: this should work better:

Spoiler

term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Ship Navigation Interface") sleep(0.5)
print("Please State DirectionnF orward, B ack, L eft, R ightnuse first letters only")
repeat
    d =  string.lower(read())
until d == "f" or d == "b" or d == "l" or d == "r"
print("nNow enter the number of blocks you wish to move.nBetween 1 and 15")
repeat
    blocks = tonumber(read())
until blocks >= 1 and blocks <=15

if direction == "f" then
    for 1=1, blocks do
        -- code
    end
elseif direction == "b" then
    for 1=1, blocks do
        -- code
    end
elseif direction == "l" then
    for 1=1, blocks do
        -- code
    end
elseif direction == "r" then
    for 1=1, blocks do
        -- code
    end
end
isee #5
Posted 01 November 2012 - 11:38 PM
When you turn on red it turns off blue since you only specified red, but after red is turned on you never turn it off or another color on. Add "rs.setBundledOutput("left", 0)" after the sleep for red and that will turn off all colors.

It looks like theres some issues with you if statements as well. I'm suprised the code is running, it should be erroring for missing ends and a misspelled else. See comments below.

if blocks >15 then blocks = 15
elseif blocks <1 then blocks = 1  --changed to elseif you do not need two if blocks for this
  if direction == "F" then
	for 1=1, blocks do
	  redstone.setBundledOutput("left", colors.blue)
	  sleep(1)
	  redstone.setBundledOutput("left", colors.red)
	  sleep(1)
	  rs.setBundledOutput("left",0) -- turn off red
	end -- end for for loop
	else print ("oopsie") --corrected else
  end -- added end for second if block
end -- added end for first if block

Sorry I didn't know how to copy from my minecraft computer terminal so retyped the code with typos, it was not misspelled in the actual code.
I am thinking, I should jsut make it do the "reset" dance after every loop is finished. so once I made the ship move 15 blocks it will jsut do the reset jig at the end, wich would mean that it will be nice and centred before any further move inputs anyway.

Is there a way i can store each small and common function into a file or as a string and then call upon it within the main program? It would save me on a lot of retyping.

So for example if the user specifies F, instead of having the loop in the main body of code i can just get the program to
do "movefrward", "blocks" amout of times?
and then run "reset" one the loop is complete?
remiX #6
Posted 02 November 2012 - 12:35 AM
Yeah well you can do this


function forward()
  redstone.setBundledOutput("left", colors.blue)
  sleep(1)
  redstone.setBundledOutput("left", colors.red)
  sleep(1)
end

for i =1, blocks do
  forward()
end
isee #7
Posted 02 November 2012 - 01:35 AM
Ok here is what I've done so far. Will now try to transfer it into game and run it.
Any glaring mistakes that you can see?

Just had a thought, the computer terminal will be part of the ship it moves, will it affect the signal timing i wonder?


repeat until -- this is so the program runs until i forcefully quit it
l=1
function resetFB() --piston reset function to be placed at the end of each front or back move
	 redstone.setBundledOutput("left", colors.blue)
	 sleep(1)
  rs.setBundledOutput("left",0)
  sleep(1)
  redstone.setBundledOutput("left", colors.blue)
	 sleep(1)
  rs.setBundledOutput("left",0)
  sleep(1)
	 redstone.setBundledOutput("left", colors.red)
	 sleep(1)
	 rs.setBundledOutput("left",0)
end
function resetLR() --piston reset function to be placed at the end of each left or right move
	 redstone.setBundledOutput("right", colors.blue)
	 sleep(1)
  rs.setBundledOutput("right",0)
  sleep(1)
  redstone.setBundledOutput("right", colors.blue)
	 sleep(1)
  rs.setBundledOutput("right",0)
  sleep(1)
	 redstone.setBundledOutput("right", colors.red)
	 sleep(1)
	 rs.setBundledOutput("right",0)
end
term.clear()
term.setCursorPos(1,1)
testutils.slowPrint("Ship Navigation Interface")
print("Please State DirectionnF orward, B ack, L eft, R ightnuse first letters only")
repeat
    d =  string.lower(read())
until d == "f" or d == "b" or d == "l" or d == "r" or d == "F" or d == "B" or d == "L" or d == "R"
print("nNow enter the number of blocks you wish to move.nBetween 1 and 15")
repeat
    blocks = tonumber(read())
until blocks >= 1 and blocks <=15
if direction == "f" or direction == "F" then
    print("executing maneuvre...")
    for i=1, blocks do
		  redstone.setBundledOutput("left", colors.blue)
		  sleep(1)
		  redstone.setBundledOutput("left", colors.red)
		  sleep(1)
		  rs.setBundledOutput("left",0)
    end
resetFB()

elseif direction == "b" or direction == "B" then
    print("executing maneuvre...")
for i=1, blocks do
		  redstone.setBundledOutput("left", colors.black)
		  sleep(1)
		  redstone.setBundledOutput("left", colors.green)
		  sleep(1)
		  rs.setBundledOutput("left",0)
    end
resetFB()
elseif direction == "l" or direction == "L" then
    print("executing maneuvre...")
for i=1, blocks do
		  redstone.setBundledOutput("right", colors.blue)
		  sleep(1)
		  redstone.setBundledOutput("right", colors.red)
		  sleep(1)
		  rs.setBundledOutput("right",0)-- code
    end
resetLR()
elseif direction == "r" or direction == "R"then
    print("executing maneuvre...")
for i=1, blocks do
		  redstone.setBundledOutput("right", colors.black)
		  sleep(1)
		  redstone.setBundledOutput("right", colors.green)
		  sleep(1)
		  rs.setBundledOutput("right",0) -- code
    end
resetLR()
end
until l=0
KaoS #8
Posted 02 November 2012 - 01:44 AM
wait a second. this flying machine works? I've made moving bases etc before but whenever I moved a computer or and CC item with a frame the game crashed and it duplicated the item when I rejoined (a copy where the frame moved it to and a copy where it was before moving). I did not report this as I was on tekkit. does this actually work?
isee #9
Posted 02 November 2012 - 01:54 AM
wait a second. this flying machine works? I've made moving bases etc before but whenever I moved a computer or and CC item with a frame the game crashed and it duplicated the item when I rejoined (a copy where the frame moved it to and a copy where it was before moving). I did not report this as I was on tekkit. does this actually work?

Well i moved it a good 64 blocks so far using timers, which were also attached to the ship. I want to stress that it is not flying. it only moves in 4 directions, rather than 6 at sea level.




I don't know how the computer will behave when it is in transition. but I suppose i could always just place a terminal on it and have a computer underground controlling the actual pulses.
isee #10
Posted 02 November 2012 - 02:01 AM
keep getting
attempt to index ? (a nil value)
in most forms of the code so far, even cutting it down to mostly what has been suggested without my own "additions"
no idea what it means
isee #11
Posted 02 November 2012 - 02:24 AM
Update, found a typo in line 3.
the all incompassinf perpetual loop didn't work, but the rest worked beautifly!
Thanks
sIdEkIcK_!
isee #12
Posted 02 November 2012 - 02:30 AM
Actually, spoke too soon, it seems to execute left side black and green only, no matter what direction i choose.
isee #13
Posted 02 November 2012 - 02:34 AM
This is the latest code I am using and it always executres the Back maneuvre
Spoiler

function resetFB() --piston reset function to be placed at the end of each front or back move
	 redstone.setBundledOutput("left", colors.blue)
	 sleep(1)
  rs.setBundledOutput("left",0)
  sleep(1)
  redstone.setBundledOutput("left", colors.blue)
	 sleep(1)
  rs.setBundledOutput("left",0)
  sleep(1)
	 redstone.setBundledOutput("left", colors.red)
	 sleep(1)
	 rs.setBundledOutput("left",0)
end
function resetLR() --piston reset function to be placed at the end of each left or right move
	 redstone.setBundledOutput("right", colors.blue)
	 sleep(1)
  rs.setBundledOutput("right",0)
  sleep(1)
  redstone.setBundledOutput("right", colors.blue)
	 sleep(1)
  rs.setBundledOutput("right",0)
  sleep(1)
	 redstone.setBundledOutput("right", colors.red)
	 sleep(1)
	 rs.setBundledOutput("right",0)
end
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Ship Navigation Interface") sleep (0.5)
print("Please State DirectionnF orward, B ack, L eft, R ightnuse first letters only")
repeat
    d =  string.lower(read())
until d == "f" or d == "b" or d == "l" or d == "r" or d == "F" or d == "B" or d == "L" or d == "R"
print("nNow enter the number of blocks you wish to move.nBetween 1 and 15")
repeat
    blocks = tonumber(read())
until blocks >= 1 and blocks <=15
if direction == "f" or direction == "F" then
    print("executing maneuvre Forward...")
    for i=1, blocks do
		  redstone.setBundledOutput("left", colors.blue)
		  sleep(1)
		  redstone.setBundledOutput("left", colors.red)
		  sleep(1)
		  rs.setBundledOutput("left",0)
    end
resetFB()

elseif direction == "b" or direction == "B" then
    print("executing maneuvre Back...")
for i=1, blocks do
		  redstone.setBundledOutput("left", colors.black)
		  sleep(1)
		  redstone.setBundledOutput("left", colors.green)
		  sleep(1)
		  rs.setBundledOutput("left",0)
    end
resetFB()
elseif direction == "l" or direction == "L" then
    print("executing maneuvre Left...")
for i=1, blocks do
		  redstone.setBundledOutput("right", colors.blue)
		  sleep(1)
		  redstone.setBundledOutput("right", colors.red)
		  sleep(1)
		  rs.setBundledOutput("right",0)-- code
    end
resetLR()
elseif direction == "r" or direction == "R"then
    print("executing maneuvre Right...")
for i=1, blocks do
		  redstone.setBundledOutput("right", colors.black)
		  sleep(1)
		  redstone.setBundledOutput("right", colors.green)
		  sleep(1)
		  rs.setBundledOutput("right",0) -- code
    end
resetLR()
end
KaoS #14
Posted 02 November 2012 - 03:26 AM
I know it works with timers, that's how I did it with levers to toggle them. but frames could not move CC items, can you place a computer in your ship?
isee #15
Posted 02 November 2012 - 03:35 AM
I know it works with timers, that's how I did it with levers to toggle them. but frames could not move CC items, can you place a computer in your ship?
yes i had no problems placing mine, though could not actually test it in transition until the code is fixed
remiX #16
Posted 02 November 2012 - 04:38 AM
So your problem now is that it only goes backwards, no matter what you type in?

Took a quick look and found it :D/>/>

You took this code from me:


repeat
  d =  string.lower(read())
until d == "f" or d == "b" or d == "l" or d == "r" or d == "F" or d == "B" or d == "L" or d == "R"  -- btw you don't need to check if it is F, B, L, or R because string.lower() makes it lower case. So you can remove that'
I changed it to 'd' from 'direction' because the until line would be long.

And the rest of the code checks if the direction is equal, not d, so change d to direction or visa versa.


if direction == "f" or direction == "F" then
You see, change that direction to d (and for all the elseif's) :)/>/> Btw the or direction == "F" is not needed because the value of the direction goes into lower-case :)/>/>
isee #17
Posted 02 November 2012 - 04:45 AM
So your problem now is that it only goes backwards, no matter what you type in?

Took a quick look and found it :D/>/>

You took this code from me:


repeat
  d =  string.lower(read())
until d == "f" or d == "b" or d == "l" or d == "r" or d == "F" or d == "B" or d == "L" or d == "R"  -- btw you don't need to check if it is F, B, L, or R because string.lower() makes it lower case. So you can remove that'
I changed it to 'd' from 'direction' because the until line would be long.

And the rest of the code checks if the direction is equal, not d, so change d to direction or visa versa.


if direction == "f" or direction == "F" then
You see, change that direction to d (and for all the elseif's) :)/>/> Btw the or direction == "F" is not needed because the value of the direction goes into lower-case :)/>/>
d'oh!

thanks again!
how can i now encompass the whole program into a loop so that whenever a user right clicks the terminal, the Ship control interface is all they see?

thanks again!
remiX #18
Posted 02 November 2012 - 04:56 AM
Use a while true do loop :D/>/>

Give it to me and I'll do it for you if you want - and I'll make it so you can press r, l, f, or b instead of typing :)/>/>
isee #19
Posted 02 November 2012 - 05:21 AM
Good news and bad news!
The code worked flawlessly!

but bits of wiring are falling off inside the engine room. the wireless receiver fell off twice…
So to my surprise, the problem is not redpulse timing and order, its the frames mod. jsut not robust enough…

Shame! I suppose I could rebuilt id slightly bigger and try a design that has more room for me to try different layouts and placements, but frankly am losing the will to live after all this :D/>/>

anyway, cheers for all the help!
KaoS #20
Posted 02 November 2012 - 06:39 AM
I have significant experience with frames, I made a ship assembling platform that helped me a lot, I could help you out if you like, the secret is covers and knowing exactly how to use them

EDIT: although I must admit that I never build a multidirectional platform, only one direction. I was only briefly interested in the moving base/tank idea, got pissed when moving forcefield generators didn't work correctly… but I am used to moving curcuits around in my workshops and cranes and suchlike. I could take a look
isee #21
Posted 02 November 2012 - 11:55 AM
I have significant experience with frames, I made a ship assembling platform that helped me a lot, I could help you out if you like, the secret is covers and knowing exactly how to use them

EDIT: although I must admit that I never build a multidirectional platform, only one direction. I was only briefly interested in the moving base/tank idea, got pissed when moving forcefield generators didn't work correctly… but I am used to moving curcuits around in my workshops and cranes and suchlike. I could take a look

Thanks, you are weclome to pop in and take a look the ship is right n fron of the spawn right now. I will send you the i.p. through a PM.
The problem is that these bits did not fall off when i was just pulsing timers and though i have only discovered frames yesterday (hence why i admit that the design could have been a bit bigger and more robust) I am pretty sure one fo the vital bits that keeps falling off is resting on the correct cover and without anything in the way.
Either way. sedning you a PM right now so you can take a look.
ChunLing #22
Posted 02 November 2012 - 12:06 PM
Use a turtle rather than a computer.