Skip to content
International Shipping
Wish Lists Cart
0 items

Two Player Thermometer Game

05 Dec 2023
Two Player Thermometer Game

Here's a summary of STEM Winter Activity: A Picobricks Mainboard with temperature and humidity sensors measures the ambient temperature for you, but keeps it secret from the players. Then players take it in turns to enter their temperature predictions. The closest guess wins the game.

Hello, future meteorologists! Let me introduce you to a great (literally!) project that is not only fun but also educational: Thermometer Game powered by PicoBricks. This game is the perfect mix of technology, learning and of course friendly competition. Perfect for cold winter days, this game is a STEM Winter activity that really warms up your brain cells!

Why This Game?

  • Educational: It’s a hands on way to learn about sensors and how they interact with the environment.
  • Seasonal Fun: Especially great for winter, this game adds a STEM twist to those long, cold days.
  • Unit Conversion Made Easy: Players get to understand the difference between Celsius and Fahrenheit in a practical, engaging way.

 

Wiring Diagram

How to Make? Step by Step Guide

Step 1

Your first mission? Install the necessary code in your PicoBricks. Once that's done, the fun begins.

Don't worry if you don't know how to do it. To learn how to use Picobricks with BricksIDE, ThonnyPhyton and Arduino C: How to Use Picobricks?

BricksIDE

Here is Bricks IDE code.Download and install on your PicoBricks.

Download BricksIDE Code

MicroBlocks Code

ThonnyPhyton Code


from time import sleep
from machine import Pin,PWM,I2C
from picobricks import WS2812, DHT11,SSD1306_I2C
import machine
import time
import math



ws2812 = WS2812(6, brightness = 1)
pin_button = machine.Pin(10, machine.Pin.IN)
dht_sensor = DHT11(Pin(11))

dht_read_time = time.time()

temp = 0

def getTemp():
   global dht_read_time
   global temp
   if time.time() - dht_read_time >= 3:
       dht_read_time = time.time()
       try:
           dht_sensor.measure()
           temp = dht_sensor.temperature
       except Exception as e:
           pass
   return temp

import math
pot = machine.ADC(26)
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000)
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)

def SetTempF():
    global tempUnite, temperatureValue, player1guess, player2guess, player1result, counter, player2result
    tempUnite = round(round( pot.read_u16() - 0 ) * ( 2 - 1 ) / ( 65535 - 0 ) + 1)
    if tempUnite == (1):
        oled.text("{}".format("celsius"), 20, 25)
    elif tempUnite == (2):
        oled.text("{}".format("Fahrenheit"), 20, 25)
    oled.show()
    oled.fill(0)

def start():
    global tempUnite, temperatureValue, player1guess, player2guess, player1result, counter, player2result
    oled.text("{}".format("Set the "), 20, 10)
    oled.text("{}".format("temperature "), 20, 20)
    oled.text("{}".format("unit by POT"), 20, 30)
    oled.show()

def countDown():
    global tempUnite, temperatureValue, player1guess, player2guess, player1result, counter, player2result
    for count in range((4)):
        oled.fill(0)
        oled.text("{}".format(counter), 55, 32)
        oled.show()
        counter -= 1
        time.sleep((1))


    oled.fill(0)
    oled.show()
    counter = 0

pin_led = machine.Pin(7, machine.Pin.OUT)
buzzer = PWM(Pin(20))

def buttonFunc():
    global tempUnite, temperatureValue, player1guess, player2guess, player1result, counter, player2result
    pin_led.on()
    buzzer.freq(300)
    buzzer.duty_u16(100)
    sleep(0.25)
    buzzer.duty_u16(0)
    pin_led.off()

def player1():
    global tempUnite, temperatureValue, player1guess, player2guess, player1result, counter, player2result
    while (pin_button.value()) == (0) and counter == (0):
        player1guess = round(round( pot.read_u16() - 0 ) * ( 212 - -20 ) / ( 65535 - 0 ) + -20)
        oled.text("{}".format("Player 1: "), 5, 25)
        oled.text("{}".format(player1guess), 85, 25)
        oled.show()
        oled.fill(0)
        print(temperatureValue)

    buttonFunc()
    counter += 1
    if (temperatureValue > 0) and (player1guess > 0):
        player1result = temperatureValue - player1guess
    elif (temperatureValue > 0) and (player1guess < 0):
        player1result = temperatureValue + (-1) * player1guess
    elif (temperatureValue < 0) and (player1guess > 0):
        player1result = (-1) * temperatureValue + player1guess
    elif (temperatureValue < 0) and (player1guess < 0):
        player1result = (-1) * temperatureValue - (-1) * player1guess
    if player1result < 0:
        player1result = player1result * (-1)
    print(temperatureValue)
    print(player1result)

def player2():
    global tempUnite, temperatureValue, player1guess, player2guess, player1result, counter, player2result
    while (pin_button.value()) == (0) and counter == (1):
        player2guess = round(round( pot.read_u16() - 0 ) * ( 212 - -20 ) / ( 65535 - 0 ) + -20)
        oled.text("{}".format("Player 2: "), 5, 25)
        oled.text("{}".format(player2guess), 85, 25)
        oled.show()
        oled.fill(0)

    print(temperatureValue)
    buttonFunc()
    counter += 1
    if (temperatureValue > 0) and (player2guess > 0):
        player2result = temperatureValue - player2guess
    elif (temperatureValue > 0) and (player2guess < 0):
        player2result = temperatureValue + (-1) * player2guess
    elif (temperatureValue < 0) and (player2guess > 0):
        player2result = (-1) * temperatureValue + player2guess
    elif (temperatureValue < 0) and (player2guess < 0):
        player2result = (-1) * temperatureValue - (-1) * player2guess
    if player2result < 0:
        player2result = player2result * (-1)

def control():
    global tempUnite, temperatureValue, player1guess, player2guess, player1result, counter, player2result
    oled.fill(0)
    oled.text("{}".format("WINNER"), 40, 0)
    if player1result >= player2result:
        oled.text("{}".format("Player 2"), 40, 20)
        ws2812.pixels_fill((51, 204, 0))
        ws2812.pixels_show()
    elif player1result <= player2result:
        oled.text("{}".format("Player1"), 40, 20)
        ws2812.pixels_fill((51, 204, 0))
        ws2812.pixels_show()
    else:
        oled.fill(0)
        oled.text("{}".format("scoreless "), 40, 20)
    oled.text("{}".format("Temp:"), 10, 40)
    oled.text("{}".format(temperatureValue), 60, 40)
    if tempUnite == (1):
        oled.text("{}".format("Celcius"), 20, 50)
    elif tempUnite == (2):
        oled.text("{}".format("Fahrenheit"), 20, 50)
    oled.show()
    counter += 1

ws2812.pixels_fill((0 ,0 ,0 ))
ws2812.pixels_show()
temperatureValue = 0
player1result = 0
player2result = 0
tempUnite = 0
counter = 3
player2guess = 0
player1guess = 0
while (pin_button.value()) == (0):
    start()
    print(getTemp())

time.sleep((0.3))
buttonFunc()
while (pin_button.value()) == (0):
    oled.fill(0)
    SetTempF()
    print(getTemp())

buttonFunc()
if tempUnite == (1):
    temperatureValue = getTemp()
elif tempUnite == (2):
    temperatureValue = ((9) * (getTemp())) / (5) + (32)
countDown()
print(temperatureValue)
player1()
time.sleep((0.5))
player2()
control()

Arduino C Code


#ifndef ACROBOTIC_SSD1306_H
#define ACROBOTIC_SSD1306_H

#if ARDUINO >= 100
 #include "Arduino.h"
#else
 #include "WProgram.h"
#endif

#ifdef __AVR__
  #include <avr/pgmspace.h>
  #define OLEDFONT(name) static const uint8_t __attribute__ ((progmem)) name[]
#elif defined(ESP8266)
  #include <pgmspace.h>
  #define OLEDFONT(name) static const uint8_t name[]
#else
  #define pgm_read_byte(addr) (*(const unsigned char *)(addr))
  #define OLEDFONT(name) static const uint8_t name[]
#endif

#include "Wire.h"
#include "fonts/font8x8.h"
#include "fonts/font5x7.h"

// Default screen size is 128x64. Using a #define in your sketch before
// the #include statement can change the default size.

#if !defined SSD1306_128_64 && !defined SSD1306_128_32
  #define SSD1306_128_64
#endif

#if defined SSD1306_128_64
  #define SSD1306_Max_X                 127
  #define SSD1306_Max_Y                 63
#endif
#if defined SSD1306_128_32
  #define SSD1306_Max_X                 127
  #define SSD1306_Max_Y                 31
#endif

#define PAGE_MODE                     01
#define HORIZONTAL_MODE               02

#define SSD1306_Address               0x3C
#define SSD1306_Command_Mode          0x80
#define SSD1306_Data_Mode             0x40
#define SSD1306_Display_Off_Cmd       0xAE
#define SSD1306_Display_On_Cmd        0xAF
#define SSD1306_Normal_Display_Cmd    0xA6
#define SSD1306_Inverse_Display_Cmd   0xA7
#define SSD1306_Activate_Scroll_Cmd   0x2F
#define SSD1306_Dectivate_Scroll_Cmd  0x2E
#define SSD1306_Set_Brightness_Cmd    0x81

#define Scroll_Left                   0x00
#define Scroll_Right                  0x01

#define Scroll_2Frames                0x7
#define Scroll_3Frames                0x4
#define Scroll_4Frames                0x5
#define Scroll_5Frames                0x0
#define Scroll_25Frames               0x6
#define Scroll_64Frames               0x1
#define Scroll_128Frames              0x2
#define Scroll_256Frames              0x3

class ACROBOTIC_SSD1306 {
  public:
    char addressingMode;
    void init(TwoWire& wire=Wire);

    void setNormalDisplay();
    void setInverseDisplay();

    void sendCommand(unsigned char command);
    void sendData(unsigned char Data);

    void setPageMode();
    void setHorizontalMode();

    void setTextXY(unsigned char Row, unsigned char Column);
    void clearDisplay();
    void setBrightness(unsigned char Brightness);
    bool putChar(unsigned char c);
    void putString(const char *string);
    void putString(String string);
    unsigned char putNumber(long n);
    unsigned char putFloat(float floatNumber,unsigned char decimal);
    unsigned char putFloat(float floatNumber);
    void drawBitmap(unsigned char *bitmaparray,int bytes);

    void setHorizontalScrollProperties(
        bool direction,
        unsigned char startPage,
        unsigned char endPage,
        unsigned char scrollSpeed);
    void activateScroll();
    void deactivateScroll();

    void displayOn();
    void displayOff();

    void setFont(const uint8_t* font, bool inverse=false);

  private:
    const uint8_t* m_font;      // Current font.
    uint8_t m_font_offset = 2;  // Font bytes for meta data.
    uint8_t m_font_width;       // Font witdth.
    uint8_t m_col;              // Cursor column.
    uint8_t m_row;              // Cursor row (RAM).
    bool m_inverse=false;       // Inverse text.
    TwoWire* m_wire;
};

extern ACROBOTIC_SSD1306 oled;  // ACROBOTIC_SSD1306 object

#endif

Step 2: Choosing Celsius or Fahrenheit?

Turn on the PicoBricks and you'll see the OLED screen light up with two options: Celsius and Fahrenheit. Now, this part is interactive and educational! Depending on which temperature unit you're more familiar with (or just curious about), use the potentiometer to toggle between the two options. Made your choice? Great! Just hit the button to lock it in.

Step 3: Time to Guess!

Now, let's get to the heart of the game. Player One, it's your turn to guess what the ambient temperature is. Twist the potentiometer and watch the OLED screen as you make your guess. Lock it in and pass it over to Player Two, who then makes their guess in the same way.

Step 4: And the Winner Is...

Once both guesses are in, it's the moment of truth. The PicoBricks compares your guesses to the actual temperature it secretly measured earlier. The closest guess wins, and the screen displays a celebratory message for the winner!

 

Prev Post
Next Post

Thanks for subscribing!

This email has been registered!

Shop the look
Choose Options

Edit Option

Have Questions?

Back In Stock Notification

Compare

Product SKURatingDescription Collection Availability Product Type Other Details
this is just a warning
Login
Shopping Cart
0 items