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

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

Started by Treky123, 01 February 2013 - 09:33 AM
Treky123 #1
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!
GravityScore #2
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