Go Down

Topic: Ok a LED Dice - but it's my first Arduino project! - Im proud :) (Read 908 times) previous topic - next topic

Kenn-p

Hey, the routine to set the digital outs is a LOT nicer than any examples I've seen!  Bare with me - Im new here :-)

Quote

// Dice by Kenn-P 11/02/2012
//
// Arrange LEDs in this pattern, 
// Either: All LEDs will need a current limiting resistor in series
// Or: Digital8, 9, 10 will need a single resistor in series with the 2 LEDs and Digital 11 will need a resistor
// half that value.
//
//  LED0 .... LED1
//  LED2 LED3 LED4
//  LED5 .... LED6
//
// Wire up like this (dont forget the resistors!)
//
// Digital8  > LED0 > LED6 > Ground
// Digital9  > LED1 > LED5 > Ground
// Digital10 > LED2 > LED4 > Ground
// Digital11 > LED3 > Ground


// See in "diceSetValue" for the definition of dicePattern" these are bit patterns for each 
// of the dice values
//1 on the dice = pattern 8 = 1000 (binary) which will set Digital 11 HIGH and 8,9,10 LOW
//2 on the dice = pattern 2 = 0010 (binary) which will set Digital 9 HIGH and 8,10,11 LOW
//3 on the dice = pattern 10 = 1010 (binary) which will set Digital 9,11 HIGH, and 8,10 LOW
// ..
//6 on the dice = pattern 7 = 0111 (binary) which will set 8,9,10 HIGH and 11 LOW

//please note this could be extended to a seven sided die by changing the line mentioned above
//to "byte dicePattern[]={8,2,10,3,11,7,15}";
//and changing "if (v<6) {"  below to "if (v<7) {" but this does add two bytes to the final codesize ;-)
//

byte diceValue; // last legal dice value

// set the digital pins to OUTPUT, set the default dice value
void setup() {
  for (byte i=8; i<12; pinMode(i++,OUTPUT));
  diceSetValue(1);
}


// this function (if the value passed to it is valid) decodes the appropriate pattern and sets the digital pins accordingly
void diceSetValue(byte v) {
  const byte dicePattern[]={8,2,10,3,11,7};   
  v--; //arrays are zero indexed
  if (v<6) {
    byte dP=dicePattern[v];   //get the pattern into a temp variable

    for (byte dOut=8; dOut<12; dOut++) {  // Loop throught Digital 8 to Digital 11
      digitalWrite(dOut, (dP &1));        // Set current Digital output according to the temp patterns lowest bit
      dP=dP >>1;                          // Shift the temp pattern to the right
    }                                     // and repeat for the next digital output

    diceValue=v+1;  //Store the dice value, if the function is called with an invalid value then this variable gets left as it was.
  }
}

// Simple test
void loop(){
  // as a test, simply step through the values 1 to 6, pausing between each
  for (byte i=1; i<7; i++){
    diceSetValue(i);
    delay(200); //wow adding the delay library adds 190 bytes to the codesize!
  }
}


thegoodhen

Wow, that's a nice algorithm you have there. It has the ingeniousness i admire. ( for example: for (byte i=8; i<12; pinMode(i++,OUTPUT)); is a nice one) Makes the code short. Also the idea to use bytes to store the value is pretty neat.

Kenn-p

Thanks :-)  I've been programming for quite a number of years, originally on the 6502 machines but more recently in Delphi.  It's quite nice to have to worry about code-size again (not to mention learning a new language - I never really took a shine to C).

A couple of tweeks:

Code: [Select]
// Dice by Kenn-P 11/02/2012
//
// Arrange LEDs in this pattern,
// Either: All LEDs will need a current limiting resistor in series
// Or: Digital8, 9, 10 will need a single resistor in series with the 2 LEDs and Digital 11 will need a resistor
// half that value.
//
//  LED0 .... LED1
//  LED2 LED3 LED4
//  LED5 .... LED6
//
// Wire up like this (dont forget the resistors!)
//
// Digital8  > LED0 > LED6 > Ground
// Digital9  > LED1 > LED5 > Ground
// Digital10 > LED2 > LED4 > Ground
// Digital11 > LED3 > Ground


// See in "diceSetValue" for the definition of dicePattern" these are bit patterns for each
// of the dice values
//1 on the dice = pattern 8 = 1000 (binary) which will set Digital 11 HIGH and 8,9,10 LOW
//2 on the dice = pattern 2 = 0010 (binary) which will set Digital 9 HIGH and 8,10,11 LOW
//3 on the dice = pattern 10 = 1010 (binary) which will set Digital 9,11 HIGH, and 8,10 LOW
// ..
//6 on the dice = pattern 7 = 0111 (binary) which will set 8,9,10 HIGH and 11 LOW

//please note this could be extended to a seven sided die by changing the line mentioned above
//to "byte dicePattern[]={8,2,10,3,11,7,15}";
//and changing "if (v<6) {"  below to "if (v<7) {" but this does add two bytes to the final codesize ;-)
//
// CHANGES
// 14-02-12 Kenn-P - change in diceSetValue - reduced codesize

byte diceValue; // last legal dice value

// set the digital pins to OUTPUT, set the default dice value
void setup() {
  for (byte i=8; i<12; pinMode(i++,OUTPUT));
  diceSetValue(1);
}


// this function (if the value passed to it is valid) decodes the appropriate pattern and sets the digital pins accordingly
void diceSetValue(byte v) {
  const byte dicePattern[]={8,2,10,3,11,7};   
  v--; //arrays are zero indexed
  if (v<6) {
    byte dP=dicePattern[v];   //get the pattern into a temp variable

    for (byte dOut=8; dOut<12; dOut++) {  // Loop throught Digital 8 to Digital 11
      digitalWrite(dOut, (dP &1));        // Set current Digital output according to the temp patterns lowest bit
      dP=dP >>1;                          // Shift the temp pattern to the right
    }                                     // and repeat for the next digital output

    diceValue=v+1;  //Store the dice value, if the function is called with an invalid value then this variable gets left as it was.
  }
}

// Simple test
void loop(){
  // as a test, simply step through the values 1 to 6, pausing between each
  for (byte i=1; i<7; i++){
    diceSetValue(i);
    //delay(200); //wow adding the delay library adds 190 bytes to the codesize!
    for (volatile long int delCount=0; delCount<0x10000; delCount++);
  }
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy