/* Conundrometer David Johnson-Davies - www.technoblogy.com - 6th June 2014 CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ #include #include #include #include #define adc_disable() (ADCSRA &= ~(1<>24 & 0xFF); GapL = GapL & 0x00FFFFFF; } while (diff < 0x80); WordL = WordL + GapL; WordH = WordH + GapH + (WordL>>24); WordL = WordL & 0x00FFFFFF; } } void DecodeWord() { unsigned char Nibble; unsigned long TempH, TempL; for (int c=0;c<9;c++) Conundrum[c] = 'A'; TempH = WordH; TempL = WordL; for (int i=0;i<9;i++) { Nibble = TempL & 0x1F; Conundrum[i] = Conundrum[i] + Nibble; TempL = TempL>>5 | (TempH & 0x1F)<<19; TempH = TempH>>5; } } void MakeScramble () { int i=0; int j; unsigned char temp; for (int c=0;c<9;c++) Scramble[c] = Conundrum[c]; for (int c=0;c<9;c++) { j = (1 + i + random(8)) % 9; // 1 to 8 temp = Scramble[i]; Scramble[i] = Scramble[j]; Scramble[j] = temp; i = j; } } // Test Guess == Conundrum boolean TestGuess () { for (int i=0;i<9;i++) { if (Guess[i] != Conundrum[i]) return false; } return true; } // Copy character c from Scramble to character g in Guess void CopyGuess (int c, int g) { Guess[g] = Scramble[c]; lcd.setCursor(g,1); lcd.write(Scramble[c]); Scramble[c] = ' '; lcd.setCursor(c,0); lcd.write(' '); } // Move the cursor to the next character int ScanCursor (int c) { do c = (c+1) % 9; while (Scramble[c]==' '); lcd.setCursor(c,0); return c; } void loop() { unsigned long time; int t, c, g; time = millis(); do { delay(100); if (millis()-time > 15000) { Sleep(); time = millis(); lcd.print(" Conundrometer! "); } } while (!ButtonAPushed()); // Start a new trial if (Trials>100) { Score = 0; Trials = 0; } lcd.clear(); lcd.print("..."); randomSeed(millis()); FindWord(random(8680)+1); DecodeWord(); MakeScramble(); lcd.setCursor(0,0); lcd.print(Scramble); // // Countdown lcd.setCursor(0,1); for (int i=0;i<15;i++) lcd.write(255); time = millis(); t = 2000; lcd.setCursor(0,1); do { if ((millis()-time > t) && (t <= 30000)) { lcd.write(32); t += 2000; } } while (!ButtonAPushed() && t<=30000); Trials++; lcd.setCursor(0,1); for (int i=0;i<15;i++) lcd.write(32); if (t > 30000) { // Display answer lcd.setCursor(0,1); lcd.print(Conundrum); } else { // User enter guess c = 0; g = 0; lcd.cursor(); lcd.setCursor(0,0); do delay(100); while(ButtonAPushed()); do { time = millis(); do { if (ButtonAPushed()) { c = ScanCursor(c); do delay(100); while(ButtonAPushed()); } if (millis()-time > 15000) { Sleep(); time = millis(); // Restore display lcd.print(Scramble); lcd.cursor(); lcd.setCursor(c,0); } } while (!ButtonBPushed()); // Copy 8 letters CopyGuess(c, g); c = ScanCursor(c); g++; do delay(100); while (ButtonBPushed()); } while (g<8); // Copy last letter automatically CopyGuess(c, g); lcd.noCursor(); if (TestGuess()) { lcd.setCursor(10,1); lcd.print("Yes!"); Score++; } else { delay(500); lcd.setCursor(0,1); lcd.print(Conundrum); } } // Display score lcd.setCursor(10,0); lcd.print(Score*100/Trials); lcd.write('%'); }