Code: Select all
import machine
from machine import I2C
from ads1x15 import ADS1115
from sh1106 import SH1106_I2C
import utime
# ------------------------------------------------------------------------
# Define GPIO Pins
i2c2 = I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000)
# ------------------------------------------------------------------------
# Define Functions
# Calculate the percentage between the wet/dry values
def percentage_value(value, in_min, out_max, out_min):
percentage_value = (value - in_min) / (out_max - out_min) * 100
return (str(round(percentage_value))) + "%"
# Scroll in screen vertically
def scroll_in_screen_v(screen):
for i in range (0, (oled_height+1), 1):
for line in screen:
oled.text(line[2], line[0], -oled_height+i+line[1])
oled.show()
if i!= oled_height:
oled.fill(0)
# Scroll out screen vertically
def scroll_out_screen_v(speed):
for i in range ((oled_height+1)/speed):
for j in range (oled_width):
oled.pixel(j, i, 0)
oled.scroll(0,speed)
# ------------------------------------------------------------------------
# Define Variables
adc0 = ADS1115(i2c2, 0x48, 1) # Gain = 1
# adc1 = ADS1115(i2c2, 0x49, 1) # Gain = 1
# adc2 = ADS1115(i2c2, 0x4A, 1) # Gain = 1
# adc3 = ADS1115(i2c2, 0x4B, 1) # Gain = 1
wet = 11140 # Reading when sensor is wet
dry = 22000 # Reading when sensor is dry
oled_width = 128 # Width of OLED screen
oled_height = 64 # Height of OLED screen
oled = SH1106_I2C(oled_width, oled_height, i2c2)
# screen1_row1 = "Screen 1, row 1"
screen1_row2 = "Screen 1, row 2"
screen1_row3 = "Screen 1, row 3"
screen1_row4 = "Screen 1, row 4"
# First Analog-to-Digital ADC PGA Converter ADS1115--------------
adc0_result_A0 = percentage_value(adc0.read(0, 0), dry, wet, dry)
# adc0_result_A1 = percentage_value(adc0.read(0, 1), dry, wet, dry)
# adc0_result_A2 = percentage_value(adc0.read(0, 2), dry, wet, dry)
# adc0_result_A3 = percentage_value(adc0.read(0, 3), dry, wet, dry)
# Second Analog-to-Digital ADC PGA Converter ADS1115-------------
# adc1_result_A0 = percentage_value(adc1.read(0, 0), dry, wet, dry)
# adc1_result_A1 = percentage_value(adc1.read(0, 1), dry, wet, dry)
# adc1_result_A2 = percentage_value(adc1.read(0, 2), dry, wet, dry)
# adc1_result_A3 = percentage_value(adc1.read(0, 3), dry, wet, dry)
screen1 = [[0, 0 , "Sensor 1: " + str(adc0_result_A0)], [0, 16, screen1_row2], [0, 32, screen1_row3], [0, 48, screen1_row4]]
# ------------------------------------------------------------------------
while True:
# Scroll in, stop, scroll out (vertical)
scroll_in_screen_v(screen1)
utime.sleep(5)
scroll_out_screen_v(2)