SHARE
TWEET

LED&KEY TernaryGuessing

prampec Mar 13th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const bool serial = false;
  2. uint8_t mode = 2; // 2: binary, 3: ternary guessing game
  3.  
  4. const uint8_t stb = 2;
  5. const uint8_t clk = 3;
  6. const uint8_t dio = 4;
  7. const uint8_t numLed7[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  8. const uint8_t lookups[][2] = { {'0',numLed7[0]}, {'1',numLed7[1]}, {'2',numLed7[2]}, {'3',numLed7[3]}, {'4',numLed7[4]},
  9.   {'5',numLed7[5]}, {'6',numLed7[6]}, {'7',numLed7[7]}, {'8',numLed7[8]}, {'9',numLed7[9]},
  10.   {'Y',110}, {'E',121}, {'S',109}, {'P',115}, {'t',120}, {'C',57}, {'r',80}, {'O',63}, {'A', 119},
  11.   {'b', 124}, {'i', 16}, {'n', 84}, {'G', 61}
  12. };
  13.  
  14. void low() {
  15.   digitalWrite(stb, LOW);
  16. }
  17. void high() {
  18.   digitalWrite(stb, HIGH);
  19.   delay(1);
  20. }
  21. void out(uint8_t data) {
  22.   shiftOut(dio, clk, LSBFIRST, data);
  23. }
  24. void command(uint8_t data) {
  25.   low(); out(data); high();
  26. }
  27. void addr(uint8_t a, uint8_t data) {
  28.   low(); out(a); out(data); high();
  29. }
  30. void resetDisplay()
  31. {
  32.   command(0x40); // autoincrement
  33.   low();
  34.   out(0xc0);   // starting address 0
  35.   for(uint8_t i = 0; i < 16; i++) out(0x00);
  36.   high();
  37. }
  38.  
  39. uint8_t readButtons(void)
  40. {
  41.   uint8_t buttons = 0;
  42.   low();
  43.   out(0x42);
  44.   pinMode(dio, INPUT);
  45.   for (uint8_t i = 0; i < 4; i++) {
  46.     uint8_t v = shiftIn(dio, clk, LSBFIRST) << i;
  47.     buttons |= v;
  48.   }
  49.   pinMode(dio, OUTPUT);
  50.   high();
  51.   return buttons;
  52. }
  53. uint8_t cPos(uint8_t pos) {
  54.   return 0xc0 + (pos<<1);
  55. }
  56. void setLed(uint8_t value, uint8_t pos)
  57. {
  58. //  pinMode(dio, OUTPUT); // default mode anyway
  59. //  command(0x44);  // already set single mode, so this is default
  60.   low();
  61.   addr(cPos(pos) + 1, value);
  62.   high();
  63. }
  64. void ledChase() {
  65.   for (uint8_t i=0; i<8; i++) {
  66.     addr(cPos(i)+1, 1);
  67.     delay(70);
  68.     resetDisplay();
  69.   }  
  70. }
  71. uint8_t lookup( char in ) {
  72.   for (uint8_t i=0; i<sizeof(lookups)/sizeof(lookups[0]); i++)
  73.     if (lookups[i][0]==in) return lookups[i][1];
  74.   return 0xff;
  75. }
  76. void displayString(String ds, uint8_t pos, int dir) {
  77. //   Serial.println(ds);
  78.   for (uint8_t i = 0; i<ds.length(); i++) {
  79.     uint8_t ind = i; if (dir==-1) ind = ds.length()-1-i;
  80.     addr(cPos(pos+dir*i), // numLed7[ (int)(ds[ind])-48 ]);
  81.       lookup( ds[ind]) );
  82.   }
  83. }
  84. void displayDecimal(int decimal, uint8_t pos, int dir) { // direction: 1 l2r, -1 r2l
  85.   displayString( String(decimal), pos, dir);
  86. }
  87.  
  88. void sLog(String label, String value) {
  89.   if (serial) { Serial.print(label); Serial.print(": "); Serial.println(value); }
  90. }
  91.  
  92. long gameTime;
  93. int randDecimal, weightDecimal, steps, gameNo, saveButtons, idle;
  94. int score;
  95. uint8_t ledStates[] = { 0,0,0,0,0,0,0,0 };
  96. uint8_t ledLights[] = { 0,0,0,0,0,0,0,0 };
  97.  
  98. void generateDecimal() {
  99.   int res = 0, weight = 0;
  100.   int topDigit = 7; if (mode==3) topDigit = 5;
  101.   for (uint8_t i=0; i<=topDigit; i++) {
  102.     uint8_t r = random(mode);
  103.     int power=1; for (int p=1; p<=i; p++) power*=mode;
  104.     res += power * r;
  105.     weight += r;
  106.   }
  107.   randDecimal = res;
  108.   weightDecimal = weight;
  109.   sLog("randDecimal", String(randDecimal));
  110. }
  111.  
  112. void newGame() {
  113.   resetDisplay();
  114.   for (uint8_t i=0; i<8; i++) ledStates[i]=0;
  115.   generateDecimal();
  116.   displayDecimal( randDecimal, 0, 1 );
  117.   steps = 0;
  118.   gameNo++;
  119.   gameTime = millis();
  120.   idle = 1;
  121. }
  122. void setup() {
  123.   delay(1000);
  124.   Serial.begin(9600);
  125.   score = 0;
  126.   gameNo = 0;
  127.   randomSeed(analogRead(0));
  128.   pinMode(stb, OUTPUT);
  129.   pinMode(clk, OUTPUT);
  130.   pinMode(dio, OUTPUT);
  131.   command(0x89); // intensity min
  132.   ledChase();
  133.   command(0x44); // single address mode
  134.   newGame();
  135. }
  136. void loop()
  137. {
  138.   for (int i=0; i<8; i++) {
  139.     if (mode==2) {
  140.       ledLights[i] = ledStates[i];
  141.       setLed(ledLights[i], i);
  142.     }
  143.     else if (mode==3) {
  144.       if (ledStates[i]==2) ledLights[i] = 1-ledLights[i];
  145.       else ledLights[i] = ledStates[i];
  146.       setLed(ledLights[i], i);
  147.     }
  148.   }
  149.   uint8_t buttons = readButtons();
  150.   if (idle && buttons) { saveButtons = buttons; idle = 0; }
  151.   if (!idle) { if (buttons > saveButtons) saveButtons = buttons; }
  152.   if (buttons==0) { buttons = saveButtons; saveButtons = 0; idle = 1; }
  153.   if (buttons > 0 && idle) {
  154.     sLog("Buttons", String(buttons));
  155.     steps++;
  156.     if (buttons == 129) {
  157.       steps--;
  158.       ledChase(); gameNo=0; score=0;
  159.       resetDisplay();
  160.       displayString("rEStArt",0,1);
  161.       delay(1000);
  162.       newGame();
  163.     } else if (buttons == 65) {
  164.       steps--;
  165.       ledChase();
  166.       resetDisplay();
  167.       displayString("t",0,1);
  168.       displayDecimal(millis()/1000, 7, -1);
  169.       delay(1000);
  170.     } else if (buttons == 3) {
  171.       steps--;
  172.       ledChase();
  173.       resetDisplay();
  174.       displayString("binArY",0,1);
  175.       mode = 2;
  176.       delay(1000);
  177.     } else if (buttons == 5) {
  178.       steps--;
  179.       ledChase();
  180.       resetDisplay();
  181.       displayString("tErnArY",0,1);
  182.       mode = 3;
  183.       delay(1000);
  184.     } else if (buttons == 9) {
  185.       steps--;
  186.       ledChase();
  187.       resetDisplay();
  188.       displayString("GEnErAtE",0,1);
  189.       delay(1000);
  190.       newGame();
  191.     } else {
  192.       for (int i=0; i<8; i++) {
  193.         int power=1; for(int p=1; p<=i; p++) power*=2;
  194.         if (buttons == power) {
  195.           ledStates[i]++; if (ledStates[i]>=mode) ledStates[i]=0;
  196.         }
  197.       }
  198.     }
  199.     int guess = 0;
  200.     resetDisplay();
  201.     displayDecimal( randDecimal, 0, 1);
  202.     for(uint8_t pos = 0; pos < 8; pos++) {
  203.       int power=1; for(int p=1; p<=7-pos; p++) power*=mode;
  204.       guess += power * ledStates[pos];
  205.     }
  206.     Serial.println( guess );
  207.     displayDecimal( guess, 7, -1 );
  208.     if (randDecimal == guess) {
  209.       sLog("Guess", "Bingo");
  210.       sLog("gameNo", String(gameNo));
  211.       sLog("gameTime", String(gameTime));
  212.       sLog("millis()", String(millis()));
  213.       long gameDuration = (int)((millis() - gameTime) / 1000);
  214.       delay(500);
  215.       score = ( (gameNo-1)*score + (steps-weightDecimal)*5+3*gameDuration ) / gameNo;
  216.       sLog("steps", String(steps));
  217.       sLog("weightDecimal", String(weightDecimal));
  218.       sLog("gameDuration", String(gameDuration));
  219.       sLog("score", String(score));
  220. /*      resetDisplay();
  221.       displayString("PrS", 0, 1);
  222.       displayDecimal(weightDecimal, 4, 1);
  223.       displayDecimal(steps, 7, -1);
  224.       delay(2000);
  225.       resetDisplay();
  226.       displayString("SEC", 0, 1);
  227.       displayDecimal(gameDuration, 7, -1);
  228.       delay(2000);
  229. */      resetDisplay();
  230.       displayString("SCOrE", 0, 1);
  231.       delay(1000);
  232.       resetDisplay();
  233.       displayDecimal(gameNo, 0, 1);
  234.       displayDecimal(score, 7, -1);
  235.       delay(2000);
  236.       newGame();
  237.     }
  238.   }
  239.   delay(10);
  240. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top