/* Bulls and Cows v2 David Johnson-Davies - www.technoblogy.com - 10th March 2015 CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ // Seven-segment definitions const int charArrayLen = 36; char charArray[charArrayLen] = { // ABCDEFG Segments 0b1111110, // 0 0b0110000, // 1 0b1101101, // 2 0b1111001, // 3 0b0110011, // 4 0b1011011, // 5 0b1011111, // 6 0b1110000, // 7 0b1111111, // 8 0b1111011, // 9 0b0000000, // 10 Space 0b0000001, // 11 Dash 0b0110000, // 12 'I' 0b0000000, // 13 ' ' 0b0001110, // 14 'L' 0b1111110, // 15 'O' 0b1011011, // 16 'S' 0b1001111, // 17 'E' 0b0110111, // 18 'H' 0b1111110, // 19 'O' 0b0000001, // 20 '-' 0b0110111, // 21 'H' 0b1111110, // 22 'O' 0b0000000, // 23 ' ' 0b1001111, // 24 'E' 0b0000101, // 25 'r' 0b0000101, // 26 'r' 0b0011101, // 27 'o' 0b0000101, // 28 'r' 0b0000000, // 29 ' ' 0b0110111, // 30 'H' 0b1001111, // 31 'E' 0b0001110, // 32 'L' 0b0001110, // 33 'L' 0b1111110, // 34 'O' 0b0000000 // 35 ' ' }; const int Dash = 11; const int Space = 10; const int rChar = 25; const int ILose = 12; const int HoHo = 18; const int Error = 24; const int Hello = 30; const int Ndigits = 6; volatile char Buffer[] = { Dash, Dash, Dash, Dash, Dash, Dash}; volatile char dp = 0; // Decimal point position; one bit per digit char digit = 0; // Pin assignments char Digits[] = {9, 10, 11, A4, 8, A5}; // Segments all go to port D; this specifies how they're wired up: // g f e d c b a dp char Segments[] = {0, 7, 4, 3, 1, 6, 5, 2}; // Keyboard matrix volatile char Keys[6]; char Matrix[] = {0, 1, 10, 0, 7, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0}; // Display multiplexer ********************************************** void ReorderBits() { char segs, newsegs; for (int i=0; i> 1; } charArray[i]=newsegs; } } void DisplayNextDigit() { char segs, row; pinMode(Digits[digit], INPUT); digitalWrite(Digits[digit], LOW); digit = (digit+1) % Ndigits; segs = charArray[Buffer[digit]]; // Decimal points if ((dp & 1<=0 ; d--) { Buffer[d]=i % 10; i = i / 10; } dp = 1<<3; } // Display message void ShowMessage (int message) { for (int d=0; d<6 ; d++) Buffer[d]=message + d; dp = 0; WaitForGo(); } void ClearDisplay () { for (int d=0; d<6 ; d++) Buffer[d] = Space; // Show all decimal points dp= 0x3F; } // Timer interrupt - multiplexes display ISR (TIMER1_COMPA_vect) { DisplayNextDigit(); } // Play Bulls and Cows ********************************************** // Display turn prompt void Prompt (int i) { int j = (i + 1) / 10; if (j == 0) Buffer[4] = rChar; else Buffer[4] = j; Buffer[5] = (i + 1) % 10; dp = 1<<3; } // Display score - Bulls and Cows void ShowScore (int i) { Buffer[4]=i / 10; Buffer[5]=i % 10; } // Read a four-digit guess or two-digit reply, display and return it int Read (int length) { int key, result = 0, first; if (length == 4) first = 0; else first = 4; int last = first + length; int d = first; for (int d=first; d= 0) cows++; } a = a / 10; b = b / 10; } return bulls*10 + cows; } // Setup and main loop ********************************************** void setup() { ReorderBits(); // Set up Timer1 to multiplex the display TCCR1A = 0<