Advertisement
prampec

LED&KEY TernaryGuessing

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