Jump to content




[Lua] How to make a computer send out a user defined number of red-stone pulses


  • You cannot reply to this topic
1 reply to this topic

#1 Treky123

  • Members
  • 3 posts

Posted 01 February 2013 - 10:33 AM

Title: [Lua] How to make a computer send out a user defined number of red-stone pulses


Hello, I am using Computer craft computer and red power, to make an "item vending machine", it is not meant to be comprehensive, just for the important items (iron, coal, diamonds, etc...). The way I have been coding it to write a separate program for each item, like this:
----
print ("how many would you like")
n = read()
rs.setBundledOutput("bottom", colors.purple)
sleep(n) -- place holder for were I want it to pulse, with n being the user defined number of pulses
rs.setBundledOutput("bottom", 0)
print ("thank you, enjoy!")
sleep(2)
term.clear()
----

does someone know how I can have the user enter a number (n) and then have the red stone pulse that many time. I apologize if this is a simplistic question but I am very new to Lua, and fairly new to coding in general, thanks!

#2 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 01 February 2013 - 01:55 PM

You can use a for loop to repeat the same piece of code for n number of times, and in that for loop turn the redstone on and off again.

Put this in your placeholder:
for i = 1, tonumber(n) do
  rs.setOutput("bottom", true)
  sleep(0.5)
  rs.setOutput("bottom", false)
  sleep(0.5)
end
This is just using normal redstone, but you can change it to support bundled cables.

Just note this code will error if n is not a number, so you might want to do some checking before that. tonumber will return nil if the thing you are tonumbering contains letters, so you could do this:
local a = tonumber(n)
if a == nil then
  print("NOT A DAMN NUMBER!")
else
  print("yay! you typed a number! congratz to you!")
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users