Components and supplies
Graphic OLED, 128 x 64
Resistor 1k ohm
Arduino UNO
Jumper wires (generic)
Tactile Switch, Top Actuated
Breadboard (generic)
Tools and machines
Brain
Apps and platforms
Arduino IDE
Project description
Code
Code
c_cpp
1#include <SPI.h> 2#include <Wire.h> 3#include <Adafruit_GFX.h> 4#include <Adafruit_SSD1306.h> 5#include <stdlib.h> 6 7int gameMode; 8bool game= true; 9long int loopCount=1; 10int p1; 11int p2; 12 13#define SCREEN_WIDTH 128 // OLED display width, in pixels 14#define SCREEN_HEIGHT 64 // OLED display height, in pixels 15#define but1 2 16#define but2 4 17#define but3 7 18 19#define ROCK 1 20#define PAPER 2 21#define SCISSOR 3 22 23// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 24Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); 25 26const unsigned char myBitmap[] = { /* bmp */ }; 27 28static const unsigned char PROGMEM rps[] = { 29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 30 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x03, 0x00, 0x00, 0x00, 31 0x00, 0x00, 0x01, 0xC0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 32 0x00, 0x00, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x70, 0x00, 0x00, 33 0x00, 0x00, 0x06, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x30, 0x00, 0x00, 34 0x00, 0x00, 0x0E, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x30, 0x00, 0x00, 35 0x00, 0x00, 0x0C, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x40, 0x00, 0x30, 0x00, 0x00, 36 0x00, 0x00, 0x0E, 0xE0, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x0E, 0xE0, 0x00, 0x30, 0x00, 0x00, 37 0x00, 0x00, 0x07, 0xE0, 0x03, 0x30, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x23, 0xB0, 0x00, 0x00, 38 0x00, 0x00, 0x01, 0xE0, 0x73, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE6, 0x77, 0xE0, 0x00, 0x00, 39 0x00, 0x00, 0x00, 0xC7, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCF, 0x3E, 0x00, 0x00, 0x00, 40 0x00, 0x00, 0x00, 0xCF, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xCF, 0x38, 0x00, 0x00, 0x00, 41 0x00, 0x00, 0x01, 0xCF, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9F, 0x98, 0x00, 0x00, 0x00, 42 0x00, 0x00, 0x01, 0x9D, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x99, 0x9C, 0x00, 0x00, 0x00, 43 0x00, 0x00, 0x03, 0x99, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x39, 0xCC, 0x00, 0x00, 0x00, 44 0x00, 0x00, 0x03, 0xB9, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF0, 0xFC, 0x00, 0x00, 0x00, 45 0x00, 0x00, 0x01, 0xE0, 0x78, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 46 0x00, 0x03, 0x80, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x00, 0x07, 0xE7, 0x18, 0x00, 47 0x00, 0x0F, 0xFC, 0x00, 0x0F, 0xE3, 0xFE, 0x00, 0x00, 0x1C, 0x7E, 0x00, 0x0E, 0x71, 0xFF, 0x00, 48 0x00, 0x38, 0xF7, 0xC0, 0x0E, 0x38, 0xE3, 0x80, 0x00, 0x70, 0xE7, 0xE0, 0x1F, 0x1C, 0x71, 0xC0, 49 0x00, 0xE0, 0x07, 0x70, 0x3F, 0x8E, 0x38, 0xE0, 0x01, 0xC0, 0x06, 0x70, 0x3B, 0xC7, 0x1C, 0x70, 50 0x01, 0xC0, 0x00, 0x78, 0x39, 0xE7, 0x8E, 0x38, 0x03, 0x80, 0x00, 0xFC, 0x1C, 0xF3, 0xC7, 0x1C, 51 0x03, 0x80, 0x00, 0x5C, 0x1E, 0x79, 0xC2, 0x0C, 0x03, 0x00, 0x00, 0x0C, 0x3F, 0x3C, 0x00, 0x00, 52 0x03, 0x00, 0x00, 0x1C, 0x77, 0x9E, 0x00, 0x00, 0x03, 0x00, 0x00, 0x38, 0x73, 0xCE, 0x00, 0x00, 53 0x03, 0x00, 0x00, 0x78, 0x71, 0xC0, 0x00, 0x00, 0x07, 0x00, 0x00, 0xFC, 0x38, 0xE0, 0x00, 0x00, 54 0x0E, 0x00, 0x01, 0xCC, 0x1C, 0x70, 0x00, 0x00, 0x1C, 0x00, 0x01, 0x8C, 0x0E, 0x00, 0x00, 0x00, 55 0x38, 0x00, 0x00, 0x1C, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x38, 0x03, 0x80, 0x00, 0x00, 56 0x00, 0x00, 0x00, 0x70, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0xE0, 0x00, 0x00, 57 0x00, 0x00, 0x01, 0xC0, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x80, 0x00, 0x38, 0x00, 0x00, 58 0x00, 0x01, 0xFF, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x03, 0xFC, 0x00, 0x00, 0x0F, 0xF0, 0x00, 59 0x00, 0x07, 0x00, 0x00, 0x00, 0x07, 0xFE, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00, 60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 61}; 62 63void setup() { 64 Serial.begin(115200); 65 pinMode(but1, INPUT); 66 pinMode(but2, INPUT); 67 pinMode(but3, INPUT); 68} 69 70void loop() { 71 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 72 Serial.println(F("SSD1306 allocation failed")); 73 for(;;); 74 } 75 delay(1000); 76 display.clearDisplay(); 77 drawCircles(); 78 display.display(); 79 // delay(5000); 80 display.clearDisplay(); 81 82 83 if(loopCount == 1) { 84 display.clearDisplay(); 85 displayOLED(1,27,20,"IoT Project"); 86 displayOLED(1,12,30,"Rock Paper Scissor"); 87 // display.drawRect(1,1,126,62,WHITE); 88 display.display(); 89 int temp = readButt(); 90 delay(500); //Wait 5 Sec 91 display.clearDisplay(); 92 display.drawBitmap(31 , 0, rps, 64, 64,1); 93 // display.invertDisplay(true); 94 display.display(); 95 delay(5000); 96 } 97 98 display.clearDisplay(); 99 displayOLED(1,10,20,"1.Single Player"); 100 displayOLED(1,10,30,"2.Two Players"); 101 display.clearDisplay(); 102 103 104 gameMode = readButt(); //Check which mode is selected 105 display.clearDisplay(); 106 if(gameMode==1) displayOLED(1,20,20,"Single Player"); 107 else if(gameMode==2) displayOLED(1,20,20,"Two Players"); 108 else displayOLED(1,20,20,"Baryon Mode"); 109 delay(2000); 110 111game = true; 112do{ 113 display.clearDisplay(); 114 displayOLED(2,10,20,"Player 1"); 115 displayOLED(1,10,35,"Rock Paper Scissor"); 116 117 p1 = readButt(); 118 delay(1000); 119 120 display.clearDisplay(); 121 displayOLED(2,10,20,"Player 2"); 122 displayOLED(1,10,35,"Rock Paper Scissor"); 123 if(gameMode==1) { 124 p2 = askAI(); 125 } else p2 = readButt(); 126 delay(1000); 127 128 game = checkWin(p1,p2); //checking for winner 129 delay(3000); 130 131 }while(game == false); 132 loopCount++; 133} 134 135void displayOLED(int textSize,int setX, int setY, char str[20]) { 136 display.setFont(); 137 display.setTextColor(WHITE); 138 display.setTextSize(textSize); 139 display.setTextColor(WHITE); 140 display.setCursor(setX, setY); 141 display.print(str); 142 display.display(); 143} 144 145void drawCircles(void) { 146 display.clearDisplay(); 147 for(int16_t i=5; i<90; i+=5) { 148 display.drawCircle(63,31, i, WHITE); 149 display.display(); 150 delay(10); 151 display.clearDisplay(); 152 } 153} 154 155int readButt() { 156 // gameMode = 0; 157 // p1=0; 158 int val=0; 159 while(1) { 160 if(digitalRead(but1)==HIGH && digitalRead(but2)==LOW && digitalRead(but3)==LOW) { 161 Serial.println("returning 1 "); 162 val = 1; 163 break; 164 } 165 if(digitalRead(but1)==LOW && digitalRead(but2)==HIGH && digitalRead(but3)==LOW) { 166 Serial.println("returning 2"); 167 val = 2; 168 break; 169 } 170 if(digitalRead(but1)==LOW && digitalRead(but2)==LOW && digitalRead(but3)==HIGH) { 171 Serial.println("returning 3 "); 172 val = 3; 173 break; 174 } 175 } 176 return val; 177} 178 179int askAI() { 180 int srand = (rand()%3) + 1; 181 Serial.print("returning "); 182 Serial.println(srand); 183 return srand; 184} 185 186bool checkWin(int p1, int p2) { 187 display.clearDisplay(); 188 if(p1==p2) { 189 displayOLED(2,20,20,"Draw"); 190 displayOLED(1,20,35,"Restart"); 191 return false; 192 } 193 if(p1==ROCK && p2==PAPER) { 194 displayOLED(1,20,20,"Rock looses to Paper"); 195 return true; 196 } else if(p1==ROCK && p2==SCISSOR) { 197 displayOLED(1,20,20,"Rock beats Scissor"); 198 return true; 199 } else if(p1==PAPER && p2==ROCK) { 200 displayOLED(1,20,20,"Paper beats Rock"); 201 return true; 202 } else if(p1==PAPER && p2==SCISSOR) { 203 displayOLED(1,20,20,"Paper looses to Scissor"); 204 return true; 205 } else if(p1==SCISSOR && p2==ROCK) { 206 displayOLED(1,20,20,"Scissor looses to Rock"); 207 return true; 208 } else if(p1==SCISSOR && p2==PAPER) { 209 displayOLED(1,20,20,"Scissor beats Paper"); 210 return true; 211 } 212} 213 214
Downloadable files
Connections
This is the picture after connecting all the components before packing it into a small box.
Connections
Documentation
Wokwi Simulator
This is the circuit screenshot where I simulated the project.
Wokwi Simulator
Comments
Only logged in users can leave comments