Skip to content
International Shipping
Wish Lists Cart
0 items

#15 Night And Day Project With PicoBricks

13 Nov 2023
#15 Night And Day Project With PicoBricks

How about playing the Night and Day game you played at school electronically? The game of night and day is a game in which we put our head on the table  when our teacher says night, and raise our heads when our teacher says day. This game will be a game that you will use your attention and reflex. In this project, we will use a 0.96” 128×64 pixel I2C OLED display. Since OLED screens can be used as an artificial light source, you can enlarge the characters on the screen using lenses and mirrors and reflect them on the desired plane. Systems that can reflect road and traffic information on smart glasses and automobile windows can be made using OLED screens.

Light sensors are sensors that can measure the light levels of the environment they are in, also called photodiodes. The electrical conductivity of the sensor exposed to light changes. We can control the light sensor by coding and developing electronic systems that affect the amount of light.

Details and Algorithm

First we will ask the player to press the button to start the game. Then we will make the OLED screen display NIGHT and DAY randomly for 2 seconds each. If the word NIGHT is displayed, the player should cover the LDR sensor; and if the word DAY is displayed, uncover it. The player has 2 secs to act. Each correct response of the player will earn 10 points. In case of wrong response, the game will be over and a different tone will sound from the buzzer. The score will be displayed on the OLED screen. If the player gives a total of 10 correct responses and gets 100 points, the phrase “Congratulations” will be displayed on the OLED screen and the buzzer will play notes in different tones.

Components

1X PicoBricks

Wiring Diagram

You can code and run Picobricks modules without wiring. If you are going to use the modules by separating them from the board, you should make the module connections with grove cables.

MicroBlocks Codes of the PicoBricks

You can access the Microblocks codes of the project by dragging the image to the Microblocks Run tab or click the button.

 

Microblocks Run Tab

MicroPython Codes of the PicoBricks

 

from machine import Pin, I2C, Timer, ADC, PWM
from picobricks import SSD1306_I2C
import utime
import urandom

# Define the libraries
WIDTH = 128
HEIGHT = 64
# OLED Screen Settings
sda = machine.Pin(4)
scl = machine.Pin(5)
# Initialize digital pins 4 and 5 as OUTPUT for OLED Communication
i2c = machine.I2C(0, sda=sda, scl=scl, freq=1000000)
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
buzzer = PWM(Pin(20))
buzzer.freq(440)
ldr = ADC(Pin(27))
button = Pin(10, Pin.IN, Pin.PULL_DOWN)
# Define the input and output pins
oled.text("NIGHT and DAY", 10, 0)
oled.text("", 40, 20)
oled.text("Press the Button", 0, 40)
oled.text("to START!", 40, 55)
oled.show()
# OLED Screen Texts Settings

def changeWord():
    global nightorday
    oled.fill(0)
    oled.show()
    nightorday = round(urandom.uniform(0, 1))
    # When data is '0', OLED texts NIGHT
    if nightorday == 0:
        oled.text("---NIGHT---", 20, 30)
        oled.show()
    else:
        oled.text("---DAY---", 20, 30)
        oled.show()
    # Waits for the button to be pressed to activate

while button.value() == 0:
    print("Press the Button")
    utime.sleep(0.01)

oled.fill(0)
oled.show()
start = 1
global score
score = 0

while start == 1:
    global gamerReaction
    global score
    changeWord()
    startTime = utime.ticks_ms()
    # When LDR's data greater than 2000, gamer reaction '0'
    while utime.ticks_diff(utime.ticks_ms(), startTime) < 2000:
        if ldr.read_u16() > 20000:
            gamerReaction = 0
        # When LDR's data lower than 2000, gamer reaction '1'
        else:
            gamerReaction = 1
        utime.sleep(0.01)
    # Buzzer working
    buzzer.duty_u16(2000)
    utime.sleep(0.05)
    buzzer.duty_u16(0)

    if gamerReaction == nightorday:
        score += 10
    # When score is 10, OLED says 'Game Over'
    else:
        oled.fill(0)
        oled.show()
        oled.text("Game Over", 0, 18, 1)
        oled.text("Your score " + str(score), 0, 35)
        oled.text("Press RESET", 0, 45)
        oled.text("To REPEAT", 0, 55)
        oled.show()
        buzzer.duty_u16(2000)
        utime.sleep(0.05)
        buzzer.duty_u16(0)
        break

    if score == 100:
        # When score is 100, OLED says 'You Won'
        oled.fill(0)
        oled.show()
        oled.text("Congratulations", 10, 10)
        oled.text("Top Score: 100", 5, 35)
        oled.text("Press Reset", 20, 45)
        oled.text("To REPEAT", 25, 55)
        oled.show()
        buzzer.duty_u16(2000)
        utime.sleep(0.1)
        buzzer.duty_u16(0)
        utime.sleep(0.1)
        buzzer.duty_u16(2000)
        utime.sleep(0.1)
        buzzer.duty_u16(0)
        break

Wiring Diagram

You can code and run Picobricks modules without wiring. If you are going to use the modules by separating them from the board, you should make the module connections with grove cables.

MicroBlocks Codes of the PicoBricks

You can access the Microblocks codes of the project by dragging the image to the Microblocks Run tab or click the button.

MicroPython Codes of the PicoBricks

from machine import Pin,I2C, Timer,ADC,PWMfrom picobricks importSSD1306_I2Cimport utime
import urandom
#define the libraries
WIDTH=128HEIGHT=64
#OLED Screen Settings
sda=machine.Pin(4)
scl=machine.Pin(5)
#initialize digital pin 4 and 5as an OUTPUTforOLED Communication
i2c=machine.I2C(0,sda=sda, scl=scl, freq=1000000)
oled =SSD1306_I2C(WIDTH,HEIGHT, i2c)
buzzer =PWM(Pin(20))
buzzer.freq(440)
ldr=ADC(Pin(27))
button=Pin(10,Pin.IN,Pin.PULL_DOWN)
#define the input and output pins
oled.text("NIGHT and DAY",10,0)
oled.text("",40,20)
oled.text("Press the Button",0,40)
oled.text("to START!",40,55)
oled.show()
#OLED Screen Texts Settings
def changeWord():
    global nightorday
    oled.fill(0)
    oled.show()
    nightorday=round(urandom.uniform(0,1))
    #when data is '0',OLED texts NIGHTif nightorday==0:
        oled.text("---NIGHT---",20,30)
        oled.show()else:
        oled.text("---DAY---",20,30)
        oled.show()
    #waits for the button to be pressed to activate

while button.value()==0:print("Press the Button")sleep(0.01)

oled.fill(0)
oled.show()
start=1
global score
score=0while start==1:
    global gamerReaction
    global score
    changeWord()
    startTime=utime.ticks_ms()
    #when LDR's data greater than 2000, gamer reaction '0'
    while utime.ticks_diff(utime.ticks_ms(), startTime)20000:
            gamerReaction=0
        #when LDR's data lower than 2000, gamer reaction '1'
        else:
            gamerReaction=1sleep(0.01)
    #buzzer working
    buzzer.duty_u16(2000)sleep(0.05)
    buzzer.duty_u16(0)if gamerReaction==nightorday:
        score +=10
    #when score is 10,OLED says 'Game Over'else:
        oled.fill(0)
        oled.show()
        oled.text("Game Over",0,18,1)
        oled.text("Your score "+str(score),0,35)
        oled.text("Press RESET",0,45)
        oled.text("To REPEAT",0,55)
        oled.show()
        buzzer.duty_u16(2000)sleep(0.05)
        buzzer.duty_u16(0)break;if score==100:
        #when score is 10,OLED says 'You Won'
        oled.fill(0)
        oled.show()
        oled.text("Congratulation",10,10)
        oled.text("Top Score: 100",5,35)
        oled.text("Press Reset",20,45)
        oled.text("To REPEAT",25,55)
        oled.show()
        buzzer.duty_u16(2000)sleep(0.1)
        buzzer.duty_u16(0)sleep(0.1)
        buzzer.duty_u16(2000)sleep(0.1)
        buzzer.duty_u16(0)break;

Arduino C Codes of the PicoBricks

 


#include
#include "ACROBOTIC_SSD1306.h"//define the library


#define RANDOM_SEED_PIN28
int Gamer_Reaction=0;
int Night_or_Day=0;
int Score=0;
int counter=0;

double currentTime=0;
double lastTime=0;
double getLastTime(){return currentTime=millis()/1000.0-lastTime;}void_delay(float seconds){
  long endTime=millis()+seconds*1000;while(millis()2)){
      Serial.println(analogRead(27));if(analogRead(27)>200){
        Gamer_Reaction=0;}else{
        Gamer_Reaction=1;}}//determine the gamer reaction based on the value of the LDR sensordigitalWrite(20,HIGH);//turn on the buzzerdelay(250);//waitdigitalWrite(20,LOW);//turn off the buzzerif(Night_or_Day==Gamer_Reaction){//if the user's reaction and the Night_or_Day variable are the sameCorrect();}else{Wrong();}_loop();if(Score==100){
      oled.clearDisplay();
      oled.setTextXY(1,1);
      oled.putString("Congratulation");
      oled.setTextXY(3,1);
      oled.putString("Your Score");
      oled.setTextXY(3,13);
      String String_Score=String(Score);
      oled.putString(String_Score);
      oled.setTextXY(5,3);
      oled.putString("Press Reset");
      oled.setTextXY(6,3);
      oled.putString("To Repeat!");//write the "Congratulation, Your Score, press Reset, To Repeat!" and score variable on the x and y coordinates determined on the OLED screenfor(int i=0;i<3;i++){digitalWrite(20,HIGH);delay(500);digitalWrite(20,LOW);delay(500);}//turn the buzzer on and off three times
    counter=1;}}}voidCorrect(){
  Score+=10;
  oled.clearDisplay();
  oled.setTextXY(3,4);
  oled.putString("10 Points");//increase the score by 10 when the gamer answers correctly}voidChange_Word(){

  oled.clearDisplay();
  Night_or_Day=random(0,2);if(Night_or_Day==0){
    oled.setTextXY(3,6);
    oled.putString("NIGHT");}else{
    oled.setTextXY(3,7);
    oled.putString("DAY");}}//write "NIGHT" or "DAY" on random OLED screenvoidWrong(){
  oled.clearDisplay();
  oled.setTextXY(1,3);
  oled.putString("Game Over");
  oled.setTextXY(3,1);
  oled.putString("Your Score");
  oled.setTextXY(1,13);
  String String_Score=String(Score);
  oled.putString(String_Score);
  oled.setTextXY(5,3);
  oled.putString("Pres Reset");
  oled.setTextXY(6,3);
  oled.putString("To Repeat");// write the score variable and the expressions is quotation marks to the coordinates determined on the OLED screen.digitalWrite(20,HIGH);//turn on the buzzerdelay(1000);//waitdigitalWrite(20,LOW);//turn off the buzzer
  counter=1;}


GitHub Project Page

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 SKUDescription Collection Availability Product Type Other Details

Terms & Conditions

What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
this is just a warning
Login
Shopping Cart
0 items