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

Broken code

Started by matt2887, 23 October 2016 - 04:39 PM
matt2887 #1
Posted 23 October 2016 - 06:39 PM
I have attempted to make a program that will wait for rs input, and based on how many (1, 2 or 3 pulses) the program will turn on a rs signal up top for 5 seconds, then wait 10, 20 or 30 minutes to put a rs signal out the right side. Here is a pastebin link to the code. i am too noob for spoilers :P/>.
http://pastebin.com/gvhfdaUS

one snippit of code i'll post right here is this

elseif goldCoins == coinCount and coinCount == twenty then –if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 20 minute timer, This is where my code seems to error "" bios:14: [string "startup"]:50: '=' expected ""

this is line 50 of the code, it works all by itself, i made a test program that did

set locals for goldcoins, coincount and twenty to = 2

if goldCoins == coinCount and coinCount == twenty then
print("Twenty!")
end

and this code worked, so i don't know if something else in the code is being stupid, or if i am XD

thanks in advance,
a very confused CC noob
matt2887 #2
Posted 23 October 2016 - 06:58 PM
i fixed part of the problem by adding () to my function calls.. i forgot them. but now the code unexpectedly quits after you enter the number.
Dog #3
Posted 23 October 2016 - 07:16 PM
Keep in mind that read() returns a string, not a number, so you'll need to convert the user's input to a number before doing your comparisons. I would change your read line to be:

local goldCoins = tonumber(read())

EDIT:

Also, keep in mind that the way you have your loop constructed, you wait for one redstone pulse, then do your comparisons and break the loop if none of your comparisons matches. This means coinCount will only ever equal 1 each time you run the program.

EDIT 2:

You aren't using timers correctly. In order to use a timer you need to capture its unique identifier with a variable when you start it, then wait for timer events and look for a timer event that has the same identifier as the timer event you started. For example, you would replace your os.startTimer(600) with the following:

local myTimer = os.startTimer(600) --# start a timer and capture its unique identifier in a variable (myTimer)
while true do --# start an infinite loop
  local _, id = os.pullEvent("timer") --# pull timer events only and capture the unique identifier with the id variable (_ captures the event and is a throwaway)
  if id == myTimer then break end --# if the timer's idenfier matches our timer's identifier then break the loop
end
Edited on 23 October 2016 - 05:31 PM
matt2887 #4
Posted 23 October 2016 - 07:34 PM
Thanks so much for pointing that out, I do not know how to count the redstone pulses from the left store that info as coinCount and compare coinCount to the goldCoins value. am i making this code overly complicated? basically my program is intended to grant access to a room for a certain amount of time dependent on how much the person has paid me. 1 gold coin inserted into a chest will pulse the left side of the computer once. the 'customer' will need to insert 1, 2, or 3 coins to enter. 1 coin will give them a 5 second window to walk through the door, then start a 10 minute timer, after which a redstone pulse will go out the right side to kick them out of the room via command blocks. i thought that if i check for a redstone event and if there was one to increase coinCount by 1, then to check that against how many they said they'd put in(goldCoins) and check it against local variables for ten(1), twenty(2), and thirty(3). so that if they said 1 coin, and goldCoins, coinCount, and ten match it would start the function tenTimer().

is there a better way to do all this? how can I keep it checking for redstone signals until it reaches the amount set in the goldCoins variable that is set by the 'customer'?
matt2887 #5
Posted 23 October 2016 - 07:47 PM
I worked on a few things and wanted to know someone's input on this change for the code.

Spoiler

while true do
	  local event, p1 = os.pullEvent()

		if event == "redstone" then  --if redstone pulse
		  coinCount = coinCount + 1  --then add 1 to coinCount
  until coinCount == goldCoins
end
if goldCoins == ten then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 10 minute timer
  tenTimer()
elseif goldCoins == twenty then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 20 minute timer, This is where my code seems to error "" bios:14: [string "startup"]:50: '=' expected ""
  twentyTimer()
elseif goldCoins == thirty then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 30 minute timer
  thirtyTimer()
else  --if nothing is true then break
break
end

so the whole code would look like this.

Spoiler
[list]
[*]	[color=#333333]
[color=#AA9900][b]local[/b][/color] coinCount [color=#66CC66]=[/color] [color=#CC66CC]0[/color]  [color=#808080][i]--coins inserted[/i][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]function[/b][/color] tenTimer[color=#66CC66]([/color][color=#66CC66])[/color]  [color=#808080][i]--starts 10 min timer and then turns rs on right side for 1 sec then turns rs off[/i][/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"top"[/color][color=#66CC66],[/color] [color=#AA9900]true[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  sleep[color=#66CC66]([/color][color=#CC66CC]5[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"top"[/color][color=#66CC66],[/color] [color=#AA9900]false[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  [color=#0000AA]os[/color][color=#66CC66].[/color]startTimer[color=#66CC66]([/color][color=#CC66CC]600[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"right"[/color][color=#66CC66],[/color] [color=#AA9900]true[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  sleep[color=#66CC66]([/color][color=#CC66CC]1[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"right"[/color][color=#66CC66],[/color] [color=#AA9900]false[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]end[/b][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]function[/b][/color] twentyTimer[color=#66CC66]([/color][color=#66CC66])[/color]  [color=#808080][i]--starts 20 min timer and then turns rs on right side for 1 sec then turns rs off[/i][/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"top"[/color][color=#66CC66],[/color] [color=#AA9900]true[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  sleep[color=#66CC66]([/color][color=#CC66CC]5[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"top"[/color][color=#66CC66],[/color] [color=#AA9900]false[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  [color=#0000AA]os[/color][color=#66CC66].[/color]startTimer[color=#66CC66]([/color][color=#CC66CC]1200[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"right"[/color][color=#66CC66],[/color] [color=#AA9900]true[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  sleep[color=#66CC66]([/color][color=#CC66CC]1[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"right"[/color][color=#66CC66],[/color] [color=#AA9900]false[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]end[/b][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]function[/b][/color] thirtyTimer[color=#66CC66]([/color][color=#66CC66])[/color]  [color=#808080][i]--starts 30 min timer and then turns rs on right side for 1 sec then turns rs off[/i][/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"top"[/color][color=#66CC66],[/color] [color=#AA9900]true[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  sleep[color=#66CC66]([/color][color=#CC66CC]5[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"top"[/color][color=#66CC66],[/color] [color=#AA9900]false[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  [color=#0000AA]os[/color][color=#66CC66].[/color]startTimer[color=#66CC66]([/color][color=#CC66CC]1800[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"right"[/color][color=#66CC66],[/color] [color=#AA9900]true[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  sleep[color=#66CC66]([/color][color=#CC66CC]1[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
  rs[color=#66CC66].[/color]setOutput[color=#66CC66]([/color][color=#FF6666]"right"[/color][color=#66CC66],[/color] [color=#AA9900]false[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]end[/b][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]local[/b][/color] ten [color=#66CC66]=[/color] [color=#CC66CC]1[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]local[/b][/color] twenty [color=#66CC66]=[/color] [color=#CC66CC]2[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]local[/b][/color] thirty [color=#66CC66]=[/color] [color=#CC66CC]3[/color][/color]
[*]	[color=#333333]
[color=#0000AA]print[/color][color=#66CC66]([/color][color=#FF6666]"How Much Time Would You Like To Buy?"[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#0000AA]print[/color][color=#66CC66]([/color][color=#FF6666]"1 Gold Coin = 10 Minutes."[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#0000AA]print[/color][color=#66CC66]([/color][color=#FF6666]"You May ONLY Buy Up To 30 Minutes."[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#0000AA]print[/color][color=#66CC66]([/color][color=#FF6666]"Enter Number Of Gold Coins:"[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#0000AA]write[/color][color=#66CC66]([/color][color=#FF6666]"$"[/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]local[/b][/color] goldCoins [color=#66CC66]=[/color] [color=#0000AA]tonumber[/color][color=#66CC66]([/color][color=#0000AA]read[/color][color=#66CC66]([/color][color=#66CC66])[/color][color=#66CC66])[/color] [color=#808080][i]-- i want it to ask the user to input gold coins, quanitity of 1-3[/i][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]while[/b][/color] [color=#AA9900]true[/color] [color=#AA9900][b]do[/b][/color][/color]
[*]	[color=#333333]
	  [color=#AA9900][b]local[/b][/color] event[color=#66CC66],[/color] p1 [color=#66CC66]=[/color] [color=#0000AA]os[/color][color=#66CC66].[/color]pullEvent[color=#66CC66]([/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
		[color=#AA9900][b]if[/b][/color] event [color=#66CC66]==[/color] [color=#FF6666]"redstone"[/color] [color=#AA9900][b]then[/b][/color]  [color=#808080][i]--if redstone pulse[/i][/color][/color]
[*]	[color=#333333]
		  coinCount [color=#66CC66]=[/color] coinCount [color=#66CC66]+[/color] [color=#CC66CC]1[/color]  [color=#808080][i]--then add 1 to coinCount [/i][/color][/color]
[*]	[color=#333333]
		[color=#AA9900][b]until[/b][/color] coinCount [color=#66CC66]==[/color] goldCoins[/color]
[*]	[color=#333333]
		[color=#AA9900][b]end[/b][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]end[/b][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]if[/b][/color] goldCoins [color=#66CC66]==[/color] ten [color=#AA9900][b]then[/b][/color]  [color=#808080][i]--if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 10 minute timer[/i][/color][/color]
[*]	[color=#333333]
  tenTimer[color=#66CC66]([/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]elseif[/b][/color] goldCoins [color=#66CC66]==[/color] twenty [color=#AA9900][b]then[/b][/color]  [color=#808080][i]--if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 20 minute timer, This is where my code seems to error "" bios:14: [string "startup"]:50: '=' expected ""[/i][/color][/color]
[*]	[color=#333333]
  twentyTimer[color=#66CC66]([/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]elseif[/b][/color] goldCoins [color=#66CC66]==[/color] thirty [color=#AA9900][b]then[/b][/color]  [color=#808080][i]--if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 30 minute timer[/i][/color][/color]
[*]	[color=#333333]
  thirtyTimer[color=#66CC66]([/color][color=#66CC66])[/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]else[/b][/color]  [color=#808080][i]--if nothing is true then break[/i][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]break[/b][/color][/color]
[*]	[color=#333333]
[color=#AA9900][b]end[/b][/color][/color]
[/list]

I tested it out but it says I need another end at line 49 to close if statment starting on line 47. i added it and am still getting the same error.
http://pastebin.com/VDTMbNSr
Edited on 23 October 2016 - 05:55 PM
Dog #6
Posted 23 October 2016 - 07:49 PM
EDIT: OK, you replied while I was typing - let me address your code:

First thing, is your loop - you're looking for a repeat loop, not a while loop (lose the while true do and the end). Below is an example of a repeat loop.

repeat
  os.pullEvent("redstone") --# wait for redstone events
  coinCount = coinCount + 1 --# increment coinCount
until coinCount == goldCoins --# end loop when coinCount == goldCoins

Then instead of a comparison, you can do some easy math to start the timer…

beginTimer(coinCount * 600)

With beginTimer(x) you can eliminate your three timer functions and make a 'universal' timer function like so:

local function beginTimer(amount)
  rs.setOutput("top", true)
  sleep(5)
  rs.setOutput("top", false)
  local myTimer = os.startTimer(amount) --# start a timer for the amount of time specified, capture the timer's unique id with the variable myTimer
  repeat --# start an infinite loop
    local _, id = os.pullEvent("timer") --# pull only timer events
  until id == myTimer --# end the loop when our timer has fired
  rs.setOutput("right", true)
  sleep(1)
  rs.setOutput("right", false)
end


EDIT2: minor corrections, added comments, expanded some info


EDIT3: Apparently you edited your post after I edited mine - please re-read what I've posted - the latest code you posted in your previous comment is still not using timers correctly.
Edited on 23 October 2016 - 09:01 PM
matt2887 #7
Posted 24 October 2016 - 12:52 AM
Ran into another snag. I fixed most of it, but now if i tell it 1 coin and put one in it works, all code executes properly. if I tell it 2 coins and put one in, it turns on and gives me the 20 minute timer running the twentyTimer() function. if I say 3 and put in 2 coins it turns on and gives me 30 minute timer running the thirtyTimer() function. I've spent 1 hour trying to find out why and i'm super LOST. I cannot pastebin so here's the code.

Spoiler

term.clear()
term.setCursorPos(1,1)

local tenMin = 6
local twentyMin = 12
local thirtyMin = 18

local coinCount = 0  --coins inserted
function tenTimer()  --starts 10 min timer and then turns rs on right side for 1 sec then turns rs off
  rs.setOutput("top", true)
  sleep(5)
  rs.setOutput("top", false)
  sleep(tenMin)
  rs.setOutput("right", true)
  sleep(1)
  rs.setOutput("right", false)
end

function twentyTimer()  --starts 20 min timer and then turns rs on right side for 1 sec then turns rs off
  rs.setOutput("top", true)
  sleep(5)
  rs.setOutput("top", false)
  sleep(twentyMin)
  rs.setOutput("right", true)
  sleep(1)
  rs.setOutput("right", false)
end

function thirtyTimer()  --starts 30 min timer and then turns rs on right side for 1 sec then turns rs off
  rs.setOutput("top", true)
  sleep(5)
  rs.setOutput("top", false)
  sleep(thirtyMin)
  rs.setOutput("right", true)
  sleep(1)
  rs.setOutput("right", false)
end
local ten = 1 --sets value to match against coinCount to ensure the correct function above is called
local twenty = 2
local thirty = 3

print("How Much Time Would You Like To Buy?")
print("1 Gold Coin = 10 Minutes.")
print("You May ONLY Buy Up To 30 Minutes.")
print("Enter Number Of Gold Coins:")
write("Coins: ")

local goldCoins = tonumber(read()) --asks user for input of how many coins they will deposit

repeat
	os.pullEvent("redstone") --# wait for redstone events
	coinCount = coinCount + 1
  until coinCount == goldCoins

term.clear()
term.setCursorPos(1,1)
print("Thank You!")
term.setCursorPos(1,2)
print("Please Enjoy!")
term.setCursorPos(1,3)
print("You Have 5 Seconds To Enter!")
term.setCursorPos(1,4)
print("You Have: ")
print(10*coinCount.." Minutes!") --this prints coinCount, times 10 to tell the user the amount of minutes they have. this works and if i put 1 for goldCoins value i get a print saying 10 minutes, if i put 2 i get 20 minutes.

if coinCount == thirty then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 30 minute timer
  thirtyTimer()
elseif coinCount == twenty then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 20 minute timer
elseif coinCount == ten then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 10 minute timer
  tenTimer()
else
  os.reboot()
end
term.clear()
term.setCursorPos(1,1)
print("Thank You! Come again.")
  for i = 1, 5 do
	print("Rebooting in: "..i)
	sleep(1)
  end
os.reboot()

EDIT: I checked the value of coinCount because i thought the os.pullevent("redstone") was counting up twice, once for rs turning on, and once for it turning off. if this is the case it's not being reflected in coinCount value.

EDIT2: I played around with your code you posted. Worked beautifully like mine, except same issue as with the other code, I like yours though, much nicer and cleaner :)/>
Here's what i have:
Spoiler

term.clear()
term.setCursorPos(1,1)

local tenMin = 6
local twentyMin = 12
local thirtyMin = 18

local coinCount = 0  --coins inserted

local function beginTimer(amount)
  rs.setOutput("top", true)
  sleep(5)
  rs.setOutput("top", false)
  local myTimer = os.startTimer(amount) --# start a timer for the amount of time specified, capture the timer's unique id with the variable myTimer
  repeat --# start an infinite loop
    local _, id = os.pullEvent("timer") --# pull only timer events
  until id == myTimer --# end the loop when our timer has fired
  rs.setOutput("right", true)
  sleep(1)
  rs.setOutput("right", false)
end

local ten = 1 --sets value to match against coinCount to ensure the correct function above is called
local twenty = 2
local thirty = 3

print("How Much Time Would You Like To Buy?")
print("1 Gold Coin = 10 Minutes.")
print("You May ONLY Buy Up To 30 Minutes.")
print("Enter Number Of Gold Coins:")
write("Coins: ")

local goldCoins = tonumber(read()) --asks user for input of how many coins they will deposit

repeat
    os.pullEvent("redstone") --# wait for redstone events
    coinCount = coinCount + 1
  until coinCount == goldCoins
  term.clear()
term.setCursorPos(1,1)
print("Thank You!")
term.setCursorPos(1,2)
print("Please Enjoy!")
term.setCursorPos(1,3)
print("You Have 5 Seconds To Enter!")
term.setCursorPos(1,4)
print("You Have: ")
print(10*coinCount.." Minutes!") --this prints coinCount, times 10 to tell the user the amount of minutes they have. this works and if i put 1 for goldCoins value i get a print saying 10 minutes, if i put 2 i get 20 minutes.
beginTimer(coinCount * 6)
term.clear()
term.setCursorPos(1,1)
print("Thank You!")
term.setCursorPos(1,2)
print("Please Enjoy!")
term.setCursorPos(1,3)
print("You Have 5 Seconds To Enter!")
term.setCursorPos(1,4)
print("You Have: ")
print(10*coinCount.." Minutes!") --this prints coinCount, times 10 to tell the user the amount of minutes they have. this works and if i put 1 for goldCoins value i get a print saying 10 minutes, if i put 2 i get 20 minutes.
term.clear()
term.setCursorPos(1,1)
print("Thank You! Come again.")
  for i = 1, 5 do
    print("Rebooting in: "..i)
    sleep(1)
  end
os.reboot()
Edited on 23 October 2016 - 11:20 PM
Dog #8
Posted 24 October 2016 - 01:26 AM
Is this the *exact* code you're running? I ask because I don't see the call to twentyTimer() and I can't see anything that would cause the program to behave the way you describe.
matt2887 #9
Posted 24 October 2016 - 01:47 AM
I tried both codes, and they do the same thing.
Spoiler

function twentyTimer()  --starts 20 min timer and then turns rs on right side for 1 sec then turns rs off
  rs.setOutput("top", true)
  sleep(5)
  rs.setOutput("top", false)
  sleep(twentyMin)
  rs.setOutput("right", true)
  sleep(1)
  rs.setOutput("right", false)
end

it's one of the functions of the first code in the first spoiler at the top of the code.
Edited on 24 October 2016 - 12:20 AM
Bomb Bloke #10
Posted 24 October 2016 - 02:46 AM
That's your definition for twentyTimer(). Nowhere in your code do you call that function.

if coinCount == thirty then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 30 minute timer
  thirtyTimer()
elseif coinCount == twenty then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 20 minute timer
  --# Are you missing something here, perhaps?
elseif coinCount == ten then  --if coinCount, coins they inserted, is equal to goldCoins, how many they said, then start 10 minute timer
  tenTimer()
else
  os.reboot()
end
Edited on 24 October 2016 - 12:46 AM
matt2887 #11
Posted 24 October 2016 - 04:27 AM
Right, that would have been a problem, but I'm not using that code anymore. I'm now using this code.
Spoiler

term.clear()
term.setCursorPos(1,1)

local coinCount = 0  --coins inserted

local function beginTimer(amount)
  rs.setOutput("top", true)
  sleep(5)
  rs.setOutput("top", false)
  local myTimer = os.startTimer(amount) --# start a timer for the amount of time specified, capture the timer's unique id with the variable myTimer
  repeat --# start an infinite loop
	local _, id = os.pullEvent("timer") --# pull only timer events
  until id == myTimer --# end the loop when our timer has fired
  rs.setOutput("right", true)
  sleep(1)
  rs.setOutput("right", false)
end

local ten = 1 --sets value to match against coinCount to ensure the correct function above is called
local twenty = 2
local thirty = 3

print("How Much Time Would You Like To Buy?")
print("1 Gold Coin = 10 Minutes.")
print("You May ONLY Buy Up To 30 Minutes.")
print("Enter Number Of Gold Coins:")
write("Coins: ")

local goldCoins = tonumber(read()) --asks user for input of how many coins they will deposit

repeat
  os.pullEvent("redstone") --# wait for redstone events
  coinCount = coinCount + 1
until coinCount == goldCoins
  term.clear()

term.setCursorPos(1,1)
print("Thank You!")
term.setCursorPos(1,2)
print("Please Enjoy!")
term.setCursorPos(1,3)
print("You Have 5 Seconds To Enter!")
term.setCursorPos(1,4)
print("You Have: ")
print(10*coinCount.." Minutes!") --this prints coinCount, times 10 to tell the user the amount of minutes they have. this works and if i put 1 for goldCoins value i get a print saying 10 minutes, if i put 2 i get 20 minutes.

beginTimer(coinCount * 6)

term.clear()
term.setCursorPos(1,1)
print("Thank You!")
term.setCursorPos(1,2)
print("Please Enjoy!")
term.setCursorPos(1,3)
print("You Have 5 Seconds To Enter!")
term.setCursorPos(1,4)
print("You Have: ")
print(10*coinCount.." Minutes!") --this prints coinCount, times 10 to tell the user the amount of minutes they have. this works and if i put 1 for goldCoins value i get a print saying 10 minutes, if i put 2 i get 20 minutes.

term.clear()
term.setCursorPos(1,1)
print("Thank You! Come again.")

  for i = 1, 5 do
	print("Rebooting in: "..i)
	sleep(1)
  end
os.reboot()
EDIT:
After a quick test with a lever next to the computer it would seem as though when I tell it I will deposit 2 coins, I flick the lever on, it doesn't proceed. and it shouldn't. i flick the lever off. it goes on with the rest of the code. so it looks like even though it's not updating the coinCount as 2 events happened, it is registering 2 events and thinks it's ok to go forward. I will test further by hooking it back up to the source and putting a toggle latch on my redstone output of my detection system.

EDIT2:
After testing with a toggle latch it works as intended. The code is perfect. My setup is this: I have an input chest they store the item. I have a pipe taking out 1 item at a time and piping it into a secondary chest. That chest has a comparator hooked up to it and a redstone engine powered by a lever being used with a wooden transport pipe to take the item out of the chest. When the item enters the chest and leaves it pulses the comparator. Because the signal is soo weak I have a pulse former take the tiny signal from the comparator and create a strong pulse that is sent down a wire to the computer on the left side. once the correct number of pulses happens it triggers the rest of the code which turns on a redstone signal from the top, starts a timer and when the timer ends, triggers a redstone pulse from the right. the 5 second redstone signal from the top is to open the door to allow the player access to the room, and the timer is for how long they can use the room, 10 minutes per coin up to 30 minutes, then the redstone pulse out of the right side signals a command block to teleport them out. I have not set up the command block yet but once I do I will update this post in case anyone else want's to use this. This is for access to a machine room I built on a server where i'm an admin. Players can buy a gold coin from thaumcraft from one of our shops and it takes 1 coin per 10 minutes for use of the room. I saw it done once before and it was an idea I loved. Thanks again everyone!
Edited on 24 October 2016 - 03:02 AM
Dog #12
Posted 24 October 2016 - 04:54 AM
It's been awhile since I've actually played with redstone, but if you're getting two events (one for redstone ON and one for redstone OFF) then simply increment coinCount by 0.5 instead of 1 for each redstone event - that should solve the problem.