Advertisement
macca-nz

charlieplex_dice_using_delays

Feb 10th, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  LED's DICE USING CHARLIE--PLEXING and delay()'s no millis()
  3.  *  7 x LED's on just 4 x DIGITAL OUTPUT's
  4.  *  
  5.  *            LED2        LED3
  6.  *            LED4  LED1  LED4
  7.  *            LED3        LED2
  8.  *            
  9.  * common anode and cathode (parallel) for LEDs 2 3 & 4
  10.  * Use an ADC pin for the randomSeed
  11.  *
  12.  * BY R NcCleery
  13.  * 10th FEBRUARY 2021
  14.  *
  15.  */
  16. #define Z (uint8_t)0
  17. #define II (uint8_t)2
  18. #define III (uint8_t)3
  19. #define IV (uint8_t)4
  20. #define V (uint8_t)5
  21. uint8_t leds[]{II,III,V,IV};
  22. int button(A0);
  23.  
  24. uint8_t diceVal[][4]{
  25.     {0,0,0,0},
  26.     {1,0,0,0}, //One
  27.     {0,1,0,0}, //Two
  28.     {1,0,1,0}, //Three
  29.     {0,1,1,0}, //Four
  30.     {1,1,1,0}, //Five
  31.     {0,1,1,1}, //Six
  32. };
  33.  
  34. #define I (uint8_t)1
  35. #define VII (uint8_t)7
  36. #define XX (uint8_t)20
  37. //#define LX (uint8_t)60
  38.  
  39. bool throwDice = true;
  40. uint8_t  count = Z, diceNUM = Z;
  41.  
  42. void setup(){
  43.       pinMode(button, INPUT_PULLUP);
  44.       for(uint8_t i = Z; i < IV; i++){
  45.         pinMode(leds[i], OUTPUT);
  46.         digitalWrite(leds[i], diceVal[diceNUM][i]);      
  47.       }
  48.       uint8_t randomSeed(analogRead(button));
  49. }
  50.  
  51. void loop(){
  52.  
  53.       uint8_t trigger = digitalRead(button);
  54.  
  55.       if(trigger == LOW){
  56.           throwDice = true;
  57.       }
  58.            
  59.       if(throwDice){
  60.           if(count > XX){
  61.               count = Z;
  62.               diceNUM = random(I, VII);
  63.               throwDice = false;  
  64.           }else{
  65.               for(uint8_t i = Z; i < IV; i++){
  66.               count++;
  67.               diceNUM = Z;
  68.               delay(XX);
  69.               digitalWrite(leds[i],I);
  70.               delay(XX);
  71.               diceNUM = Z;
  72.               delay(XX);
  73.               digitalWrite(leds[i],I);
  74.               delay(XX);        
  75.               }
  76.           }
  77.       }
  78.         for(uint8_t i = Z; i < IV; i++){
  79.               digitalWrite(leds[i], diceVal[diceNUM][i]);
  80.      }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement