1

Example: If the user presses the button sequence 1-2-2-1 then the LEDs light up in the order 1-2-2-1

int userAmount[4];
const int ledPins[] = {4, 5};
const int btnPins[] = {8, 9};
    
int buttonState_1 = 0;
    
void setup() {
  Serial.begin(9600);
    for (int n = 0; n < 4; n++)
     {
    
     }
     pinMode(PIN_BUTTON_1, INPUT_PULLUP);
}
    
void loop() {
  buttonState_1 = digitalRead(PIN_BUTTON_1);  
   if (buttonState_1 == LOW)
      {
        for (int i = 0; i < 4; i++)
        {
    
        }
      }
}
2
  • Ehm, and what exactly is your problem? You didn't ask a question. If you don't know how to go further, it often helps to first lay out the logic of your desired program on paper as pseudo code. Nothing with real code at that point. That might start like: "Check buttons 1 and 2. If either presses, save press in variable. Repeat, until buttons where pressed 4 times ...". After that you can start converting to real Arduino code. Pseudo code can help a lot, especially, when you are not yet fluent in C/C++. – chrisl Mar 28 at 21:19
  • record the button transitions ... those occur only once when a button is pressed and once then the button is released ... if you only record digitalRead(), then you will get one record every time loop() iterates, which can be 10000 times per second – jsotola Mar 28 at 21:40
0

Start by writing a sketch that debounces your buttons to get an accurate count of each button press. Install the Bounce2 Library available in the IDE.

Use a byte array to store the sequence of button presses (record the pin numbers).

Once the pre-set limit (sequence length) is reached, play back the sequence.

Playing back the sequence could look something like this:

const byte ledPin1 = 4;
const byte ledPin2 = 5;
const byte sequenceLength = 4;
byte sequence[sequenceLength] = {4, 5, 5, 4};

void setup(){

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);

  for(byte i = 0; i < sequenceLength; i++){
    if(sequence[i] == ledPin1){
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
    }
    else{
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
    }
    delay(500);
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    delay(500);
  }

}

void loop(){}

EDIT: Adding a couple of buttons using the Bounce2 library is a trivial task.

// Arduino IDE 1.8.9
// Sketch uses 1696 bytes (5%) of program storage space.
// Global variables use 30 bytes (1%) of dynamic memory,
#include <Bounce2.h>
const byte ledPin1 = 4;
const byte ledPin2 = 5;
const byte buttonPin1 = 8;
const byte buttonPin2 = 9;
const byte sequenceLength = 4;
byte sequence[sequenceLength];
byte counter = 0;

Bounce button1 = Bounce();
Bounce button2 = Bounce();

void setup(){

  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, LOW);

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);

  button1.attach(buttonPin1);
  button1.interval(50);
  button2.attach(buttonPin2);
  button2.interval(50);

}

void loop(){

  if(button1.update()){
    if(button1.read() == 0){
      addToSequence(ledPin1);
    }
  }

  if(button2.update()){
    if(button2.read() == 0){
      addToSequence(ledPin2);
    }
  }

  if(counter == sequenceLength){
    for(byte i = 0; i < sequenceLength; i++){
      if(sequence[i] == ledPin1){
        digitalWrite(ledPin1, HIGH);
        digitalWrite(ledPin2, LOW);
      }
      else{
        digitalWrite(ledPin1, LOW);
        digitalWrite(ledPin2, HIGH);
      }
      delay(500);
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      delay(500);
    }
    counter = 0;
  }
}

void addToSequence(byte pinNumber){
  sequence[counter] = pinNumber;
  counter++;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.