from machine import I2C, Pin, ADC, PWM from time import sleep_us, sleep_ms, ticks_ms, ticks_diff from pico_i2c_lcd import I2cLcd from rp2 import PIO, StateMachine, asm_pio import math, gc, _thread #variables Potu16 = 0 Sens1u16 = 0 Sens2u16 = 0 ErrorBool = False BoolCelsius = True PageIndex = 0 AvgTemp = 0 DeltaTemp = 0 frequency = 50 Page1Line1 = "T1 T2 Set Stir" Page2Line1 = "Temperature in: Degrees Celsius" timeButtonDown = 2000 #display i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000) I2C_ADDR = i2c.scan()[0] lcd = I2cLcd(i2c, I2C_ADDR, 2, 16) lcd.clear() lcd.putstr("Booting...\nPlease wait") #outputs Fet1 = PWM(Pin(0)) Fet1.freq (frequency) Fet1.duty_u16(0) Fet2 = PWM(Pin(1)) Fet2.freq (frequency) Fet2.duty_u16(0) BJT1 = Pin(12, Pin.OUT) BJT1.off() BJT2 = PWM(Pin(13))#perhaps pwm BJT2.duty_u16(0) LEDOrange = Pin(14, Pin.OUT)#perhaps pwm LEDOrange.off() LEDRed = Pin(15, Pin.OUT)#perhaps pwm LEDRed.off() #Digital inputs Button1 = Pin(20, Pin.IN, Pin.PULL_DOWN) Button2 = Pin(21, Pin.IN, Pin.PULL_DOWN) Button3 = Pin(22, Pin.IN, Pin.PULL_DOWN) #Analog inputs ADCPot = ADC(26) ADCSens1 = ADC(27) ADCSens2 = ADC(28) #init machine.freq(150000000) LEDOrange.on() LEDRed.on() sleep_ms(1000) LEDOrange.off() sleep_ms(1000) LEDRed.off() lcd.clear() lcd.putstr("Booting...\nInit I/O done") sleep_ms(1000) lcd.clear() lcd.putstr(Page1Line1) lcd.hide_cursor() def Core2Main(): global Potu16 global Sens1u16 global Sens2u16 global ErrorBool global AvgTemp global DeltaTemp global PageIndex while(True): sleep_ms(333) Potu16 = ADCPot.read_u16() Sens1u16 = ADCSens1.read_u16() Sens2u16 = ADCSens2.read_u16() if Sens1u16 > Sens2u16 + 600 or Sens2u16 > Sens1u16 + 600: ErrorBool = True AvgTemp = 65535 LEDRed.on() if not ErrorBool: AvgTemp = int((Sens1u16 + Sens2u16)/2) LEDRed.off() if PageIndex == 0: lock.acquire() lcd.move_to(0, 1) lcd.putstr(str(int((Sens1u16*0.008392462)-50)) + " ") lcd.move_to(4, 1) lcd.putstr(str(int((Sens2u16*0.008392462)-50)) + " ") lcd.move_to(8, 1) lcd.putstr(str(int((Potu16*0.008392462)-50)) + " ") lock.release() DeltaTemp = Potu16 - AvgTemp if DeltaTemp < 0: Fet1.duty_u16(0) Fet2.duty_u16(0) LEDOrange.off() if DeltaTemp > 0 and Potu16 < 5000: Fet1.duty_u16(DeltaTemp * 2) Fet2.duty_u16(DeltaTemp * 2) LEDOrange.on() if DeltaTemp > 0 and Potu16 > 5000: Fet1.duty_u16(DeltaTemp) Fet2.duty_u16(DeltaTemp) LEDOrange.on() lock = _thread.allocate_lock() _thread.start_new_thread(Core2Main, ()) buttonactiveBool1 = False buttonactiveBool2 = False buttonactiveBool3 = False ActiveTimer = 0 stirringstrengthint = 0 stirringb = 0 okb = 0 ackb = 0 while(True):#stirring and menu handling sleep_ms(200) stirringb = Button1.value() okb = Button2.value() ackb = Button3.value() if PageIndex == 0: if stirringb == 1 and not buttonactiveBool1: BJT1.on() buttonactiveBool1 = True stirringstrengthint = stirringstrengthint + 1 if stirringstrengthint > 5: stirringstrengthint = 0 else: buttonactiveBool1 = False if stirringstrengthint == 0: BJT2.duty_u16(26214) lock.acquire() lcd.move_to(13, 1) lcd.putstr("40%") lock.release() elif stirringstrengthint == 1: BJT2.duty_u16(39321) lock.acquire() lcd.move_to(13, 1) lcd.putstr("60%") lock.release() elif stirringstrengthint == 2: BJT2.duty_u16(52428) lock.acquire() lcd.move_to(13, 1) lcd.putstr("80%") lock.release() elif stirringstrengthint == 3: BJT2.duty_u16(65534) lock.acquire() lcd.move_to(13, 1) lcd.putstr("MAX") lock.release() elif stirringstrengthint == 4: BJT1.off() lock.acquire() lcd.move_to(13, 1) lcd.putstr("OFF") lock.release() if okb == 1 and not buttonactiveBool2: buttonactiveBool2 = True ActiveTimer = ticks_ms() elif okb == 1 and buttonactiveBool2: if ticks_diff(ticks_ms(), ActiveTimer) > 2000: lock.acquire() lcd.clear() PageIndex = 1 lcd.clear() lcd.show_cursor() lcd.move_to(0, 0) lcd.putstr(Page2Line1) lcd.show_cursor() lcd.blink_cursor_on() lock.release() else: buttonactiveBool2 = False if ackb == 1 and not buttonactiveBool3 and ErrorBool: lock.acquire() lcd.clear() ErrorBool == False LEDRed.off() lcd.clear() lcd.move_to(0, 0) lcd.putstr("Acknowledged") lcd.move_to(0, 1) lcd.putstr("Processing..") sleep_ms(1000) lcd.clear() lcd.move_to(0, 0) lcd.putstr(Page1Line1) lock.release() buttonactiveBool3 = True elif ackb == 0 and buttonactiveBool3: buttonactiveBool3 = False elif PageIndex == 1: if ackb == 1 and not buttonactiveBool3: PageIndex = 0 lock.acquire() lcd.clear() lcd.hide_cursor() lcd.move_to(0, 0) lcd.putstr(Page1Line1) lock.release() BJT1.on() buttonactiveBool3 = True elif ackb == 0 and buttonactiveBool3: buttonactiveBool3 = False if stirringb == 1 and not buttonactiveBool1: buttonactiveBool1 = True #switch to degree F elif stirringb == 1 and buttonactiveBool1: buttonactiveBool1 = False