zaterdag 28 november 2009

Project: Rolling the dice, putting it together

In the previous posts I have talked about building a die, expanding the I/O pins on the Arduino and reading input from a player. Today we are going to build the complete game.

The following circuit is cut in parts. The first circuit  is the overview, the second is one die. Therefore you will need to build the second circuit twice. I've put in transistors, because the PCF8574P will not deliver much current.  The following components are required to build the game:

1 x Arduino Duemilanove
15 x Red LED (7 for each die, 1 for player two)
1 x green LED (player one)
2 x PCF8574P (one for each die)
2 x 1.5 KOhm resistor (brown, green, red, gold) You can use up to 2 KOhm. I put 1 KOhm + 680 Ohm in series instead.
16 x 680 Ohm resistor (blue, gray, brown, gold)

2 x 10 KOhm resistor (brown, black, orange, gold)
14 x transistor (C547B)
2 x press-to-connect buttons

 
 

/* define addresses of the dice */
#define DICE_1 (0x4 << 3 | 0x0) // 0100000
#define DICE_2 (0x4 << 3 | 0x1) // 0100001

/* include wire */
#include

/* define pin output for player lights and buttons */
int light1 = 9;
int light2 = 10;
int button1 = 11;
int button2 = 12;

/* define gamemodes */
int gamestart = 0;
int gamePlayer1 =1;
int gamePlayer2 = 2;
int rollingPlayer1 = 3;
int rollingPlayer2 = 4;
int gameEnd = 5;
int gameMode = gamestart;

/* current number for each die*/
int curNum[2];

/* results for each throw */
int result1[2];
int result2[2];

int tick=0;
int tmp=0;
bool tmpBool;
int input;

/* different dicemodes */
byte dicemode[8] =
{
  B0000000,
  B0001000,
  B1000001,
  B1001001,
  B1010101,
  B1011101,
  B1110111,
  B1111111

};

byte curmode_d1;
byte curmode_d2;

void setup()
{
  /* start the I2c */
  Wire.begin();

  /* set pins */
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  pinMode(button1, INPUT);    
  pinMode(button2, INPUT);    
 
  /* reset dice */
  setDiceMode(DICE_1,0);
  setDiceMode(DICE_2,0);
}

void setDiceMode(byte dice, int num)
{
  /* copy a dice mode into the current mode for a dice */
  if (dice == DICE_1)
    memcpy(&curmode_d1,&dicemode[num],sizeof(curmode_d1));
  else
    memcpy(&curmode_d2,&dicemode[num],sizeof(curmode_d2));
}

void showDice()
{
  /* show die one by sending the current mode */
  Wire.beginTransmission(DICE_1);
  Wire.send(curmode_d1);
  Wire.endTransmission();
 
  /* show die two by sending the current mode */
  Wire.beginTransmission(DICE_2);
  Wire.send(curmode_d2);
  Wire.endTransmission();
}

void GameTick()
{
  if (gameMode==gamestart)
  {
    /* game starts, show all lights */
    setDiceMode(DICE_1,7);
    setDiceMode(DICE_2,7);
    showDice();
    digitalWrite(light1, HIGH);   
    digitalWrite(light2, HIGH); 
   
    tmp++;
    if (tmp == 20)
    {
      /* after 2 seconds the game begins, turn all lights off */
      gameMode=gamePlayer1;
      setDiceMode(DICE_1,0);
      setDiceMode(DICE_2,0);    
      showDice();
      digitalWrite(light1, LOW);   
      digitalWrite(light2, LOW);  
      tmp =0;
    }
  }
  else if (gameMode==gamePlayer1)
  {
    /* player one is at turn, blink players one's light */
     tmpBool=!tmpBool;
     if (tmpBool)
       digitalWrite(light1, HIGH);
      else
        digitalWrite(light1, LOW);  
       
     /* did player press his button? */
     input = digitalRead(button1);
     if (input ==HIGH)
     {
        gameMode = rollingPlayer1;
        digitalWrite(light1, HIGH);
        tmp=0;
     } 
  }
  else if (gameMode==gamePlayer2)
  {
     /* player two is at turn, blink players two's light */
     tmpBool=!tmpBool;
     if (tmpBool)
       digitalWrite(light2, HIGH);
      else
        digitalWrite(light2, LOW);  
       
     /* did player press his button? */
     input = digitalRead(button2);
     if (input ==HIGH)
     {
        gameMode = rollingPlayer2;
        digitalWrite(light2, HIGH);
        tmp=0;
     } 
  }
  else if (gameMode==rollingPlayer1)
  {
    /* rolling dice for player 1 */
    tmp++;
    if(tmp < 15)
    {
      /* rolling... */
      curNum[0] = random(1,6);
      curNum[1] = random(1,6);
      setDiceMode(DICE_1,curNum[0]);
      setDiceMode(DICE_2,curNum[1]);   
      showDice();
    }
    /* after 1.5 seconds show result, till 2 seconds passed */
    if(tmp == 20)
    {
     
      memcpy(&result1,&curNum,sizeof(result1));
     
      setDiceMode(DICE_1,0);
      setDiceMode(DICE_2,0);  
      showDice();
      gameMode=gamePlayer2;
      digitalWrite(light1, LOW);  
    }
  }  
  else if (gameMode==rollingPlayer2)
  {
     /* rolling dice for player two */
    tmp++;
    if(tmp < 15)
    {
      /* rolling... */
      curNum[0] = random(1,6);
      curNum[1] = random(1,6);
      setDiceMode(DICE_1,curNum[0]);
      setDiceMode(DICE_2,curNum[1]);   
       showDice();
    }
    /* after 1.5 seconds show result, till 2 seconds passed */
    if(tmp == 20)
    {
      memcpy(&result2,&curNum,sizeof(result2));
      setDiceMode(DICE_1,0);
      setDiceMode(DICE_2,0);  
      showDice();
      gameMode=gameEnd;
      digitalWrite(light2, LOW);  
    }       
  }
   else if (gameMode==gameEnd)
  {
    tmpBool=!tmpBool;
   
    /* was the result for player one more or equal to player two? */
    if (result1[0] + result1[1]>=result2[0] + result2[1])
    {
      setDiceMode(DICE_1,result1[0]);
      setDiceMode(DICE_2,result1[1]);
   
      /* blink LED */ 
       if (tmpBool)
         digitalWrite(light1, HIGH);
        else
          digitalWrite(light1, LOW); 
    }
   
    /* was the result for player two more or equal to player one? */
    if (result1[0] + result1[1]<=result2[0] + result2[1])
    {
      setDiceMode(DICE_1,result2[0]);
      setDiceMode(DICE_2,result2[1]);  
     
      /* blink LED */ 
      if (tmpBool)
         digitalWrite(light2, HIGH);
       else
          digitalWrite(light2, LOW); 
    }
   
    /* blink dice */ 
    if (!tmpBool)
    {
      setDiceMode(DICE_1,0);
      setDiceMode(DICE_2,0);   
    }
   
    showDice();
   
    /* did any player press a button? */
    if ((digitalRead(button1) == HIGH) ||
      (digitalRead(button2) == HIGH))
    {
      /* reset game */
      setDiceMode(DICE_1,0);
      setDiceMode(DICE_2,0);  
   
      showDice();
      digitalWrite(light1, LOW);
      digitalWrite(light2, LOW); 
      gameMode = gamestart;
      tmp=0;
    }
  }
}

void loop()
{
 
  /* ready for antoher tick? */
  if (tick==0)
  {
    GameTick();
  }

  /* tick,tick,tick */
  tick++;
  if (tick==100)
    tick =0;
   
  /* wait a bit */
  delay(1);
}



 
 
 
 
 
 
 

Geen opmerkingen:

Een reactie posten