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

[Computer] Lightswitch

Started by EmTeaKay, 13 April 2012 - 11:54 PM
EmTeaKay #1
Posted 14 April 2012 - 01:54 AM
This code is for a lightswitch.
Spoilerhttp://pastebin.com/QE96u2Ts
Name that as startup and put it into your programs file.
Spoilerhttp://pastebin.com/gAUqnUH2
The second file must be names use2!

P.S. This 100% my code!
libraryaddict #2
Posted 14 April 2012 - 12:49 PM
You can also look at other ways to accomplish this.


while true do
  shell.run("clear")
  write("Would you like to turn the lights on or off? ")
  local YesOrNo = read()
  if YesOrNo == "on" then
    rs.setOutput("back", true)
  elseif YesorNo == "off" then
    rs.setOutput("back", false)
  else
    print("Please use "on" or "off"")
    sleep(3)
  end
end

while true do - This will loop the following block while it is true

This is looping "clear screen" "write question" "Wait for input" "if input matches change redstone" "restart!"
nitrogenfingers #3
Posted 14 April 2012 - 01:12 PM
Simpler still:


term.clear()
term.setCursorPos(15, 8)
print("Hit Enter to change the lights.")
while true do
  local id,key = os.pullEvent("key")
  if key==28 then rs.setOutput("back", not rs.getOutput("back")) end
end
EmTeaKay #4
Posted 14 April 2012 - 02:26 PM
Look, I'm not trying to make it short and sweet, I'm trying to learn. And if I have to code a lot of lines, so be it! You'll use your way, and I'll use mine.
Edited by
Advert #5
Posted 14 April 2012 - 10:11 PM
Look, I'm not trying to make it short and sweet, I'm trying to learn. And if I have to code a lot of lines, so be it! You'll use your way, and I'll use mine.

Please don't take their posts the wrong way – they are showing you ways you could have done it differently. Essentially, they are giving you constructive criticism.
Wolvan #6
Posted 14 April 2012 - 10:31 PM
I want to help you with some ideas:
1. You don't have to do that:

yes = "yes"
if input == (yes)
you can instead use

if input == "yes"
2. As the people above already said try using while loops (build like this)

while [condition|true for infinite loop]
[stuff you want to do]
end
If you get an too long without yielding error write

while [condition|true for infinite loop] do
sleep(0)
[stuff you want to do]
end
you don't even need use2 anymore with a loop



with these 2 ideas you can make a nicer code but I have to admit that's a really nice code you have there. Is this your first coding?
EmTeaKay #7
Posted 14 April 2012 - 11:03 PM
Yes, without help. And sorry for taking the post the wrong way. I did this all in-game and was too lazy to check for other was of doing it. It was a great learning experiance.
I know only the basic's and I heard that loops were bad, so I didn't use them.
libraryaddict #8
Posted 15 April 2012 - 01:48 AM
Yere, In this case I didnt know about nitrogenfingers way of doing code.

Lots of my learning was looking at other people's code.

You see how it all works and fits together, Then you can use little snippets of their code and before you know it.
You learnt how to code in a day.
nitrogenfingers #9
Posted 15 April 2012 - 02:50 AM
Sorry EmTeaKay, no offence meant!
Cloudy #10
Posted 15 April 2012 - 02:52 AM
I know only the basic's and I heard that loops were bad, so I didn't use them.

Loops aren't bad - so feel free to use them. I don't know why anyone would think loops are bad, they are fundamental programming concept. Does your computer shut down after you type one command or run one program? :)/>/> No, because they use loops.
EmTeaKay #11
Posted 15 April 2012 - 03:08 AM
I know only the basic's and I heard that loops were bad, so I didn't use them.

Loops aren't bad - so feel free to use them. I don't know why anyone would think loops are bad, they are fundamental programming concept. Does your computer shut down after you type one command or run one program? :)/>/> No, because they use loops.

I heard someone say that they caused their computer to crash. And I didn't know.
BigSHinyToys #12
Posted 15 April 2012 - 04:22 AM
it depends on what the loop does but computer craft should never crash your computer. i will automatically stop loops that have been going for ludicrous amounts of time.

fore example

local a = 0 --local variable.
while true do  -- start of a loop
print (a) -- prints to the screen the value of a
a = a+1 -- makes a larger by one every loop
end --  end of the loop

running that will result in computer craft automatically ending it.
Cloudy #13
Posted 15 April 2012 - 07:36 PM
I know only the basic's and I heard that loops were bad, so I didn't use them.

Loops aren't bad - so feel free to use them. I don't know why anyone would think loops are bad, they are fundamental programming concept. Does your computer shut down after you type one command or run one program? :)/>/> No, because they use loops.

I heard someone say that they caused their computer to crash. And I didn't know.

If you call the same function in a function you will eventually run out of memory - but not if you use loops properly.
cant_delete_account #14
Posted 15 April 2012 - 07:44 PM
I know only the basic's and I heard that loops were bad, so I didn't use them.

Loops aren't bad - so feel free to use them. I don't know why anyone would think loops are bad, they are fundamental programming concept. Does your computer shut down after you type one command or run one program? :)/>/> No, because they use loops.

I heard someone say that they caused their computer to crash. And I didn't know.

If you call the same function in a function you will eventually run out of memory - but not if you use loops properly.
Yeah, that still doesn't crash your computer, it crashes Minecraft, it will give an 'Minecraft ran out of memory' error.
Cloudy #15
Posted 15 April 2012 - 11:59 PM
I know only the basic's and I heard that loops were bad, so I didn't use them.

Loops aren't bad - so feel free to use them. I don't know why anyone would think loops are bad, they are fundamental programming concept. Does your computer shut down after you type one command or run one program? :)/>/> No, because they use loops.

I heard someone say that they caused their computer to crash. And I didn't know.

If you call the same function in a function you will eventually run out of memory - but not if you use loops properly.
Yeah, that still doesn't crash your computer, it crashes Minecraft, it will give an 'Minecraft ran out of memory' error.

By computer, I was thinking the minecraft computer, not the actual computer. It will eventually "crash" as the stack has been filled up.
beanie92 #16
Posted 15 January 2013 - 04:37 PM
ive been looking for something like this, so i tried the codes out but they dont seem to work
theoriginalbit #17
Posted 15 January 2013 - 06:06 PM
By computer, I was thinking the minecraft computer, not the actual computer. It will eventually "crash" as the stack has been filled up.
What is the current max stack size?
1lann #18
Posted 15 January 2013 - 06:29 PM
By computer, I was thinking the minecraft computer, not the actual computer. It will eventually "crash" as the stack has been filled up.
What is the current max stack size?

*cough* Cloudy, on 15 April 2012 - 05:59 PM, said:

Anywho, the max stack size in cc is 256
theoriginalbit #19
Posted 15 January 2013 - 06:30 PM
*cough* Cloudy, on 15 April 2012 - 05:59 PM, said:

Anywho, the max stack size in cc is 256
thanx… where did he say that?
1lann #20
Posted 15 January 2013 - 08:40 PM
*cough* Cloudy, on 15 April 2012 - 05:59 PM, said:

Anywho, the max stack size in cc is 256
thanx… where did he say that?
I was trying to point out that this topic is almost a year old. Also you can tell the max stack size by doing

function a()
  a()
end
a()
However I tried using some printing functions to print the stack number and it actually varies between 238-242 but I believe it's meant to be 256
theoriginalbit #21
Posted 15 January 2013 - 08:47 PM
I was trying to point out that this topic is almost a year old. Also you can tell the max stack size by doing
But it was bumped today, and I saw it today :P/>

However I tried using some printing functions to print the stack number and it actually varies between 238-242 but I believe it's meant to be 256
Ahhh ok interesting…