//Conectare display la placa UNO: //VCC-->5V //GND-->GND //SDA-->D11 //RES-->D9 //DC-->D8 //CS-->D3 //BL--> liber (neconectat) //Cod Arduino IDE: #include // Core graphics library #include // Hardware-specific library for ST7735 #include // Hardware-specific library for ST7789 #include #include #include // For the breakout board, you can use any 2 or 3 pins. // These pins will also work for the 1.8" TFT shield. #define TFT_CS 3 #define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin #define TFT_DC 8 #define IR_RECV_PIN 7 Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST); byte row = 0; byte column = 0; char prev = '0' ; char word_to_find[6] = "00000"; char current_word[6] = "00000"; bool win = false; bool lose = false; void setup(void) { Serial.begin(9600); Serial.println(F("LCD screen initialising...")); pinMode(TFT_CS, OUTPUT); // sets the pin as output analogWrite(TFT_CS, 0); // Use this initializer if using a 1.8" TFT screen: tft.initR(INITR_BLACKTAB); // Init ST7735S chip, black tab tft.setSPISpeed(40000000); Serial.println(F("Done.")); tft.fillScreen(ST77XX_BLACK); tft.setTextWrap(true); tft.setTextSize(2); delay(500); // large block of text tft.fillScreen(ST77XX_BLACK); initGrid(); blinkSquare(0,0); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } Serial.print("SD card initializing ..."); if (!SD.begin(10)) { Serial.println("initialization failed!"); while (1); } Serial.println("Done."); // open the file for reading: IrReceiver.begin(IR_RECV_PIN, ENABLE_LED_FEEDBACK); // Start the receiver Serial.println("IR remote ready."); randomSeed(analogRead(11)); getRandomWord(); Serial.println(word_to_find); current_word[5] = 0; } void loop() { bool empty = false; if (IrReceiver.decode() && win == false && lose == false) { bool incomplete; switch(IrReceiver.decodedIRData.decodedRawData){ case 0xEA15FF00: win = false; lose = false; for(byte i = 0 ; i < 5 ; i ++ ){ current_word[i] = '0'; word_to_find[i] = '0'; } row = 0; column = 0; prev = '0' ; if(empty == false){ tft.fillScreen(ST77XX_BLACK); empty = true; } initGrid(); randomSeed(analogRead(11)); getRandomWord(); Serial.println(word_to_find); //IrReceiver.decodedIRData.decodedRawData; break; case 0xBC43FF00: empty = true; incomplete = false; for(byte i = 0 ; i < 5 ; i ++){ if(current_word[i] == '0'){ incomplete = true; } } if(incomplete) break; char filename[6]; filename[0] = current_word[0]; filename[1] = '.'; filename[2] = 't'; filename[3] = 'x'; filename[4] = 't'; filename[5] = 0; Serial.println(filename); Serial.println(current_word); Serial.println(word_to_find); if(findWord(current_word,filename) == true){ deleteBlink(row,column); typeLetter(current_word[row]-32); blinkSquare(0,column + 1); win = colorCode(current_word); column += 1; if(column == 5 && win == false) lose = true; row = 0; //IF WORD_TO_FIND == CURENT_WORD => YOU WIN + NEW GAME //ELSE YOU LOSE + NEW GAME for(byte i = 0 ; i < 5 ; i ++) current_word[i] = '0'; current_word[5] = 0; } else { //RESET ROW deleteBlink(row,column); row = 0; for(byte i = 0 ; i < 5; i ++) { deleteLetter(current_word[i]); row += 1; } row = 0; for(byte i = 0 ; i < 5; i ++) current_word[i] = '0'; } if(win == true) writeStatus("You won!"); if(lose == true) writeStatus("You lost!"); break; case 0xF30CFF00: if(prev == 'A' || prev == 'B'){ typeLetter(prev+1); prev += 1; } else { typeLetter('A'); prev = 'A'; } break; case 0xE718FF00: if(prev == 'D' || prev == 'E'){ typeLetter(prev+1); prev += 1; } else { typeLetter('D'); prev = 'D'; } break; case 0xA15EFF00: if(prev == 'G' || prev == 'H'){ typeLetter(prev + 1); prev += 1; } else { typeLetter('G'); prev = 'G'; } break; case 0xF708FF00: if(prev == 'J'|| prev =='K'){ typeLetter(prev + 1); prev += 1; } else { typeLetter('J'); prev = 'J'; } break; case 0xE31CFF00: if(prev == 'M' || prev == 'N'){ typeLetter(prev + 1); prev += 1; } else { typeLetter('M'); prev = 'M'; } break; case 0xA55AFF00: if(prev == 'P' || prev == 'Q'){ typeLetter(prev + 1); prev += 1; } else { typeLetter('P'); prev = 'P'; } break; case 0xBD42FF00: if(prev == 'S' || prev == 'T'){ typeLetter(prev + 1); prev += 1; } else { typeLetter('S'); prev = 'S'; } break; case 0xAD52FF00: if(prev == 'V' || prev == 'W'){ typeLetter(prev + 1); prev += 1; } else { typeLetter('V'); prev = 'V'; } break; case 0xB54AFF00: if(prev == 'Y'){ typeLetter(prev + 1); prev += 1; } else { typeLetter('Y'); prev = 'Y'; } break; case 0xBF40FF00: if(row < 4) { deleteBlink(row, column); if(current_word[row]!='0') typeLetter(current_word[row] - 32); row += 1; blinkSquare(row, column); if(current_word[row]!='0') typeLetter(current_word[row] - 32); prev = '0'; } break; case 0xBB44FF00: if(row > 0) { deleteBlink(row, column); if(current_word[row]!='0') typeLetter(current_word[row] - 32); row -= 1; blinkSquare(row, column); if(current_word[row]!='0') typeLetter(current_word[row] - 32); prev ='0'; } break; case 0xF807FF00: if(current_word[row] != '0' ) deleteLetter(current_word[row]); current_word[row] = '0'; break; } IrReceiver.resume(); // Enable receiving of the next value } } boolean findWord(char* string, char* filename){ File myFile; myFile = SD.open(filename); if (myFile) { while (myFile.available()) { boolean ok = true; for(byte i = 0; i < 5; i ++){ char aux_string = myFile.read(); if(aux_string != string[i]){ ok = false; } } for(byte i = 0 ; i < 2; i ++){ myFile.read(); } if(ok == true){ return true; } } // close the file: myFile.close(); } else { // if the file didn't open, print an error: Serial.println("error opening hello.txt"); } return false; } void getRandomWord(){ Serial.println("Generating word"); int random_nr = random(2309); Serial.println(random_nr); File myFile; myFile = SD.open("wordle.txt"); if (myFile) { myFile.seek(random_nr*7); for(byte i = 0 ; i < 5; i++) { word_to_find[i] = myFile.read(); } word_to_find[5] = 0; myFile.close(); } } bool colorCode(char *string) { byte count = 0; for(byte i = 0 ; i < 5; i ++) { tft.setCursor(i * 25 + 10, column * 20 + 10); if(string[i] == word_to_find[i]) { tft.setTextColor(ST77XX_GREEN); tft.print((char)(string[i] - 32)); tft.setTextColor(ST77XX_WHITE); count += 1; } else if (letterInWord(string[i])){ tft.setTextColor(ST77XX_ORANGE); tft.print((char)(string[i] - 32)); tft.setTextColor(ST77XX_WHITE); } } if(count == 5) return true; return false; } boolean letterInWord(char letter) { for(byte i = 0 ; i < 5; i ++) { if(letter == word_to_find[i]) return true; } return false; } void typeLetter(const char letter) { if(column < 7){ if(current_word[row] != '0') deleteLetter(current_word[row]); tft.setCursor(row * 25 + 10, column * 20 + 10); tft.setTextColor(ST77XX_WHITE); tft.print(letter); current_word[row] = letter + 32; } } void deleteLetter(char letter) { tft.setCursor(row * 25 + 10, column * 20 + 10); tft.setTextColor(ST77XX_BLACK); tft.print(char(letter - 32)); tft.setTextColor(ST77XX_WHITE); } void writeStatus(const char* string){ tft.setTextSize(1.5); tft.setCursor(40, 130); tft.print(string); tft.setCursor(5, 145); tft.print("Press + for new game"); tft.setTextSize(2); } void blinkSquare(int n, int m) { for(int i = n*25 +20 ; i < n*25 + 30 ; i ++) for(int j = m*20 +10; j < m * 20 + 25; j++) tft.drawPixel(i,j,0xF800); } void deleteBlink(int n, int m) { for(int i = n*25+20 ; i < n*25 + 30 ; i ++) for(int j = m*20+10 ; j < m * 20 + 25; j++) tft.drawPixel(i,j,0x0000); } void initGrid() { tft.setCursor(10, 10); tft.setTextColor(ST77XX_WHITE); for (int16_t x=7; x < 7+20*6; x+=20) { tft.drawLine(5, x, 25*5, x, ST77XX_WHITE); } for (int16_t x=5; x < 25*6; x+=25) { tft.drawLine(x, 7, x, 106, ST77XX_WHITE); } }