####
#
# Esko Liimatainen
# Student id: 2521953
# Course: Internet of Things IT00CR95-3011
# Date: 27.03.2026
#
####
print("Hello, Pi Pico W!")
help('modules')
#from machine import Pin, I2C, Button, Pin_Led1, Pin_Led2, Pin_Led3
from picozero import LED, Button
#from machine import Pin, I2C, PWM
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
# For Rotary Encoder (KY-040) potentiometer
#from rotary import RotaryIRQ
#from picozero import PWMLED #<- EI TOIMI Wokwi TÄLLÄKÄÄN
#import time
# TÄMÄ AIHEUTTI POTIKAN TOIMIMATTOMUUDEN !!
#from signal import pause
#import time
#time.sleep(0.1) # Wait for USB to become ready
WIDTH =128
HEIGHT= 64
#Create the I2C connection
i2c=I2C(1,scl=Pin(15),sda=Pin(14),freq=200000)
#Initialize the OLED display
oled= SSD1306_I2C(WIDTH,HEIGHT,i2c)
#Update the frame buffer with some text
oled.text("Hello World Esko", 0, 0)
#Draw the frame on the display
oled.show()
led_1 = LED(22)
button_1 = Button(7)
button_1.when_pressed = led_1.pulse
led_2 = LED(21)
button_2 = Button(8)
button_2.when_pressed = led_2.pulse
##button_2.is_pressed = led_2.pulse
led_3 = LED(20)
button_3 = Button(9)
button_3.when_pressed = led_3.pulse
# NOOO, NYT RIITÄÄ ! EIKÄ TOIMI Wokwi TÄLLÄKÄÄN ! -> EN KOKEILE ENÄÄN led_3:N SÄÄTÄMISTÄ POTIKALLA
# 1. Setup PWM LED (GPIO 15)
#led_3 = PWM(Pin(20))
#led_3.freq(1000)
# 2. Setup Encoder Pins (Must use PULL_UP)
#clk = Pin(10, Pin.IN, Pin.PULL_UP)
#dt = Pin(11, Pin.IN, Pin.PULL_UP)
#sw = Pin(12, Pin.IN, Pin.PULL_UP)
# State variables
#val = 32768 # Start at 50% brightness (out of 65535)
#last_clk = clk.value()
#print("Rotate the knob in Wokwi!")
#while True:
# Check rotation
# current_clk = clk.value()
# if current_clk != last_clk and current_clk == 0: # Falling edge
# if dt.value() != current_clk:
# val = min(65535, val + 3000) # Turn Up
# else:
# val = max(0, val - 3000) # Turn Down
# led_3.duty_u16(val)
# print(f"Brightness: {int((val/65535)*100)}%")
# last_clk = current_clk
# Check button press
# if sw.value() == 0:
# print("Button Clicked!")
# val = 0 if val > 0 else 32768 # Toggle Off or to 50%
# led_3.duty_u16(val)
# time.sleep(0.2) # Debounce
# time.sleep(0.001) # Keeps the simulation smooth
# NO EIKÄ TOIMI Wokwi TÄLLÄKÄÄN ! -> EN KOKEILE ENÄÄN led_3:N SÄÄTÄMISTÄ POTIKALLA
# SIIIIIS, MINKÄ MINKÄ OLIN KOPIOINUT TÄHÄÄN
#led_3 = LED(20)
#button_3 = Button(9)
#button_3.when_pressed = led_3.pulse
##button_3.is_pressed = led_3.pulse
# Raspberry Pico: Rotary Encoder (KY-040)
# Setup the LED on GPIO 20 (Must be a PWM-capable pin)
# Setup PWM LED manually (picozero can be finicky in Wokwi with Rotary)
#led_3 = PWM(Pin(20))
#led_3.freq(1000)
# Encoder Pins
#clk = Pin(10, Pin.IN, Pin.PULL_UP)
#dt = Pin(11, Pin.IN, Pin.PULL_UP)
#sw = Pin(12, Pin.IN, Pin.PULL_UP)
# Variables
#counter = 512 # Start at mid-brightness (0-1023)
#last_clk_state = clk.value()
#print("System Ready. Turn the knob!")
#while True:
# 1. Handle Rotation
# current_clk_state = clk.value()
# if current_clk_state != last_clk_state and current_clk_state == 1:
# if dt.value() != current_clk_state:
# counter = min(1023, counter + 50) # Clockwise
# else:
# counter = max(0, counter - 50) # Counter-clockwise
# led_3.duty_u16(counter * 64) # Convert 1024 to 65535
# print(f"Brightness: {int((counter/1023)*100)}%")
# last_clk_state = current_clk_state
# 2. Handle Button (SW)
# if sw.value() == 0:
# print("Button Pressed!")
# led_3.duty_u16(0) # Turn off
# time.sleep(0.3) # Debounce
# time.sleep(0.001) # Ultra-short delay for simulator stability
# EIKÄ TOIMI Wokwi TÄLLÄKÄÄN !
# 1. Setup the LED using picozero
#led_3 = PWMLED(20)
# 2. Setup the Encoder using the rotary library
# Note: pull_up=True is required for KY-040
#encoder = RotaryIRQ(pin_num_clk=10,
# pin_num_dt=11,
# min_val=0,
# max_val=100,
# reverse=False,
# range_mode=RotaryIRQ.RANGE_BOUNDED)
# 3. Setup the Button (SW) using machine.Pin
#sw = Pin(12, Pin.IN, Pin.PULL_UP)
#last_val = 0
#is_on = True
#while True:
# Get the current position (0 to 100)
# current_val = encoder.value()
# If the knob turned, update brightness
# if last_val != current_val:
# led_3.value = current_val / 100 # Convert 0-100 to 0.0-1.0
# last_val = current_val
# print(f"Brightness: {current_val}%")
# Check if button is pressed (logic is 0 when pressed)
# if sw.value() == 0:
# is_on = not is_on
# if is_on:
# led_3.value = last_val / 100
# print("LED On")
# else:
# led_3.off()
# print("LED Off")
# time.sleep(0.3) # Debounce delay
# time.sleep(0.01) # Small delay for stability
## EI TÄLLÄ
# Setup the Encoder (CLK on 10, DT on 11)
# 'max_steps' defines how many turns it takes to get to full brightness
#encoder = RotaryEncoder(10, 11, max_steps=20)
#def change_brightness():
# # .value returns a float between 0.0 and 1.0 based on max_steps
# led_3.value = encoder.value
# print(f"Brightness: {led_3.value * 100:.0f}%")
# Tell the encoder to call our function whenever it rotates
#encoder.when_rotated = change_brightness
# Keep the script running
#pause()
#Button_1.is_pressed = Button.is_active
#Button.is_released = Button.is_inactive
######
#from picozero import RotaryEncoder, PWMLED
#from signal import pause
# Setup the LED on GPIO 15 (Must be a PWM-capable pin)
#led = PWMLED(15)
# Setup the Encoder (CLK on 12, DT on 13)
# 'max_steps' defines how many turns it takes to get to full brightness
#encoder = RotaryEncoder(12, 13, max_steps=20)
#def change_brightness():
# # .value returns a float between 0.0 and 1.0 based on max_steps
# led.value = encoder.value
# print(f"Brightness: {led.value * 100:.0f}%")
# Tell the encoder to call our function whenever it rotates
#encoder.when_rotated = change_brightness
# Keep the script running
#pause()