Beep
Beep

Reputation: 2823

show highest score

I have a simple program that has a loop to create a timer, a beep starts the game and ends the game, there are 4 buttons so 4 players.

I have a button click event that registers a click every time the button is clicked at the end I have a print out of all players clicks.

I need a way to tell who got the most clicks so I can light a led up next to there button.

Dose any one know how I could solve this problem code is bellow.

Code

    const int  buttonPin = 7;    // the pin that the pushbutton is attached to
const int  buttonPin2 = 6;    // the pin that the pushbutton is attached to
const int  buttonPin3 = 4;    // the pin that the pushbutton is attached to
const int  buttonPin4 = 3;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

int buttonPushCounter2 = 0;   // counter for the number of button presses
int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button

int buttonPushCounter3 = 0;   // counter for the number of button presses
int buttonState3 = 0;         // current state of the button
int lastButtonState3 = 0;     // previous state of the button

int buttonPushCounter4 = 0;   // counter for the number of button presses
int buttonState4 = 0;         // current state of the button
int lastButtonState4 = 0;     // previous state of the button

boolean player1 = false;
boolean player2 = false;
boolean player3 = false;
boolean player4 = false;


int speakerOut = 5;
  int a = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // read the pushbutton input pin:

   a ++;
  Serial.println(a);
    analogWrite(speakerOut, NULL);

  if(a > 50 && a < 300){
  analogWrite(speakerOut, 200);
  }

  if(a <= 49){
    analogWrite(speakerOut, NULL);
  }

  if(a >= 300 && a <= 2499){
      analogWrite(speakerOut, NULL);
  }

if(a > 300 && a < 2500){

  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) 
 {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button1 pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
   if (buttonState2 != lastButtonState2) 
 {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter2++;
      Serial.println("on");
      Serial.print("number of button2 pushes:  ");
      Serial.println(buttonPushCounter2);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  if (buttonState3 != lastButtonState3) 
 {
    // if the state has changed, increment the counter
    if (buttonState3 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter3++;
      Serial.println("on");
      Serial.print("number of button3 pushes:  ");
      Serial.println(buttonPushCounter3);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  if (buttonState4 != lastButtonState4) 
 {
    // if the state has changed, increment the counter
    if (buttonState4 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter4++;
      Serial.println("on");
      Serial.print("number of button4 pushes:  ");
      Serial.println(buttonPushCounter4);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;
  lastButtonState2 = buttonState2;
  lastButtonState3 = buttonState3;
  lastButtonState4 = buttonState4;

}

if(a > 2500){
    analogWrite(speakerOut, 200);

}

if (a > 2700){
      analogWrite(speakerOut, NULL);

// below is a print out of click by each player
    Serial.print("player 1 clicks = ");
    Serial.println(buttonPushCounter);

    Serial.print("player 2 clicks = ");
    Serial.println(buttonPushCounter2);

    Serial.print("player 3 clicks = ");
    Serial.println(buttonPushCounter3);

    Serial.print("player 4 clicks = ");
    Serial.println(buttonPushCounter4);

   // if(buttonPushCounter > buttonPushCounter2 || buttonPushCounter > buttonPushCounter3 || buttonPushCounter > buttonPushCounter4){
   //   player1 = true;
   // }
    //if(buttonPushCounter2 > buttonPushCounter || ..... ){
    //  player2 = true;
    //}

   // Serial.print("The two players with the highest scores are player... ");   
}
}

so what I need is a way to tell who got the most clicks

Upvotes: 0

Views: 73

Answers (2)

chux
chux

Reputation: 153527

Use qsort().

 #include<stdlib.h>

 typedef struct {
   int Player;
   int Score;
 } PS_T;

 int compar(const void *va, const void *vb) {
   PS_T *ia = (PS_T *) va;
   PS_T *ib = (PS_T *) vb;
   return (ia->Score >= ib->Score) - (ib->Score >= ia->Score);
 }

 // Return true if Silver beat Bronze (detect ties after 2nd place.)
 int Sort4(int *Gold, int *Silver, 
     int Score1, int Score2, int Score3, int Score4) {
   PS_T PS[4] = { { 1, Score1 }, { 2, Score2 }, { 3, Score3} , {4, Score4} };
   qsort(PS, 4, sizeof PS[0], compar);
   *Gold = PS[0].Player;
   *Silver = PS[1].Player;
   return PS[1].Score > PS[2].Score;
 }

 ...
 int First;
 int Second;
 Sort4(&First, &Second, buttonPushCounter, buttonPushCounter2, 
   buttonPushCounter3, buttonPushCounter4);

Upvotes: 2

smagnan
smagnan

Reputation: 1257

You can define a macro like that:

#define MAX(a,b) ((a) > (b) ? (a) : (b)) 

(max of two values)

(or anything else to calculate a maximum) then use it to get the best score

int bestScore = MAX(MAX(buttonPushCounter,buttonPushCounter2),MAX(buttonPushCounter3,buttonPushCounter4));

and finally use that best score to get the corresponding player, with a simple switch case or something else (Up to you to decide how you want to do) like that:

player1 = (bestScore==buttonPushCounter); // player1=true if the scores match etc...
player2 = (bestScore==buttonPushCounter2);
player3 = (bestScore==buttonPushCounter3);
player4 = (bestScore==buttonPushCounter4);

It's probably not the most elegant way to do, but you can see if there are more than one winner, like two players with the same high score, and "display" all of them.

Upvotes: 1

Related Questions