0

I'm having trouble either controlling the flow, or maybe my math is wrong, but I keep getting stuck in a loop. My code should be getting the temperature of a thermistor (TempF), translate that to a position on a stepper motor of 0 - 450 steps, (wanted_pos = TempF * 450 / 100) save that variable as wanted_pos. Then it should compare wanted_pos with last_pos and if the difference is positive, or negative, set the (direction pin) dir_pin on, or off, respectively. (The problem I'm having is in this part, where it's not comparing correctly, or the pin is being set incorrectly.) After comparing last_pos to wanted_pos the code should calculate the number of steps to move, and move in 'x' direction 'x' number of steps. I have built in an if statement to run at power on to checks if my variables have been assigned, and if not, then it should move the motor to wanted_pos, set some variables, and then restart in my actual loop.

My startup-if-statement gets wanted_pos, moves the motor from 0 to wanted_pos, defines last_pos as 0, defines current pos as wanted pos, and then restarts, calling all functions this time in order. The first couple times it runs fine it seems, and sometimes it seems to run fine as long as there's not a major temperature change. It seems to be getting stuck on one of the if statements that compare the current_pos to wanted_pos and it keeps saying that wanted_pos > current_pos setting the dir_pin to move up even if that's incorrect.

End observation is that if I run my code, then it moves the gauge up to around 70F, if I leave it alone it seems to be fine for a while, but then gets stuck only moving up. If I restart the program, I can trigger the effect every time by making a fast temperature change.

I have tried everything I can think of, my code is probably a mess, I have been tinkering with this for 3 days now and I still do not have a temperature gauge...

from machine import ADC
from machine import Pin
import time
import math
    
off = 0
on = 1
dir_pin = Pin(20, Pin.OUT)  #set direction of stepper motor 1/0
step_pin = Pin(21, Pin.OUT)  #step motor pin
adcpin = 26  #Thermistor data pin
thermistor = ADC(adcpin)
Vin = 3.3  #SysBus
Ro = 10000  #10k Resistor for voltage divider
delay = 0.006  #define step delay
#Steinheart Constants
A = 0.001129148
B = 0.000234125
C = 0.0000000876741

def get_current_pos(steps_to_move, last_dir, last_pos):
    print("get current position called")
    if last_dir == off:
        current_pos = last_pos + steps_to_move
        print(current_pos)
    elif last_dir == on:
        current_pos = last_pos - steps_to_move
        print(current_pos)
    else:
        current_pos = last_pos


def get_temp_pos():
    print("get temp position")
    adc = thermistor.read_u16()
    Vout = (3.3/65535)*adc
    #calculate resistance in thermistor
    try:
        
        Rt = (Vout*Ro) / (Vin - Vout)
        #Steinheart equasion
        TempK = 1 / (A + (B * math.log(Rt)) + C * math.pow(math.log(Rt), 3))

        #convert to C and F
        TempC = TempK - 253.15
        global TempF
        TempF = TempC * 1.8 + 32
        if TempF > 100:
            global TempF
            TempF = 100
        elif TempF < 0:
            global TempF
            TempF = 1
        else:
            print("happy range")
            print(TempF)
    except ZeroDivisionError:
        global wanted_pos
        wanted_pos = 0
        
def get_wanted_pos():
    global wanted_pos
    wanted_pos = TempF * 450 / 100
    print("wanted pos")
    print(wanted_pos)
    global current_pos



def get_steps_to_move(wanted_pos, current_pos):
    print("get steps to move")
    if wanted_pos > current_pos:
        global steps_to_move
        steps_to_move = wanted_pos - current_pos
        print(steps_to_move)
    elif wanted_pos < current_pos:
        global steps_to_move
        steps_to_move = current_pos - wanted_pos
        print(steps_to_move)
        print("2")
    elif wanted_pos == current_pos:
        global steps_to_move
        steps_to_move = 0
        print(steps_to_move)
    else:
        pass
        
def move_motor(steps_to_move):
    global last_pos
    last_pos = current_pos
    print("motor call")
    print(TempF)
    for _ in range(steps_to_move): 
        step_pin.on()
        time.sleep(delay)
        step_pin.off()
        time.sleep(delay)
    time.sleep(2)

def define_zero():
    print("define 0")
    get_temp_pos()
    get_wanted_pos()
    global steps_to_move
    steps_to_move = wanted_pos
    global current_pos
    current_pos = wanted_pos
    global last_pos
    last_pos = current_pos
    global last_dir
    last_dir = off
    dir_pin.off()
    move_motor(steps_to_move)
    
    
while True:
    if 'last_dir' in globals():
        get_current_pos(steps_to_move, last_dir, last_pos)
        get_temp_pos()
        get_wanted_pos()
        if wanted_pos > current_pos:
            dir_pin.off()
            last_dir = off
            print("OFF")
        elif wanted_pos < current_pos:
            last_dir = on
            dir_pin.on()
            print("ON")
        else:
            ("same_error")
        get_steps_to_move(wanted_pos, current_pos)
        move_motor(steps_to_move)
    else:
        define_zero()
2
  • What hardware are you using? You're expecting 6ms resolution in time.sleep, and you usually get that much accuracy. Commented Apr 6 at 1:08
  • 3
    You don't seem to understand the difference between local and global variables, and you definitely need to learn how to return values from functions. Your get_current_pos(), for example, is completely useless - it only sets a local variable (that vanishes at the end of the function), and has no side-effects, so calling it has no effect. Commented Apr 6 at 1:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.