Problem with LED based Game

Hello mighty swarm intelligence,

i'm working on a LED based puzzle game. in my head it was straight forward and not to hard. so i thought :smiley: by now i started four times over and tried 3 different approaches which all got me stuck on the same issues. so here we go.

first the game experience for a player. the player will have three LEDs (blue, white, red) in a row. there will be 4 rows in total and a 1,2,3,4 keypad matrix for input. the game starts at the top with the first row. randomly the 3 LEDs will each show one of the following states (on, blink slow, blink fast or off). then the player hast to check a diagram to see which of the four numbers he has to press for the first row. the game will then move to next row until all four rows are solved right or the player made a mistake. if a wrong input is given the game starts with the first row and a new random combination.

so now to the part i was trying. i started off by creating to void commands for the blink slow and fast with a fsm each so i could do it without using delay. so far so good. also got a random selection for the LED states working. but i get stuck when i was trying to store the information so i could compare it later on to the right user inputs. my current view is to make a bunch of arrays that store values for each LED in a row. like which LED and State. But i lost my self on the way and i think i over complicated it each time i started over with unnecessary variables which could be avoided. Also in one Version my random function over wrote the state of the LED before because they shared the same variable. Yeah, so for now my mind is in game over mode until someone can help me out here.

a will add the code below. which will have the basic functions for the blinking and a messed up mode selection. which is still missing the storing part. the loop is a mess i think and is also in a state to let me see what the LEDs are doing.

int p = 9;  //LED Pins (9,10,11)
int x = 1;

int m = 0;  //Modus 1=an, 2=blinkenSchnell, 3=blinkenLangsam
byte mod[] = { 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3};

int a = 0;
byte anAus[] = {0, 0, 255, 0, 0, 255, 0, 0, 255, 255, 0, 255, 0, 0, 0, 255, 0, 255, 0, 255}; // 20 Werte 


void setup() {
  Serial.begin(9600);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop() {
 if(x==1){ 
  a = anAus[random(1,15)];
  m = mod[random(1,20)];
  if(a == 0){
    m = 0;
  }
  else
    x=0;
  Serial.println(p);
  Serial.println(m);
  Serial.println(a);
 }
  modus();

}
void modus(){
  if(a == 255){
    if(m == 1){
      analogWrite(p, 255);
    }
    if(m == 2){
      blinkFast();
    }
    if(m == 3){
      blinkSlow();
    }
  }
  
  
}

void blinkFast(){
  static int state = 30; 
  static unsigned long ts;  // To store the "current" time in for delays.

  switch(state)
  {
    case 30:
      analogWrite(p, 255);
      ts = millis();
      state = 31;
      break;
    case 31:
     if(millis() > ts + 100)
      {
        state = 32;
      }
      break;
    case 32:
      analogWrite(p, 0);
      ts = millis();
      state = 33;
      break;
    case 33:
      if(millis() > ts + 100)
      {
        state = 30;
      }
      break;
    default:
      state = 30;
      break;
  }
}

void blinkSlow(){
  static int state = 40; 
  static unsigned long tss;  // To store the "current" time in for delays.

  switch(state)
  {
    case 40:
      analogWrite(p, 255);
      tss = millis();
      state = 41;
      break;
    case 41:
     if(millis() > tss + 1000)
      {
        state = 42;
      }
      break;
    case 42:
      analogWrite(p, 0);
      tss = millis();
      state = 43;
      break;
    case 43:
      if(millis() > tss + 1000)
      {
        state = 40;
      }
      break;
    default:
      state = 40;
      break;
  }
}

help would be really great :smiley: and sorry for typos i'm not a native speaker.

but i get stuck when i was trying to store the information so i could compare it later on

What information do you think you need to store? You generated a random number to choose what to display on a row. That is the only piece of information that I see that you need to store. 4 bytes isn't really going to take up a lot of space.

void loop() {
 if(x==1){

I can't even begin to guess what x means, or why is it the basis for what the Arduino does over and over. Good variable names are very important.

Hello Paul,

yeah forget the loop part for the moment.

what i think i need is to know which led is in which state in a row. or the smarter way would be to do it for a row with every possible combination. but thats the part im stuck with. maybe my brain is just over worked on this project.