zaterdag 21 november 2009

Project: Rolling the dice, reading input

Part of our game will be pressing a button to role the dice. Last time we build a die. Today we will see how to read input from a player to role the die and play the game.

 Because we have two players, we will need two buttons. A button input is digital, it's either pressed or not. This means we can use it on our Arduino as digital input. The Arduino has 13 digital I/O pins, each configurable as either input or output.

The following circuit will require the following items:
8 x Red LED (1 LED for player two)
1 x Green LED (player one)
1 x Arduino Duemilanove
1 x 680 Ohm Resistor (blue, gray, brown, gold)
2 x 10 KOhm resistor (brown, black, orange, gold)
2 x press-to-connect buttons






The 10 Kohm resistors are required to avoid the Arduino to misread the input pin when the button is unpressed. If not connected with an resistor, the Arduino will randomly see the button as pressed.

The following code will enable us to play the game with one die:



/* translate pin numbers into something human readable */
int dicePinstart =2;
int light1 = 9;
int light2 = 10;
int button1 = 11;
int button2 = 12;

/* game modes */
int gamestart = 0;
int gamePlayer1 =1;
int gamePlayer2 = 2;
int rollingPlayer1 = 3;
int rollingPlayer2 = 4;
int gameEnd = 5;
int gameMode = gamestart;
int curNum;
int result1;
int result2;

/* keep track of the gameticks */
int tick=0;
int tmp=0;
bool tmpBool;
int input;

/* different modes for the die */
int dicemode[8][7] =
{
  {0,0,0,0,0,0,0}
  ,{0,0,0,1,0,0,0}
  ,{1,0,0,0,0,0,1}
  ,{1,0,0,1,0,0,1}
  ,{1,0,1,0,1,0,1}
  ,{1,0,1,1,1,0,1}
  ,{1,1,1,0,1,1,1}
  ,{1,1,1,1,1,1,1}

};

int curmode[8];

void setup()
{
  /* define die LEDs as output */
  for (int i = 0; i<10; i++)
    pinMode(dicePinstart+i, OUTPUT);

  /* define player LEDs as output */ 
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
 
  /* define player buttons as input */
  pinMode(button1, INPUT);    
  pinMode(button2, INPUT);    
 
  /* reset the die to no die mode 0 */
  setDiceMode(0);
}

void setDiceMode(int num)
{
  /* copy a die mode into the current diemode */
  memcpy(curmode,&dicemode[num],sizeof(curmode));
}

void showDice()
{
  /* base on the value for the LED in the current die mode, either enable or diable the LED */
  for (int i = 0; i<7; i++)
    if (curmode[i]==1)
      digitalWrite(dicePinstart+i, HIGH);     
    else
      digitalWrite(dicePinstart+i, LOW);    
   
}

void GameTick()
{
  /* this funcction will handle game modes*/
 
  if (gameMode==gamestart)
  {
    /* a new game starts, enable all LEDs to test them */
    setDiceMode(7);
    showDice();
    digitalWrite(light1, HIGH);   
    digitalWrite(light2, HIGH); 
   
    /* count till 20 (2 seconds) */
    tmp++;
    if (tmp == 20)
    {
      /* switch to another gamemode and disable all LEDs*/
      gameMode=gamePlayer1;
      setDiceMode(0);
    
      showDice();
      digitalWrite(light1, LOW);   
      digitalWrite(light2, LOW);  
      tmp =0;
    }
  }
  else if (gameMode==gamePlayer1)
  {
    /* we want the player LED to blink */
     tmpBool=!tmpBool;
     if (tmpBool)
       digitalWrite(light1, HIGH);
      else
        digitalWrite(light1, LOW);  
       
     /* does the player press his button? */
     input = digitalRead(button1);
     if (input ==HIGH)
     {
        /* yes he did, role the die */
        gameMode = rollingPlayer1;
        digitalWrite(light1, HIGH);
        tmp=0;
     } 
  }
  else if (gameMode==gamePlayer2)
  {
    /* we want the player LED to blink */
     tmpBool=!tmpBool;
     if (tmpBool)
       digitalWrite(light2, HIGH);
      else
        digitalWrite(light2, LOW);  
    
     /* does the player press his button? */
     input = digitalRead(button2);
     if (input ==HIGH)
     {
        /* yes he did, role the die */
        gameMode = rollingPlayer2;
        digitalWrite(light2, HIGH);
        tmp=0;
     } 
  }
  else if (gameMode==rollingPlayer1)
  {
    /* role for 1.5 seconds, show the results for 0.5 seconds and continue */
    tmp++;
    if(tmp < 15)
    {
      /* choose a random number */
      curNum = random(1,6);
      setDiceMode(curNum);
      showDice();
    }
    if(tmp == 20)
    {
      /* 2.0 seconds are up, the other player is at turn. reset die and player LED */
      result1 = curNum;
      setDiceMode(0);
      showDice();
      gameMode=gamePlayer2;
      digitalWrite(light1, LOW);  
    }
  }  
    else if (gameMode==rollingPlayer2)
  {
    /* role for 1.5 seconds, show the results for 0.5 seconds and continue */
    tmp++;
    if(tmp < 15)
    {
      /* choose a random number */
      curNum = random(1,6);
      setDiceMode(curNum);
      showDice();
    }
    if(tmp == 20)
    { 
      /* 2.0 seconds are up, the game ends. reset die and player LED */
      result2= curNum;
      setDiceMode(0);
      showDice();
      gameMode=gameEnd;
      digitalWrite(light2, LOW);  
    }       
  }
   else if (gameMode==gameEnd)
  {
     /* we want the result to blink */
    tmpBool=!tmpBool;
   
    /* did player one win, or played a tie? */
    if (result1>=result2)
    {
      /* blink his LED */
      setDiceMode(result1);
      if (tmpBool)
        digitalWrite(light1, HIGH);
      else
        digitalWrite(light1, LOW); 
    }
   
    /* did player two win, or played a tie? */
    if (result1<=result2)
    {
      /* blink his LED */
      setDiceMode(result2);
      if (tmpBool)
        digitalWrite(light2, HIGH);
      else
        digitalWrite(light2, LOW); 
    }
   
    /* blink the die */
    if (!tmpBool)
      setDiceMode(0);
    showDice();
   
    /* did either player pressed his button? */
    if ((digitalRead(button1) == HIGH) ||
        (digitalRead(button2) == HIGH))
    {
      /* reset game */
      setDiceMode(0);
   
      showDice();
      digitalWrite(light1, LOW);
      digitalWrite(light2, LOW); 
      gameMode = gamestart;
      tmp=0;
    }
  }
}

void loop()
{

  /* ready for antohter game tick? */
  if (tick==0)
  {
    GameTick();
  }
 
  /* tick, tick, tick */
  tick++;
  if (tick==100)
    tick =0;
   
  /* wait a bit */
  delay(1);
}



 


Well that's about it. We are able to play the game... with one die. But we do need two dice. The solution would be simple to connect another 7 LEDs with the Arduino, if the Arduino had another 7 pins left. So we need to extend the Arduino. How? See you later ;)

Geen opmerkingen:

Een reactie posten