Game using Wire Library Master Reader/Slave Sender for two players

Hi All i have this game code and I would like to make the two Arduino’s link to each other so that two players can play against each other (who is the fastest). using Wire Library Master Reader/Slave Sender and I2C. please anyone can help.the code in the attachment

code.doc (35 KB)

What is “this game code”? What attachment?

I can now see the attachment. The attachment is great if you have Microsoft Word or the Word Viewer or something else that can read Word .doc files. However, the proper way to post is…

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

#include <TrueRandom.h>

// pins
const int buttonPin = 7;
int ledPin[] = {2, 3, 4, 5};

// variabelen
int number = TrueRandom.random(0, 16);
boolean buttonState = LOW;
int counter = 0;
unsigned long startTime, stopTime, beginTime, endTime;
int record = 30000;
int gameMode = 1;
int gameNr = 1;


void setup() {
  Serial.begin(9600);
  instructions();
  pinMode(buttonPin, INPUT);

  for (int i = 0; i < 4; i++) {
    pinMode(ledPin[i], OUTPUT);
  }
}


void showBinary(byte binaryNumber) {
  for (int i = 0; i < 4; i++) {
    if (bitRead(binaryNumber, i) == 1) {
      digitalWrite(ledPin[i], HIGH);
    }
    else {
      digitalWrite(ledPin[i], LOW);
    }
  }
}



void loop() {
  // waanner word 16 hij gaat gam bendegen
  while ( number < 16 ) {
    /*
 There are 4 different game modes
        1 is the beginning of the game
        2 is the game itself
        3 if the answer is correct
        4 if the answer is wrong
    */

    if (gameMode == 1) {
      counter = 0;                                                           
      stopTime = 0;                                                           
      buttonState = LOW;                                                      
      number = TrueRandom.random(0, 16);                                      
      if (debounceButton(buttonState) == HIGH && buttonState == LOW) {       
        buttonState = HIGH;                                                   
        startGame();                                                          
        showBinary(number);                                                   
        beginTime = millis();                                                
        startTime = millis();
        gameMode = 2;                                                         
      }
    }
    else if (gameMode == 2) {
      stopTime = millis() - startTime;                                     
      if (debounceButton(buttonState) == HIGH && buttonState == LOW) {       
        buttonState = HIGH;                                                   
        startTime = millis();
        counter++;                                                            
      }
      else if (debounceButton(buttonState) == LOW && buttonState == HIGH) {   
        buttonState = LOW;                                                   
      }
      if (stopTime > 2000) {                                                  
        if (counter == number) {
          correctNumber();
          gameMode = 3;
        }
        else {
          wrongNumber();                                                        
          gameMode = 4;                                                         
        }
      }

      else if (counter > number) {                                            
        wrongNumber();                                                        
        gameMode = 4;                                                         
      }
    }
    else if (gameMode == 3) {
      endTime = millis() - beginTime;                                         
      if (endTime < record) {                                                
        record = endTime;                                                   
      }
      Serial.print(endTime);                                                  
      Serial.print("\t");
      Serial.print("\t");
      Serial.print(record);
      Serial.println(" ");
      Serial.println("----------------------------");
      gameNr++;                                                               
      gameMode = 1;                                                           
    }
    else if (gameMode == 4) {
      Serial.println("Game Over!");                                           
      Serial.println("----------------------------");
      gameNr++;                                                               
      gameMode = 1;                                                           
    }
  }
}

boolean debounceButton(boolean state) {
  boolean stateNow = digitalRead(buttonPin);
  if (state != stateNow) {
    delay(10);
    stateNow = digitalRead(buttonPin);
  }
  return stateNow;
}


void correctNumber() {
  digitalWrite(2, LOW);
  digitalWrite(5, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(100);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  delay(100);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  delay(100);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
}


void wrongNumber() {
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(2, HIGH);
  digitalWrite(5, HIGH);
  delay(100);
  digitalWrite(2, LOW);
  digitalWrite(5, LOW);
  delay(100);
  digitalWrite(2, HIGH);
  digitalWrite(5, HIGH);
  delay(100);
  digitalWrite(2, LOW);
  digitalWrite(5, LOW);
}


void startGame() {
  Serial.println("############################");
  Serial.print("GAME NR. ");
  Serial.print(gameNr);
  Serial.println(" ");
  Serial.println("----------------------------");
  Serial.print("YOUR TIME");
  Serial.print("\t");
  Serial.println("RECORD");
}


void instructions() {
  Serial.println("################################################################");
  Serial.println("#                                                              #");
  Serial.println("#  BINARY GAME                                                 #");
  Serial.println("#                                                              #");
  Serial.println("#  INSTRUCTIES                                                 #");
  Serial.println("#  PRESS THE BUTTON TO START THE GAME                     #");
  Serial.println("#  LEDS GIVING A NUMBER 0 T / M 15 WEATHER AS BINARY #");
  Serial.println("#  PRESSURE AS SOON AS POSSIBLE THE NUMBER ON THE BUTTON             #");
  Serial.println("#                                                              #");
  Serial.println("#  HAVE FUN!                                             #");
  Serial.println("#                                                              #");
  Serial.println("################################################################");

}

Would you be so kind as to translate the instructions at the end of the code into english. It would help to understand what the code is supposed to do.

What part do you want the I2C line to play? What data do you envision sending from one board to the other?

Forget the code for a minute and tell us how it is supposed to work with two boards. Does one board just need to send its chosen random number to the other? Is there timing involved between the two? Don't worry if you can't explain it in programming terms. Just explain it in regular English. But it is impossible to help you until we know what your vision is.

Delta_G: Forget the code for a minute and tell us how it is supposed to work with two boards. Does one board just need to send its chosen random number to the other? Is there timing involved between the two? Don't worry if you can't explain it in programming terms. Just explain it in regular English. But it is impossible to help you until we know what your vision is.

Hi Delta_G ok i like to have this 1- two arduino master reader/slave sender 2- two buttons to play against each other 3- shown the result on computer screen using the Serial.begin(9600) (Who is fastest in the answer ) that all wat i need

Who is the fastest at what exactly. You have to understand that I don't know anything about your game other than what you've told me which is next to nothing.

What do you mean by, "play against each other". I get that there will be two players, but how will that work.

If you can't describe how the game works or how it is played or how you want to use the two arduinos any better than that then please stop wasting our time with this. If you want help then for the love of god tell us how the freaking game is played. Tell us how you want the two boards to interact. Have you even thought about that? If not then it is WAY too early to start writing any code.

Do both players see the same binary display?

You can certainly determine the reaction time on each arduino and compare the results, but there may be a problem with the slightly different clock rates on the two arduinos. If one response is in 300ms and the other is 303, the appaerently longer time may be shorter.

Were you trying to have a single measurement clock?

EDIT: You certainly can not have one player start the game with a button press. You will need to have the number display initiated by a random timer. You might be better off with two buttons on one Arduino. Why do you think you need two Arduino's to play?

Delta_G:
Who is the fastest at what exactly. You have to understand that I don’t know anything about your game other than what you’ve told me which is next to nothing.

What do you mean by, “play against each other”. I get that there will be two players, but how will that work.

If you can’t describe how the game works or how it is played or how you want to use the two arduinos any better than that then please stop wasting our time with this. If you want help then for the love of god tell us how the freaking game is played. Tell us how you want the two boards to interact. Have you even thought about that? If not then it is WAY too early to start writing any code.

Dear

the game is working for 1 player I mean working on 1 ardruino and the game ardruino chooses a random number and then random number is converted to a binary and the player have 2 second to pressed the button for right answer
I would like to add another player using second ardruino to play against the first player like master reader/slave sender using i2c

OK, so it displays a number and then you see who presses a button faster? You don't need two Arduinos for that. Just two buttons on the same Arduino and see who presses theirs first.

What do the button presses and the binary number have to do with one another?

And one more time just in case you might answer this time. WHAT DO YOU WANT TRANSMITTED BETWEEN THE TWO ARDUINOS? Don't just say you want it to be a 2 player game. I get that. You've said that enough. Now, please explain how that will work. Will one Arduino pick the number? Should both arduinos display the binary number? Will both arduinos have the same number? Will they be different? Does one need to tell the other when the button was pressed?

Seriously, forget the code for a minute and explain how you want this thing to work. I don't understand why you won't do that. You just keep saying that you want it to be 2 players but you won't say anything about how you want that to look.

Here's an idea. One player gets the Arduino that you already have. The other player gets an Arduino card with nothing connected. They throw the unconnected Arduino at the head of the first player. If the first player can press his button without getting hit in the head he wins. If he gets hit then the thrower wins. There, now y ou have a two player game.

If that's not how you want it to work, then please please PLEASE please tell us how you want it to work. Do you really not understand that you need to define that?

cattledog: Do both players see the same binary display?

You can certainly determine the reaction time on each arduino and compare the results, but there may be a problem with the slightly different clock rates on the two arduinos. If one response is in 300ms and the other is 303, the appaerently longer time may be shorter.

Were you trying to have a single measurement clock?

EDIT: You certainly can not have one player start the game with a button press. You will need to have the number display initiated by a random timer. You might be better off with two buttons on one Arduino. Why do you think you need two Arduino's to play?

Hi Cattedog

dear i like to do it because there a challenge between me and my friends if i do it i get extra score in school and i like to find the answer and to have fun with it :)

cattledog: Do both players see the same binary display?

And to answer the question the OP writes:

Habib_m: dear i like to do it because there a challenge between me and my friends if i do it i get extra score in school and i like to find the answer and to have fun with it :)

Friends, I think we are either being trolled...

Habib_m: Friends, I think we are either being trolled... haha sure you do :)

Yes I do. You must be a troll. If you wanted help with your project you would answer all the questions you have been asked. Instead you just keep saying the same thing over and over. Now you have answered 1 out of many questions you have been asked. I can only assume that you aren't really interested in doing any of this or you would answer the questions. Posting for help when you don't really want it is trolling. Therefore you are a troll.

Now answer the questions you have been asked or just stop wasting our time and quit posting. But don't troll us.

Delta_G: Yes I do. You must be a troll. If you wanted help with your project you would answer all the questions you have been asked. Instead you just keep saying the same thing over and over. Now you have answered 1 out of many questions you have been asked. I can only assume that you aren't really interested in doing any of this or you would answer the questions. Posting for help when you don't really want it is trolling. Therefore you are a troll.

Now answer the questions you have been asked or just stop wasting our time and quit posting. But don't troll us.

Dear I am not losing your time or my time or time of any one here I have a question and I like to have answer for it if you would like that will be a great and if you can’t is it ok The game it like this now

master Arduino have smaller cod of slave Arduino The master random a number and he send this number to start the game with the slave both consctet to pc with usb and both using serial 9600 and in the end the master will showing the winner of the game if you can’t understand something I will explained again

Habib_m: if you can’t understand something I will explained again

What do you mean? There are at least ten good questions about this project that you have so far refused to answer. If you want me to buy that you are not just some troll here to waste our time, then go read back through the thread and answer the questions that you have been asked.

Otherwise I think you are just a troll here trying to waste our time. That's not very nice of you. :(

Several posts have disappeared from this thread. Did a mod delete them or is this user doing it?

Delta_G: What do you mean? There are at least ten good questions about this project that you have so far refused to answer. If you want me to buy that you are not just some troll here to waste our time, then go read back through the thread and answer the questions that you have been asked.

Otherwise I think you are just a troll here trying to waste our time. That's not very nice of you. :(

Dear Delta

I apologise i don't mean bad thing I mean if is something unclear or incomprehensible ,and I delete some unimportant comments to reduce the number of pages.

Habib_m:
Dear Delta

I apologise i don’t mean bad thing I mean if is something unclear or incomprehensible ,and I delete some unimportant comments to reduce the number of pages.

Yet another post without answering the questions you have been asked.

And now you’re deleting posts out of the thread.

I’m done here. Maybe someone else will try to guess what you are doing. But I doubt it. When you’re ready to answer the questions you have been asked then you can try again. If you are not going to go through and answer those questions then don’t waste your time posting. Nobody can help you until you answer those.

I won’t be back to this thread. You have wasted enough of my time. Good luck to you.