how to create a system which reads button presses

im trying to create a system which goes something like this.

user pushes button 1 (start button) led1 flashes for a random number of times (3-6)then stops user then uses button2 and presses the same amount of times. after user inputs they press button3 sort of like an enter button if correct green led flashes then new sequence is created (repeat) if incorrect red led flashes and user must try again.

im having difficulty in figuring out how to 1 make the led flash a random amount of times then stop 2 making the arduino figure out if the user has presses the right amount of times.

so far this is what im working with

const int ledFlash = 7; // flashing LED const int ledCorrect = 13; // correct LED const int ledIncorrect = 12; // incorrect LED const int buttonStart = 2; // start button const int buttonPress = 4; // User Input Button const int buttonEnter = 8; // Enter Button

int buttonState = 0; int nFlashes = 0; // number of flashes int nPresses = 0; // number of presses int lastPressCount = 0; // how many times buttonPress was pressed. int randNumber = 0; // random number

void setup() { pinMode(ledFlash, OUTPUT); pinMode(ledCorrect, OUTPUT); pinMode(ledIncorrect, OUTPUT); pinMode(buttonStart, INPUT); pinMode(buttonPress, INPUT); pinMode(buttonEnter, INPUT); }

void loop() { buttonState = digitalRead(buttonStart); if(buttonState == HIGH) { digitalWrite(ledFlash, HIGH); } else { digitalWrite(ledFlash, LOW); } }

Ok, simple. You can use the random() and randomSeed() functions to get the LEDs to blink.

Here is a simple button counter code.

const byte LED = 6;
const byte Button = 2;
byte ButtonState;
byte last = LOW;
int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  ButtonState = digitalRead(Button);
  
  if(ButtonState && ButtonState != last) {
    analogWrite(LED, count += 5);// every time the button is pressed, count increases by 5. This can be changed to inc by 1
    Serial.println(count);
  }
  
  if(count > 255){
    digitalWrite(LED, LOW);
    count = 0;
  }
  
  last = ButtonState;
}

If you use a blocking design then a sequence of flashes just needs a FOR loop with a pair of calls to turn the LED on and off with some delays in between.

Look at the state change detection example to see how to detect when a button has been pressed and released. You would need to count the button press events until the 'enter' button was pressed.