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
#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;}
Tags: