LEDs are often used on electronic systems. Each button can have small LEDs next to each option. By making a single LED light up in different colors, it is possible to do the work of more than one LED with a single LED. LEDs working in this type are called RGB LEDs. It takes its name from the initials of the color names Red, Green, Blue. Another advantage of this LED is that it can light up in mixtures of 3 primary colors. Purple, turquoise, orange…
In this project you will learn about the randomness used in every programming language. We will prepare a enjoyable game with the RGB LED, OLED screen and button module of Picobricks.
In this project, we will create a timer alarm that adjusts for daylight using the light sensor in Picobricks.
MicroPython Codes of the PicoBricks
from machine import Pin,I2C,ADC,PWM #to access the hardware on the pico
from picobricks importSSD1306_I2C #OLED Screen Library
import utime
from picobricks importWS2812 #ws8212 library
#OLED Screen Settings
WIDTH=128HEIGHT=64
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)
neo =WS2812(pin_num=6, num_leds=1, brightness=0.3)#initialize digital pin 6as an OUTPUTfor NeoPixel
oled =SSD1306_I2C(128,64, i2c)
ldr =ADC(Pin(27))#initialize digital pin 6as an OUTPUTfor NeoPixel
button =Pin(10,Pin.IN,Pin.PULL_DOWN)#initialize digital pin 10as an INPUTfor button
buzzer =PWM(Pin(20, Pin.OUT))#initialize digital pin 20as an OUTPUTfor buzzer
buzzer.freq(1000)BLACK=(0,0,0)WHITE=(255,255,255)
#RGB black and white color code
oled.fill(0)
oled.show()
neo.pixels_fill(BLACK)
neo.pixels_show()if ldr.read_u16()<4000:
wakeup = True
else:
wakeup = False
while True:while wakeup==False:
oled.fill(0)
oled.show()
oled.text("Good night",25,32)
oled.show()
#Show on OLED and print "Good night"
utime.sleep(1)if ldr.read_u16()40000:
wakeup= False
utime.sleep(1)
#wait for one second
Arduino C Codes of the PicoBricks
from machine import Pin,I2Cfrom picobricks importSSD1306_I2Cimport utime
import urandom
import _thread
from picobricks importWS2812WIDTH=128HEIGHT=64
sda=machine.Pin(4)
scl=machine.Pin(5)
i2c=machine.I2C(0,sda=sda, scl=scl, freq=1000000)
ws =WS2812(pin_num=6, num_leds=1, brightness=0.3)
oled =SSD1306_I2C(WIDTH,HEIGHT, i2c)
button =Pin(10,Pin.IN,Pin.PULL_DOWN)RED=(255,0,0)GREEN=(0,255,0)BLUE=(0,0,255)WHITE=(255,255,255)BLACK=(0,0,0)
oled.fill(0)
oled.show()
ws.pixels_fill(BLACK)
ws.pixels_show()
global button_pressed
score=0
button_pressed = False
def random_rgb():
global ledcolor
ledcolor=int(urandom.uniform(1,4))if ledcolor ==1:
ws.pixels_fill(RED)
ws.pixels_show()
elif ledcolor ==2:
ws.pixels_fill(GREEN)
ws.pixels_show()
elif ledcolor ==3:
ws.pixels_fill(BLUE)
ws.pixels_show()
elif ledcolor ==4:
ws.pixels_fill(WHİTE)
ws.pixels_show()
def random_text():
global oledtext
oledtext=int(urandom.uniform(1,4))if oledtext ==1:
oled.fill(0)
oled.show()
oled.text("RED",45,32)
oled.show()
elif oledtext ==2:
oled.fill(0)
oled.show()
oled.text("GREEN",45,32)
oled.show()
elif oledtext ==3:
oled.fill(0)
oled.show()
oled.text("BLUE",45,32)
oled.show()
elif oledtext ==4:
oled.fill(0)
oled.show()
oled.text("WHITE",45,32)
oled.show()
def button_reader_thread():while True:
global button_pressed
if button_pressed == False:if button.value()==1:
button_pressed = True
global score
global oledtext
global ledcolor
if ledcolor == oledtext:
score +=10else:
score -=10
utime.sleep(0.01)
_thread.start_new_thread(button_reader_thread,())
oled.text("The Game Begins",0,10)
oled.show()
utime.sleep(2)for i inrange(10):for j inrange(10):random_text()random_rgb()
button_pressed=False
utime.sleep(1.5)
oled.fill(0)
oled.show()
ws.pixels_fill(BLACK)
ws.pixels_show()
utime.sleep(1.5)
oled.fill(0)
oled.show()
oled.text("Your total score:",0,20)
oled.text(str(score),30,40)
oled.show()