Keypad & Laser & Laser Dector sensor

Dear all,

I’m a new one regarding Arduino coding and as I am trying to implement a project I need your help.

I want to make a Basketball Game. The game is going to be consisted from 5 basketball baskets. Under

each basket a laser beam will going through and each time the ball goes in the basket, the laser beam

will be interrupted and accordingly the points will be added (I am going to add an OLED screen in the

future).

Furthermore. I have include a 4x4 keypad where the users will be able to choose

  1. NOs of players
  2. Change player
  3. Total score

As you can see from my code, when player 1 is selected the loop for Laser detector starts. My problem is that I cannot exit the loop. I have used a "“while” loop and a “bool” expression inside the “switch…case” (in order to exit the loop) but it doesn’t work.

Any help/advice would be appreciated

Nick

#include <Keypad.h>

int relay = 10;
int receiver1 = 11;
int receiver2 = 12;
int R1;
int R2;
int score=0;
int x=1;
int y=3;

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup(){
  
Serial.begin(9600);
pinMode(10,OUTPUT);
pinMode(11,INPUT);
pinMode (12, INPUT);

Serial.println ("*****BASKETBALL GAME*****\n");
Start_menu();

}

void loop (){

digitalWrite(10,HIGH); // Turns the relay ON

bool z=true;
char key = keypad.getKey();
  
if (key){
 switch(key){
  case '1':
  Serial.println("Player 1");
  while (z==true){
  player(1);
  if (key)
  z=stop_loop(key);
  }
   break;
   
  case '2':
  Serial.println("Player 2");
  while (int z=true){
  player(2);}
    break;
    
  case '3':
  Serial.println("Player 3");
    break;
  
  case 'A':                               // Shows the total score of all players
  Serial.println("Total Score");
    break;
  
  case 'B':                               // You can change player
  Serial.println ("Change Player");
  Start_menu();
    break;
    
    }
  } 
} 
 

void Start_menu() {
Serial.println("Press 1....9 to choose player\n");
}

void player(int i){
  
R1=digitalRead(11); // 1st Receiver               // val=1 while the beam  is unobtructed
                                                  // val=0 while the beam if obstructed
R2=digitalRead(12); //2nd Receiver

  if (R1==0) {
Serial.print("Player ");
Serial.print(i);
Serial.print(" = ");
Serial.println(score+=x); 
}

  if (R2==0) {
Serial.print ("Player ");
Serial.print(i);
Serial.print(" = ");
Serial.println (score+=y);
}
delay (150);
}

bool stop_loop(char keyy){
if(keyy=='D'){
  return false;
}else {
return true;
  }
}

Apart from any other problems that are lurking waiting to be found, this variable named z

        while (z == true)

is not the same as this variable named z

        while (int z = true)

Is that what you intended ?

  while (z==true){
  player(1);
  if (key)
  z=stop_loop(key);
  }

You already KNOW that key has a value ('1'), so it makes no sense to test that it has a value again. You already KNOW that stop_loop() is going to return true (since key is not 'D'), so calling the function is rather pointless.

So, what you really have is

  while (z==true)
  {
    player(1);
    z=true;
  }

How is THAT loop ever going to end?

Thank you for your answers!!! I can see my mistake but how could I exit the loop?

I can see my mistake but how could I exit the loop?

Step 1 - define the condition(s) that should be met in order to exit the loop Step 2 - test for that condition/those conditions being true

That might, or might not, involve reading the keypad again. I don't know. It's not my project.

I want to exit the loop as soon as the button "D" is pressed but I don't know hao should I modify my code :/

I want to exit the loop as soon as the button "D" is pressed but I don't know hao should I modify my code :/

I want to end this thread when you use the word elephant in your reply. But, I don't intend to actually read any more of your replies. How can this thread ever end?

Don't you think that you need to call getKey() in the body of the while loop? Don't you think that it is the new value, if any, from getKey() that should be passed to stop_loop()?

  while (z)
  {
    player(1);
    key = keypad.getKey(); // Read the keypad again (and again and again)
    if (key)
    z=stop_loop(key);
  }

The == true bit really isn't needed, because z contains true or false. true == true is true. false == true is false. So, the body of while(z) will be executed if z is true, and skipped if z is false.

It works great!!! Thank you PaulS!!!