Search code examples
cmatrixarduinoarduino-idearduino-esp8266

Controlled On and Off in a Led-Matrix And Push-Button


Sir, I am using a 3*3 led -matrix with 3*3 push-button, and I need a program that should light up the top left-led and wait for the user's response. If the corresponding button is pressed the led turn's off and the immediate adjacent led lights up and the process continues. This code works for this, the problem is when the user press the 1st button the 1st led goes off and the 2nd led goes on and when 2nd push button is pressed the 2nd led goes off and 3rd led goes off BUT when the user mistakenly touches the 2nd push-button again in the middle of the process the random led starts glowing which should not happen the code should wait till the correct push-button is pressed and the process continues, can anyone help me with the code. Thanks.

#include <Keypad.h>

int led_rows[]={ 2 , 3 , 4 };
int led_cols[]={ 5 , 6 , 7 };

int led_matriz[3][3]= {     
    {0, 0, 0}, 
    {0, 0, 0},  
    {0, 0, 0}, };

const byte rows = 3; 
const byte cols = 3; 

char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
};

byte rowPins[rows] = {10,9,8}; 
byte colPins[cols] = {13,12,11}; 

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

void setup(){
  Serial.begin(9600);

  for(int i=0;i<3;i++){
  pinMode(led_cols[i], OUTPUT);
  digitalWrite (led_cols[i], HIGH);
  }

  for(int i=0;i<3;i++){
  pinMode(led_rows[i], OUTPUT);
  digitalWrite (led_rows[i], LOW);
  }
  Led1_On();
}

void Led1_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[0], LOW);
}

void Led1_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[0], HIGH);
}

void Led2_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[1], LOW);
}

void Led2_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[1], HIGH);
}

void Led3_On(){
  digitalWrite (led_rows[0], HIGH);
  digitalWrite (led_cols[2], LOW);
}

void Led3_Off(){
  digitalWrite (led_rows[0], LOW);
  digitalWrite (led_cols[2], HIGH);
}

void Led4_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[0], LOW);
}

void Led4_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[0], HIGH);
}

void Led5_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[1], LOW);
}

void Led5_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[1], HIGH);
}

void Led6_On(){
  digitalWrite (led_rows[1], HIGH);
  digitalWrite (led_cols[2], LOW);
}

void Led6_Off(){
  digitalWrite (led_rows[1], LOW);
  digitalWrite (led_cols[2], HIGH);
}

void Led7_On(){
  digitalWrite (led_rows[2], HIGH);
  digitalWrite (led_cols[0], LOW);
}

void Led7_Off(){
  digitalWrite (led_rows[2], LOW);
  digitalWrite (led_cols[0], HIGH);
}

void Led8_On(){
  digitalWrite (led_rows[2], HIGH);
  digitalWrite (led_cols[1], LOW);
}

void Led8_Off(){
  digitalWrite (led_rows[2], LOW);
  digitalWrite (led_cols[1], HIGH);
}

void Led9_On(){
  digitalWrite (led_rows[2], HIGH);
  digitalWrite (led_cols[2], LOW);
}

void Led9_Off(){
  digitalWrite (led_rows[2], LOW);
  digitalWrite (led_cols[2], HIGH);
}

void loop(){
  char key = keypad.getKey();
  //Led1_On();

  switch(key)
  {
    case '1' :
    Led1_Off();
    Led2_On();
    break;

    case '2' :
    Led2_Off();
    Led3_On();
    break;

    case '3':
    Led3_Off();
    Led4_On();
    break;

    case '4':
    Led4_Off();
    Led5_On();
    break;

    case '5' :
    Led5_Off();
    Led6_On();
    break;

    case '6' :
    Led6_Off();
    Led7_On();
    break;

    case '7' :
    Led7_Off();
    Led8_On();
    break;

    case '8' :
    Led8_Off();
    Led9_On();
    break;

    case '9' :
    Led9_Off();

  }
}

Solution

  • You need to check that the user only presses the expected key.
    For that you need to keep track of which key is expected and check it, too.

    Start by setting up a global variable with the initially expected key.

    char expected = '1';
    

    Then put your switch statement into a conditional, only to execute if the condition is met.

    if (key==expected)
    {
        switch(key)
        {
    

    Then, inside each of the switch cases, update the expected key to the next one.

            case '1' :
                expected = '2';
                Led1_Off();
                Led2_On();
                break;