Interrupt for-loop on Raspberry Pi

IT WORKS! :partying_face:

Thank you @stucork for the threading tip! The only thing I was wondering why you used ‘def run(self):’? I had to remove it from my code in order to get it working.

Big thanks to @p.colbert for learning me the basics of the server/client principle and the usefull tips and @david.wylie for correcting my noobie mistakes! :wink: I’ve learned a lot!

Raspberry Pi code:
#Running a NEMA17 stepper motor with a TB6600 driver

import RPi.GPIO as GPIO
import time
import threading
import anvil.server
from anvil.tables import app_tables

GPIO.setmode(GPIO.BOARD) #read the pin as board instead of BCM pin

anvil.server.connect("YOUR KEY")

DIR = 33 # Direction of the stepper motor
PUL = 35 # Pulses send to the stepper motor
ENA = 36 # ON/OFF stepper motor


GPIO.setwarnings(False)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(PUL, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)

steps = 10000

motor_uit_Rasp = False

GPIO.output(ENA, True)


def motor_start (direction_Rasp, speed_Rasp):
    for x in range (steps):
            global motor_uit_Rasp
            GPIO.output(ENA, False) 
            GPIO.output(DIR, direction_Rasp)
            if motor_uit_Rasp == True:
                GPIO.output(ENA, True)
                return
            GPIO.output(PUL, 1)
            time.sleep(speed_Rasp)
            GPIO.output(PUL, 0)
            time.sleep(speed_Rasp)
    GPIO.output(ENA, True)
    return
 
@anvil.server.callable
def motor_aan(direction,speed):
    direction_Rasp = direction
    speed_Rasp = speed
    
    global motor_uit_Rasp
    motor_uit_Rasp = False
    
    motor = threading.Thread(target = motor_start, name = 'thread 1', args = (direction_Rasp, speed_Rasp))
    motor.start()
    
    print('Motor AAN')
    return

@anvil.server.callable
def motor_uit():
    global motor_uit_Rasp
    motor_uit_Rasp = True
    GPIO.output(ENA, True) 
    
    print('Motor UIT')
    return

anvil.server.wait_forever()
3 Likes