A program that stores 4 button presses in an array and then plays them on the LEDs.
Example: If the user presses the button sequence 1-2-2-1,
then the LEDs light up in the order 1-2-2-1.
Personally I find this very difficult to do, so if anyone can give me some leads that would be great.
Off the top of my head, and I don't have time to do any coding, this would as a start keep track of the four pressed buttons
- Have a look at the state change detect example in the ide to see how to detect a button has become pressed
- Put all the button pins in an array
- Put all the led pins in an array
- Have another array to hold the array index of each button that's newly pressed
- Have a counter set to 0 initially, to count how many presses there have been
- Continually monitor all the buttons (for loop across the button array)
- When you detect a button is newly pressed store its array index in the presses array, using the counter value as the index into that array
- Increment the counter, and when it's 3 (that would be 4 presses since arrays start at 0) you're done with that part
- Then use the values in the presses array to be the indices into the led array to access the led pins and turn them on
- You need some way to start over.... perhaps when you get a 5th button press, that counts as a new 1st press, and turn the leds all off, zero the counter and go again
Very off the top of my head, that.
What @void-basil said plus, familiarize yourself with arrays.
For state change , debouncing, etc. get to know the first five demos in IDE -> file/examples/digital.
consider
byte pinsLed [] = { 10, 11, 12 };
byte pinsBut [] = { A1, A2, A3 };
#define N_BUT sizeof(pinsBut)
enum { Off = HIGH, On = LOW };
#define LIST_SIZE 4
byte butList [LIST_SIZE] = {};
unsigned butIdx = 0;
char s [80];
// -----------------------------------------------------------------------------
void setup ()
{
Serial.begin (115200);
for (unsigned n = 0; n < N_BUT; n++) {
digitalWrite (pinsLed [n], Off);
pinMode (pinsLed [n], OUTPUT);
pinMode (pinsBut [n], INPUT_PULLUP);
}
}
// -----------------------------------------------------------------------------
void
chkButtons (void)
{
static byte butSt [N_BUT] = {};
for (unsigned n = 0; n < N_BUT; n++) {
byte but = digitalRead (pinsBut [n]);
if (butSt [n] != but) {
butSt [n] = but;
if (On == but) {
butList [butIdx++] = n;
#if 0
sprintf (s, "%s: %d %d", __func__, butIdx, butList [butIdx-1]);
Serial.println (s);
#endif
}
}
delay (20);
}
}
// -------------------------------------
void loop ()
{
if (LIST_SIZE > butIdx)
chkButtons ();
else {
for (unsigned i = 0; i < butIdx; i++) {
#if 0
sprintf (s, "%s: i %d, list %d pin %d",
__func__, i, butList [i], pinsLed [ butList [i]]);
Serial.println (s);
#endif
digitalWrite (pinsLed [ butList [i]], On);
delay (500);
digitalWrite (pinsLed [ butList [i]], Off);
delay (500);
}
butIdx = 0;
}
}
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.