woensdag 18 november 2009

Project: rolling the dice, building a die

Part one of dice project is to show the pips of a die with LEDs. To do this we require seven LEDs. With these LED we can show every side of the die by light the correct LEDs. But we do need a way to do this. Luckily with the Arduino we have 13 digital pins which we can enable/disable as we please.

The following circuit will show the LEDs and the Arduino to make the die. You will need the following components:

7 x Red LED
1 x Arduino Duemilanove
1 x 680 Ohm Resistor (blue, gray, brown, gold)


The following code will show all sides of the dice:


/* first pin connected to a LED */
int dicePinstart =2;

/* the different states for each LED for each side of 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}

};

/* current side of the die to show */
int curmode[8];

void setup()
{
  /* set all connected pins as output */
  for (int i = 0; i<10; i++)
    pinMode(dicePinstart+i, OUTPUT);    
  
  /* initialize the die as 0 */
  setDiceMode(0);
}

void setDiceMode(int num)
{
  /* copy a side of the die to the current die buffer */
  memcpy(curmode,&dicemode[num],sizeof(curmode));
}

void showDice()
{
  /* for each LED check if it should be enabled for the current die side */
  for (int i = 0; i<7; i++)
    if (curmode[i]==1)
      digitalWrite(dicePinstart+i, HIGH);    
    else
      digitalWrite(dicePinstart+i, LOW);    
}


void loop()
{
  /* loop through the sides of the die */
  for (int i = 0; i <8; i++)
  {
    /* set current side of the die */
    setDiceMode(i);
   
    /*enable the LEDs */
    showDice();
    delay(1000);
  }
}



Geen opmerkingen:

Een reactie posten