I re-read this passage in the Scripting Tutorial, and I think I found out why I had difficulties understanding the script @Galapagoose kindly provided above:
While the scripting tutorial explains that input[1].change creates a change event if the input changes, and that this change event calls a function, it does not mention that the state of the change is also transmitted.
As far as I believe to understand it now, if input[X].mode is set to either falling or rising, the state of input[1].change will always be true. But in case of mode both, the input[1].change will be true for the rising edge, and false for the falling edge.
Of course I am biased by my own experience, but I think it would be helpful for beginners like me to mention this state thingy in the tutorial. I believe this could be achieved by two edits to this part:
EDIT 1 Print the state instead of “Bang!” (it is already handed over to the function):
input[1].change = function(state)
print(state)
end
EDIT 2 Replace this sentence
Those BANG! s tell us the input is correctly setup and detecting the clock signal.
with something like this:
"These true messages tell us the input is correctly setup and detecting the clock signal.
But where does this true come from? Each time change event is triggered, it not only calls the function, but also hands over the state of the input change to the function. This is called the argument of a function, and as we gave it a name (state), the print command inside the function can print this argument. We could have given it any name we like, but state seemed like the right one.
Please note that for the input modes rising and falling the state is always true, whereas for the input mode boththe state is true for the rising edge and false for the falling edge."
I hope my proposal does not come across as preposterous or impertinent, if it does, please accept my apologies. I just feel this could really help absolute beginners like me. Thanks for listening!