I have three buttons and I want to have different patterns that I can enter to get different results. For example, if I hit the button one twice, then hit bottom two it would flash a light, but if I hit button one three times then hit button three once it would do something else. I also cannot use the delays because it will mess up my other code.
I thought about using an array to store the sequence, but that is where it got over my head.
Hi, you could imagine your buttons are 1, 2 & 3 on a keypad. Each time a button is pressed, multiply the previous result by 10 and add the new value. Each sequence then becomes a unique number like 1132 or 1113. You can then use a case/switch in your code to perform the actions for each value.
PaulRB:
Hi, you could imagine your buttons are 1, 2 & 3 on a keypad. Each time a button is pressed, multiply the previous result by 10 and add the new value. Each sequence then becomes a unique number like 1132 or 1113. You can then use a case/switch in your code to perform the actions for each value.
That sounds like a good idea but I don't see how it could give the numbers you have listed.
1032 would represent 3 presses of 1, 2 presses of 3 and 1 press of 2.
However it would not record the order of presses and an array would be needed for that with an auto-increasing index so that each successive button press was saved into the next location. Something very roughly like
for (byte n = 1; n < numKeys; n ++) {
buttonVal = digitalRead(buttonPins[n]);
if (buttonVal == HIGH) {
buttonArray[index] = n;
}
index ++;
}
By the way, this snippet only stores a single keypress and will need to be repeated as necessary. It also assumes only 1 key is pressed at any one time.
Four presses in any sequence, one at a time. The method suggested would produce a 4-digit code that represents both the order of presses and the button number. Additionally, the code could be cleared if the time elapsed since last press > x seconds. Only one variable is required to store the code.