Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Uno R3-based Rock-Paper-Scissors Game with OLED Display

View and Edit Project In Cirkit DesignerImage of Arduino Uno R3-based Rock-Paper-Scissors Game with OLED Display

Circuit Documentation

Summary of the Circuit

This circuit is designed to interface an Arduino Uno R3 microcontroller with a 0.96" OLED display and three pushbuttons. The pushbuttons are used to input a player's choice in a game of Rock-Paper-Scissors, which is then displayed on the OLED. The Arduino controls the game logic and the OLED display, which shows the choices and the outcome of the game. Each pushbutton is connected to the Arduino through a 1000 Ohm resistor, and the OLED communicates with the Arduino via the I2C protocol.

Component List

Pushbutton

Resistor

0.96" OLED

Arduino Uno R3

Wiring Details

Pushbutton

Resistor

0.96" OLED

Arduino Uno R3

Documented Code

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const int LeftButtonPin = 2;
const int MiddleButtonPin = 3;
const int RightButtonPin = 4;

int playerchoice, compchoice;

void setup() {
  pinMode(LeftButtonPin, INPUT);
  pinMode(MiddleButtonPin, INPUT);
  pinMode(RightButtonPin, INPUT);

  Serial.begin(115200);
  randomSeed(analogRead(0));

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
}

void loop() {
  playerchoice = 0;
  compchoice = 0;

  display.setTextSize(1);
  display.setTextColor(WHITE);
  
  display.setCursor(10,0); // 128  x 64
  display.println("Lets Play a Game!");
  display.setCursor(23,15);
  display.println("(choose one)");
  display.setCursor(0,30);
  display.println("ROCK PAPER or SCISSOR");
  display.setCursor(0,45);
  display.println("LEFT MIDDLE or RIGHT");

  display.display();

  while (playerchoice == 0)
  { 
    if (digitalRead(LeftButtonPin) == HIGH)
    {
    playerchoice = 1;
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("You Chose");
    display.setCursor(5,15);
    display.println("Rock");
    display.display();
    }
  
    if (digitalRead(MiddleButtonPin) == HIGH)
    {
    playerchoice = 2;
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("You Chose");
    display.setCursor(5,15);
    display.println("Paper");
    display.display();
    }

    if (digitalRead(RightButtonPin) == HIGH)
    {
    playerchoice = 3;
    display.clearDisplay();
    display.setCursor(0,0);
    display.println("You Chose");
    display.setCursor(5,15);
    display.println("Scissor");
    display.display();
    } 

  }

  display.drawLine(60, 0, 60, 29, WHITE);
  display.drawLine(0, 29, 128, 29, WHITE);
  display.display();  

  delay(500);

  compchoice = random(1, 4);

    if (compchoice==1)
    {
    display.setCursor(68,0);
    display.println("Comp Chose");
    display.setCursor(73,15);
    display.println("Rock");
    display.display();
    }

    if (compchoice==2)
    {
    display.setCursor(68,0);
    display.println("Comp Chose");
    display.setCursor(73,15);
    display.println("Paper");
    display.display();
    } 

    if (compchoice==3)
    {
    display.setCursor(68,0);
    display.println("Comp Chose");
    display.setCursor(73,15);
    display.println("Scissor");
    display.display();
    } 

  delay(1000);

  if(playerchoice==compchoice)
  {
    display.setCursor(32, 40);
    display.println("Its a Tie!");
    display.display(); 
  } 
  else if ((playerchoice == 1 && compchoice == 3) ||
           (playerchoice == 2 && compchoice == 1) ||
           (playerchoice == 3 && compchoice == 2))
  {
    display.setCursor(32, 40);
    display.println("You Won!");
    display.display(); 
  }
  else
  {
    display.setCursor(32, 40);
    display.println("Comp Won!");
    display.display(); 
  }

  delay(2000);  
  display.clearDisplay();

  //display.drawBitmap(75, 20, middle_finger, 50, 50, 1);
  
}

This code is responsible for the game logic and interfacing with the OLED display and pushbuttons. It initializes the display, reads the button states, and displays the game's outcome.