Tinkerdoodle DIY
Published © MIT

Help you sleep with an ESP8266 projector

Have you ever stared at the ceiling and found it hard to fall asleep? Try this ESP8266 powered projector!

BeginnerFull instructions provided1 hour748
Help you sleep with an ESP8266 projector

Things used in this project

Hardware components

ESP8266
×1
SSD1306 128x64 OLED
×1
Convex Lens 25mm Diameter 45mm Focal Length
×1

Software apps and online services

Tinkerdoodle online IDE

Story

Read more

Custom parts and enclosures

3D Printed Enclosure

Schematics

Schematic Diagram

Code

Conway's Game of Life

Python
# Code is also available at https://tinkerdoodle.cc/user/_/notebooks/Shared/Tinkerdoodle/ESP8266%20Projector.ipynb
# Strongly suggest to edit and deploy the code using Tinkerdoodle.
from ssd1306 import SSD1306_I2C
from machine import Pin, I2C
from time import sleep
from uos import urandom

width = 128
height = 64
# Pin D0 = GPIO16, Pin D7 = GPIO13
i2c = I2C(-1, scl=Pin(16), sda=Pin(13))
lcd = SSD1306_I2C(128, 64, i2c)

def rand(n):
    return int.from_bytes(urandom(2), 'big') % n

def draw_grid(arr):
    for i in range(len(arr[0])):
        lcd.line(0, 16*i, 127, 16*i, 0)
    lcd.line(0, 63, 127, 63, 0)
    for i in range(len(arr)):
        lcd.line(16*i, 0, 16*i, 63, 0)
    lcd.line(127, 0, 127, 63, 0)

def draw_lives(arr):
    for i in range(len(arr)):
        for j in range(len(arr[i])):
            if arr[i][j] == 1:
                lcd.fill_rect(16*i, 16*j, 16, 16, 1)
            else:
                lcd.rect(16*i, 16*j, 16, 16, 1)

def create_lives(w, h):
    arr = [[0] * h for _ in range(w)]
    for i in range(len(arr)):
        for j in range(len(arr[i])):
            arr[i][j] = 1 if rand(10) > 6 else 0
    return arr

def update_lives(arr):
    w = len(arr)
    h = len(arr[0])
    arr2 = [[0] * h for _ in range(w)]
    for i in range(w):
        for j in range(h):
            l = arr[(w+i-1)%w][j]
            r = arr[(i+1)%w][j]
            u = arr[i][(h+j-1)%h]
            d = arr[i][(j+1)%h]
            lu = arr[(w+i-1)%w][(h+j-1)%h]
            ld = arr[(w+i-1)%w][(j+1)%h]
            ru = arr[(i+1)%w][(h+j-1)%h]
            rd = arr[(i+1)%w][(j+1)%h]
            n = l + r + u + d + lu + ld + ru + rd
            if arr[i][j] == 1:
                arr2[i][j] = 1 if n == 2 or n == 3 else 0
            else:
                arr2[i][j] = 1 if n == 3 else 0
    return arr2

arr = create_lives(8, 4)
count = 0
while True:
    lcd.fill(0)
    draw_lives(arr)
    draw_grid(arr)
    lcd.show()
    arr = update_lives(arr)
    sleep(1)
    count += 1
    if sum([sum(x) for x in arr]) == 0 or count % 60 == 0:
        arr = create_lives(8, 4)

Credits

Tinkerdoodle DIY

Tinkerdoodle DIY

3 projects • 8 followers

Comments