Components and supplies
ESP32
2.9inch E-Ink display module
Project description
Code
e-ink_connect4
arduino
1#define ENABLE_GxEPD2_GFX 0 2 3#include <GxEPD2_BW.h> // including both doesn't use more code or ram 4#include <GxEPD2_3C.h> // including both doesn't use more code or ram 5#include <U8g2_for_Adafruit_GFX.h> 6 7GxEPD2_BW<GxEPD2_290_T94_V2, GxEPD2_290_T94_V2::HEIGHT> display(GxEPD2_290_T94_V2(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEM029T94, Waveshare 2.9" V2 variant 8U8G2_FOR_ADAFRUIT_GFX u8g2Fonts; 9 10uint16_t bg = GxEPD_WHITE; //white 11uint16_t fg = GxEPD_BLACK; //black 12int x = 150; 13int mat[6][7] = {{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0}}; //here the placement is stored. 0 --> not occupied; 1 --> playerA; 2 --> playerB 14int player = 1; //we begin with playerA 15bool win = false; 16 17bool touch1detected = false; //variables to indicate if a touch pin is high 18bool touch2detected = false; 19 20void gotTouch2(){ 21 touch2detected = true; 22} 23void gotTouch1(){ 24 touch1detected = true; 25} 26 27void place(int player, int x){ //this function fills in the circles/crosses the players place 28 int col = (x - 132)/18 - 1; 29 display.setPartialWindow(145, 17, 296, 128); 30 display.firstPage(); 31 32 for(int i = 5; i >= 0; i--){ 33 if(mat[i][col] == 0){ 34 mat[i][col] = player; 35 break; 36 } 37 } 38 39 40 do{ 41 display.drawRect(150, 18, 126, 108, fg); 42 display.drawLine(150, 36, 274, 36, fg); 43 display.drawLine(150, 54, 274, 54, fg); 44 display.drawLine(150, 72, 274, 72, fg); 45 display.drawLine(150, 90, 274, 90, fg); 46 display.drawLine(150, 108, 274, 108, fg); 47 display.drawLine(168, 18, 168, 124, fg); 48 display.drawLine(186, 18, 186, 124, fg); 49 display.drawLine(204, 18, 204, 124, fg); 50 display.drawLine(222, 18, 222, 124, fg); 51 display.drawLine(240, 18, 240, 124, fg); 52 display.drawLine(258, 18, 258, 124, fg); 53 54 for(int i = 0; i < 6; i++){ 55 for(int j = 0; j < 7; j++){ 56 if(mat[i][j] == 1){ 57 display.fillCircle(159+j*18, 27+i*18, 5, fg); 58 } 59 else if(mat[i][j] == 2){ 60 display.fillRect(157+j*18, 20+i*18, 4, 14, fg); 61 display.fillRect(152+j*18, 25+i*18, 14, 4, fg); 62 } 63 } 64 } 65 } 66 while(display.nextPage()); 67} 68 69 70bool check_if_4(){ //this function checks if theres a line of four pieces 71 for(int i = 0; i < 3; i++){ 72 for(int j = 0; j < 7; j++){ 73 if(mat[i][j] == mat[i+1][j] && mat[i+1][j] == mat[i+2][j] && mat[i+2][j] == mat[i+3][j] && mat[i][j] != 0){ 74 return true; 75 } 76 } 77 } 78 79 for(int i = 0; i < 6; i++){ 80 for(int j = 0; j < 4; j++){ 81 if(mat[i][j] == mat[i][j+1] && mat[i][j+1] == mat[i][j+2] && mat[i][j+2] == mat[i][j+3] && mat[i][j] != 0){ 82 return true; 83 } 84 } 85 } 86 87 for(int i = 0; i < 3; i++){ 88 for(int j = 0; j < 4; j++){ 89 if(mat[i][j] == mat[i+1][j+1] && mat[i+1][j+1] == mat[i+2][j+2] && mat[i+2][j+2] == mat[i+3][j+3] && mat[i][j] != 0){ 90 return true; 91 } 92 } 93 } 94 95 for(int i = 0; i < 3; i++){ 96 for(int j = 3; j < 7; j++){ 97 if(mat[i][j] == mat[i+1][j-1] && mat[i+1][j-1] == mat[i+2][j-2] && mat[i+2][j-2] == mat[i+3][j-3] && mat[i][j] != 0){ 98 return true; 99 } 100 } 101 } 102 103 return false; 104} 105 106 107void(* resetFunc) (void) = 0; //resets the game if someone won 108 109 110void setup() 111{ 112 touchAttachInterrupt(T3, gotTouch2, 40); //the 40 sets the sensitivity of the touchpin 113 touchAttachInterrupt(T4, gotTouch1, 40); //bigger number --> higher sensitivity 114 115 display.init(); 116 display.setTextColor(GxEPD_BLACK); 117 118 display.firstPage(); 119 display.setRotation(1); //rotate the screen 90° 120 121 u8g2Fonts.begin(display); // connect u8g2 procedures to Adafruit GFX 122 delay(1000); 123 124 u8g2Fonts.setFont(u8g2_font_logisoso16_tr); 125 126 u8g2Fonts.setForegroundColor(fg); // apply Adafruit GFX color 127 u8g2Fonts.setBackgroundColor(bg); 128 129 do 130 { 131 display.fillScreen(GxEPD_WHITE); 132 u8g2Fonts.setCursor(10, 30); 133 u8g2Fonts.println("Connect 4"); //title 134 u8g2Fonts.setCursor(10, 80); 135 u8g2Fonts.println("Player A"); 136 137 138 display.drawRect(150, 18, 126, 108, fg); //print the grid 139 display.drawLine(150, 36, 274, 36, fg); 140 display.drawLine(150, 54, 274, 54, fg); 141 display.drawLine(150, 72, 274, 72, fg); 142 display.drawLine(150, 90, 274, 90, fg); 143 display.drawLine(150, 108, 274, 108, fg); 144 display.drawLine(168, 18, 168, 124, fg); 145 display.drawLine(186, 18, 186, 124, fg); 146 display.drawLine(204, 18, 204, 124, fg); 147 display.drawLine(222, 18, 222, 124, fg); 148 display.drawLine(240, 18, 240, 124, fg); 149 display.drawLine(258, 18, 258, 124, fg); 150 } 151 while (display.nextPage()); 152} 153 154 155void loop(){ 156 157 display.setPartialWindow(150, 0, 126, 15); 158 display.firstPage(); 159 160 do{ 161 display.fillTriangle(x, 4, x+18, 4, x+9, 13, fg); 162 } 163 while(display.nextPage()); 164 165 delay(100); 166 touch2detected = false; 167 touch1detected = false; 168 while(!touch2detected && !touch1detected){delay(10);} //the program waits until there is a touch input 169 if(touch1detected){ 170 place(player, x); 171 172 if(check_if_4()){ //if true someone connected 4 --> the game is over 173 display.setPartialWindow(0, 35, 140, 98); 174 display.firstPage(); 175 176 do 177 { 178 display.fillScreen(GxEPD_WHITE); 179 u8g2Fonts.setCursor(10, 80); 180 if(player == 1){u8g2Fonts.println("Player A won");} 181 else{u8g2Fonts.println("Player B won");} 182 } 183 while (display.nextPage()); 184 delay(3000); 185 resetFunc(); //reset the program 186 } 187 188 if(player == 1){player = 2;} //switch players 189 else{player = 1;} 190 191 display.setPartialWindow(0, 35, 140, 98); 192 display.firstPage(); 193 194 do 195 { 196 display.fillScreen(GxEPD_WHITE); 197 u8g2Fonts.setCursor(10, 80); 198 if(player == 1){u8g2Fonts.println("Player A");} 199 else{u8g2Fonts.println("Player B");} 200 } 201 while (display.nextPage()); 202 } 203 204 else{ 205 x += 18; 206 if(x > 258){x = 150;} 207 } 208 209}
Downloadable files
Wiring
Unfortunately I can't create a Fritzing file because the parts are not available there. Just connect all the wires so that the colors match. You also have to look at the pinout of your specific ESP32 board.
Wiring
Wiring
Unfortunately I can't create a Fritzing file because the parts are not available there. Just connect all the wires so that the colors match. You also have to look at the pinout of your specific ESP32 board.
Wiring
Comments
Only logged in users can leave comments
galoebn
5 Followers
•9 Projects
0
0