Advertisement
macca-nz

charlieplex_dice_using_millis

Feb 10th, 2021
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  LED's DICE USING CHARLIE--PLEXING with millis() delays
  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.  
  35. #define I (uint8_t)1
  36. #define VII (uint8_t)7
  37. #define XL (uint8_t)40
  38. #define LX (uint8_t)60
  39.  
  40. bool throwDice = true;
  41. long ctRollDice = Z;
  42. uint8_t  count = Z, diceNUM = Z;
  43.  
  44. void setup(){
  45.       pinMode(button, INPUT_PULLUP);
  46.       for(uint8_t i = 0; i < IV; i++){
  47.         pinMode(leds[i], OUTPUT);
  48.         digitalWrite(leds[i], diceVal[diceNUM][i]);      
  49.       }
  50.       uint8_t randomSeed(analogRead(button));
  51. }
  52.  
  53. void loop(){
  54.  
  55.       uint8_t trigger = digitalRead(button);
  56.  
  57.       if(trigger == LOW){
  58.           ctRollDice = millis();
  59.           throwDice = true;
  60.       }
  61.            
  62.       if(throwDice){
  63.           if(count > XL){
  64.               count = Z;
  65.               diceNUM = random(I, VII);
  66.               throwDice = false;  
  67.           }else{
  68.               if(millis() - ctRollDice > LX){
  69.                   ctRollDice = millis();
  70.                   count++;
  71.                   diceNUM = random(I, VII);
  72.               }else if(millis() - ctRollDice > XL){
  73.                   uint8_t diceNUM = Z;            
  74.               }
  75.           }
  76.       }
  77.         for(uint8_t i = Z; i < IV; i++){
  78.               digitalWrite(leds[i], diceVal[diceNUM][i]);
  79.      }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement