Please complete the basic installation-chapters before starting a project.
This project will show you how to interact with a screen using a button. The script will follow a sequence when the button is pressed and change what is displayed. It will call different functions depending on the current function that is displayed.
You will need the following to build this project:
Assemble the components according to the circuit diagram below:
Read the comments in the code and change the variables as necessary then upload it to your board.
In order to use the OLED screen you will need to install the following module:
1mip.install("https://raw.githubusercontent.com/micropython/micropython-lib/master/micropython/drivers/display/ssd1306/ssd1306.py")
If you are unsure how to install external modules you can read up on it here
1from machine import SoftI2C, Pin2from Button import Button3import ssd1306_1315 as ssd13064import framebuf5import gc67DISPLAY_WIDTH = 1288DISPLAY_HEIGHT = 329class Point():10 def __init__(self, x, y):11 self.x = x12 self.y = y13 def set_coords(coords):14 return1516i2cbus = SoftI2C(scl = Pin(12), sda = Pin(11), freq = 100000)17print(i2cbus)18oled = ssd1306.SSD1306_I2C(DISPLAY_WIDTH, DISPLAY_HEIGHT, i2cbus)1920counter_pressed = 021total_pressed = 02223def textDisplay():24 oled.show()25 oled.text('Arduino', 40, 0)26 oled.text('and', 60, 12)27 oled.text('MicroPython', 23, 24)28 oled.show()2930def arduinoLogo():31 global total_pressed32 oled.fill(0)33 oled.show()34 total_pressed = total_pressed + counter_pressed35 oled.text('Number of times', 5, 0)36 oled.text(f'button was', 5, 10)37 oled.text(f'pressed: {total_pressed}', 5, 20)38 oled.show()3940def micropythonLogo():41 oled.fill(0)42 oled.fill_rect(0, 0, 32, 32, 1)43 oled.fill_rect(2, 2, 28, 28, 0)44 oled.vline(9, 8, 22, 1)45 oled.vline(16, 2, 22, 1)46 oled.vline(23, 8, 22, 1)47 oled.fill_rect(26, 24, 2, 4, 1)48 oled.show()49 50def button_change(button, event):51 global counter_pressed52 if event == Button.PRESSED:53 counter_pressed += 154 if counter_pressed == 1:55 textDisplay()56 if counter_pressed == 2:57 arduinoLogo()58 if counter_pressed == 3:59 micropythonLogo()60 if counter_pressed > 3:61 oled.fill(0)62 oled.show()63 counter_pressed = 064 65button_one = Button(17, False, button_change)6667while(1):68 button_one.update()