Random num generator

I am trying to generate a random number when a switch is hit (connected to pin 12). That random number is then used as an index in an array of strings, which is in an if statement. However, I only want it to execute once when the switch goes from high to low. Currently when it switches to low, it keeps generating random numbers.

sample code:

If (pin12 == LOW){

r = random(0,4);

char sample[5][4] = {"Test", "Tested", "Once", "Five", "Seven"}
char x = sample[r];

}

What happens now is "x"keeps cycling through. I want one random number to be generated and then it waits till the switch goes from HIGH to LOW to generate another random number.

Below the 'if' statement store 'pin12' in a global variable. This will be the previous value of 'pin12' for the next loop. In the 'if', check that 'pin12' is both == LOW and != the previous value.

char sample[5][4] = {"Test", "Tested", "Once", "Five", "Seven"}This won't work. Four bytes stores at most a 3 character string.

In addition to the other items mentioned, there are two more problems. The random number will be 0, 1, 2, or 3. The function, with an upper limit of 4, will never return a 4.

Second, you are trying to assign an array to x, which is a scalar. That will never work.

Also, you almost certainly want to look at the state change detection example, since you want to assign the data when the switch BECOMES pressed, not when the switch IS pressed.

Oh, and you need to post ALL of your code AND tell us hoe the switch IS wired.

Sorry for the crappy example code.

Johnwasser, thanks for the info, that helped me figure it out!!

All, thanks for taking some time to provide feedback/answers!