here the code:
the code is not complete, it is still under construction.
the error is on line 500 & 508
from machine import Pin, Timer, I2C, UART
import time
import utime
import machine
from PCF8574 import PCF8574
import array
import gc # garbage collection
test25 = Pin(25, Pin.OUT) # on-board led
scanner_read_write = Pin(28, Pin.OUT) # scanner read write
scanner_read_write.value(0) #read write
test25. value(0) # on-board led
limit_swich_reaches = 20 # is a number that does not exist in this case
uart = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1)) # touch screen
scanner_uart = UART (1, baudrate=9600, tx=Pin(4), rx=Pin(5)) # scanner
#scanner_uart.init (bits=8, parity=None, stop=2)
pcf1 = PCF8574(0x21, sda=26, scl=27) # I2C you have to change in libary: line 97 - i2c_id=0 to i2c_id=1
pcf1.Pin(PCF8574.P0, Pin.IN, Pin.PULL_UP) # I2C
pcf1.Pin(PCF8574.P1, Pin.IN, Pin.PULL_UP)
pcf1.Pin(PCF8574.P2, Pin.IN, Pin.PULL_UP)
pcf1.Pin(PCF8574.P3, Pin.IN, Pin.PULL_UP)
pcf1.Pin(PCF8574.P4, Pin.IN, Pin.PULL_UP)
pcf1.Pin(PCF8574.P5, Pin.IN, Pin.PULL_UP)
pcf1.Pin(PCF8574.P6, Pin.IN, Pin.PULL_UP)
pcf1.Pin(PCF8574.P7, Pin.IN, Pin.PULL_UP)
pcf2 = PCF8574(0x22, sda=26, scl=27) # I2C you have to change in libary: line 97 - i2c_id=0 to i2c_id=1
pcf2.Pin(PCF8574.P0, Pin.IN, Pin.PULL_UP) # I2C
pcf2.Pin(PCF8574.P1, Pin.IN, Pin.PULL_UP)
pcf2.Pin(PCF8574.P2, Pin.IN, Pin.PULL_UP)
pcf2.Pin(PCF8574.P3, Pin.IN, Pin.PULL_UP)
pcf2.Pin(PCF8574.P4, Pin.IN, Pin.PULL_UP)
pcf2.Pin(PCF8574.P5, Pin.IN, Pin.PULL_UP)
pcf2.Pin(PCF8574.P6, Pin.IN, Pin.PULL_UP)
pcf2.Pin(PCF8574.P7, Pin.IN, Pin.PULL_UP)
pcf3 = PCF8574(0x24, sda=26, scl=27) # I2C you have to change in libary: line 97 - i2c_id=0 to i2c_id=1
pcf3.Pin(PCF8574.P0, Pin.OUT) # I2C
pcf3.Pin(PCF8574.P1, Pin.OUT)
pcf3.Pin(PCF8574.P2, Pin.OUT)
pcf3.Pin(PCF8574.P3, Pin.OUT)
pcf3.Pin(PCF8574.P4, Pin.OUT)
pcf3.Pin(PCF8574.P5, Pin.OUT)
pcf3.Pin(PCF8574.P6, Pin.OUT)
pcf3.Pin(PCF8574.P7, Pin.OUT)
pcf3.digital_write(PCF8574.P0, 1) # to make sure al relays are off
pcf3.digital_write(PCF8574.P1, 1)
pcf3.digital_write(PCF8574.P2, 1)
pcf3.digital_write(PCF8574.P3, 1)
pcf3.digital_write(PCF8574.P4, 1)
pcf3.digital_write(PCF8574.P5, 1)
pcf3.digital_write(PCF8574.P6, 1)
pcf3.digital_write(PCF8574.P7, 1)
LXpulse = Pin(6, Pin.OUT)
LXdir = Pin(7, Pin.OUT)
LYpulse = Pin(8, Pin.OUT)
LYdir = Pin(9, Pin.OUT)
LZpulse = Pin(10, Pin.OUT)
LZdir = Pin(11, Pin.OUT)
LWpulse = Pin(12, Pin.OUT)
LWdir = Pin(13, Pin.OUT)
RXpulse = Pin(14, Pin.OUT)
RXdir = Pin(15, Pin.OUT)
RYpulse = Pin(16, Pin.OUT)
RYdir = Pin(17, Pin.OUT)
RZpulse = Pin(18, Pin.OUT)
RZdir = Pin(19, Pin.OUT)
RWpulse = Pin(20, Pin.OUT)
RWdir = Pin(21, Pin.OUT)
X_front = 0
X_back = 0
Y_top = 0
Y_down = 0
#*****************************************************************************************************
def pulse (stepper_driver):
if stepper_driver == 0:
LXpulse.value(1)
time.sleep_us (25)
LXpulse.value(0)
if stepper_driver == 1:
LXpulse.value(1)
time.sleep_us (25)
LXpulse.value(0)
if stepper_driver == 2:
LYpulse.value(1)
time.sleep_us (25)
LYpulse.value(0)
if stepper_driver == 3:
LYpulse.value(1)
time.sleep_us (25)
LYpulse.value(0)
if stepper_driver == 4:
LZpulse.value(1)
time.sleep_us (25)
LZpulse.value(0)
if stepper_driver == 5:
LZpulse.value(1)
time.sleep_us (25)
LZpulse.value(0)
if stepper_driver == 6:
LWpulse.value(1)
time.sleep_us (25)
LWpulse.value(0)
if stepper_driver == 7:
LWpulse.value(1)
time.sleep_us (25)
LWpulse.value(0)
if stepper_driver == 8:
RXpulse.value(1)
time.sleep_us (25)
RXpulse.value(0)
if stepper_driver == 9:
RXpulse.value(1)
time.sleep_us (25)
RXpulse.value(0)
if stepper_driver == 10:
RYpulse.value(1)
time.sleep_us (25)
RYpulse.value(0)
if stepper_driver == 11:
RYpulse.value(1)
time.sleep_us (25)
RYpulse.value(0)
if stepper_driver == 12:
RZpulse.value(1)
time.sleep_us (25)
RZpulse.value(0)
if stepper_driver == 13:
RZpulse.value(1)
time.sleep_us (25)
RZpulse.value(0)
if stepper_driver == 14:
RWpulse.value(1)
time.sleep_us (25)
RWpulse.value(0)
if stepper_driver == 15:
RWpulse.value(1)
time.sleep_us (25)
RWpulse.value(0)
#*****ALLOCATE DIR PINS******************************************************************************
def dir_pin (move):
if move == 0: LXdir.value(0) # left X front
if move == 1: LXdir.value(1) # left X back
if move == 2: LYdir.value(0) # left Y top
if move == 3: LYdir.value(1) # left Y down
if move == 4: LZdir.value(0) # left Z in
if move == 5: LZdir.value(1) # left Z out
if move == 6: LWdir.value(0) # left W for
if move == 7: LWdir.value(1) # left W back
if move == 8: RXdir.value(0) # right X front
if move == 9: RXdir.value(1) # right X back
if move == 10: RYdir.value(0) # right Y top
if move == 11: RYdir.value(1) # right Y down
if move == 12: RZdir.value(1) # right Z in
if move == 13: RZdir.value(0) # right Z out
if move == 14: RWdir.value(0) # right W for
if move == 15: RWdir.value(1) # right W back
#****************************************************************************************************
#*****PCF8574 EXTENDED I/O INTERRUPT*****************************************************************
#****************************************************************************************************
PCF8574_interrupt = 0
pin = Pin(2, Pin.IN, Pin.PULL_DOWN) # pull-up nog controleren
def callback(pin):
global PCF8574_interrupt
PCF8574_interrupt = 1
pin.irq(trigger=Pin.IRQ_FALLING, handler=callback) # nextion interrupt
#*****RESET PCF8574_INTERRUPT*************************************************************************
def reset_PCF8574_interrupt():
global PCF8574_interrupt
PCF8574_interrupt = 0
#*****************************************************************************************************
#*****I2C*********************************************************************************************
#*****************************************************************************************************
def get_I2C_left (): # check or there are limitswitch reached
byte_input = pcf1.digital_read_all_byte() # I2C
return byte_input
def get_I2C_right (): # check or there are limitswitch reached
byte_input = pcf2.digital_read_all_byte() # I2C
return byte_input
#*****************************************************************************************************
#*****SEE ORE A TOUCHSCREEN BUTTONE IS RELEASED*******************************************************
#*****************************************************************************************************
nextion_interrupt = 0
pin = Pin(3, Pin.IN, Pin.PULL_DOWN) # pull-up nog controleren
def callback(pin):
global nextion_interrupt
nextion_interrupt = 1
pin.irq(trigger=Pin.IRQ_FALLING, handler=callback) # nextion interrupt
#*****RESET NEXTION_INTERRUPT************************************************************************
def reset_nextion_interrupt():
global nextion_interrupt
nextion_interrupt = 0
#*****GET delay_between_steps*********************************************************
def get_dbs(speed): # dbs = delay_between_steps
if speed == 10: delay_between_steps = 1800 # delay_between_steps =
if speed == 20: delay_between_steps = 1600 # the speed of the movement
if speed == 30: delay_between_steps = 1400 # how bigger the value
if speed == 40: delay_between_steps = 1200 # how slower the movement
if speed == 50: delay_between_steps = 1000
if speed == 60: delay_between_steps = 800
if speed == 70: delay_between_steps = 600
if speed == 80: delay_between_steps = 400
if speed == 90: delay_between_steps = 200
return delay_between_steps
#*****GET length_of_acc_decc_delay*********************************************************
def get_loadd(speed): # loadd = length of acc decc delay
if speed == 10: length_of_acc_decc_delay = 600
if speed == 20: length_of_acc_decc_delay = 750
if speed == 30: length_of_acc_decc_delay = 900
if speed == 40: length_of_acc_decc_delay = 1050
if speed == 50: length_of_acc_decc_delay = 1200
if speed == 60: length_of_acc_decc_delay = 1350
if speed == 70: length_of_acc_decc_delay = 1500
if speed == 80: length_of_acc_decc_delay = 1650
if speed == 90: length_of_acc_decc_delay = 2100
return length_of_acc_decc_delay
#*****GET length_of_acc_decc_pulses******************************************************
def get_loadp(speed): # load = length of acc decc pulses
if speed == 10: length_of_acc_decc_pulses = 200
if speed == 20: length_of_acc_decc_pulses = 250
if speed == 30: length_of_acc_decc_pulses = 300
if speed == 40: length_of_acc_decc_pulses = 350
if speed == 50: length_of_acc_decc_pulses = 400
if speed == 60: length_of_acc_decc_pulses = 450
if speed == 70: length_of_acc_decc_pulses = 500
if speed == 80: length_of_acc_decc_pulses = 550
if speed == 90: length_of_acc_decc_pulses = 700
return length_of_acc_decc_pulses
def AFD(hom, man, move, pos): # AFD = acceleration - fixed speed - decceleration
dir_pin(move) # allocate directions pins
reset_nextion_interrupt() # reset "nextion_interrupt" flag
# ACCELERATION
if move == 0: # LXF
stepper_pulse = 0
X_or_Y_axle = 1
Z_axle = 0
W_axle = 0
speed = 80
steps = 40000
if move == 1: # LXB
stepper_pulse = 1
X_or_Y_axle = 1
Z_axle = 0
W_axle = 0
speed = 80
steps = 40000
if move == 2: # LYT
pcf3.digital_write(PCF8574.P1, 0) # RY brake = not braking
stepper_pulse = 2
X_or_Y_axle = 1
Z_axle = 0
W_axle = 0
speed = 80
steps = 40000
if move == 3: # LYD
pcf3.digital_write(PCF8574.P1, 0) # RY brake = not braking
stepper_pulse = 3
X_or_Y_axle = 1
Z_axle = 0
W_axle = 0
speed = 90
steps = 40000
if move == 4: # LZI
stepper_pulse = 4
X_or_Y_axle = 0
Z_axle = 1
W_axle = 0
speed = 80
steps = 40000
if move == 5: # LZO
stepper_pulse = 5
X_or_Y_axle = 0
Z_axle = 1
W_axle = 0
speed = 80
steps = 40000
if move == 6: # LWF
stepper_pulse = 6
X_or_Y_axle = 0
Z_axle = 0
W_axle = 1
speed = 80
steps = 4000
if move == 7: # LWB
stepper_pulse = 7
X_or_Y_axle = 0
Z_axle = 0
W_axle = 1
speed = 80
steps = 40000
if move == 8: # RXF
stepper_pulse = 8
X_or_Y_axle = 1
Z_axle = 0
W_axle = 0
speed = 80
steps = 40000
move_dir = 1
if move == 9:# RXB
stepper_pulse = 9
X_or_Y_axle = 1
Z_axle = 0
W_axle = 0
speed = 80
steps = 40000
move_dir = 0
if move == 10: # RYT
pcf3.digital_write(PCF8574.P1, 0) # RY brake = not braking
stepper_pulse = 10
X_or_Y_axle = 1
Z_axle = 0
W_axle = 0
speed = 80
steps = 40000
move_dir = 1
if move == 11: # RYD
pcf3.digital_write(PCF8574.P1, 0) # RY brake = not braking
stepper_pulse = 11
X_or_Y_axle = 1
Z_axle = 0
W_axle = 0
speed = 80
steps = 40000
move_dir = 0
if move == 12: # RZI
stepper_pulse = 12
X_or_Y_axle = 0
Z_axle = 1
W_axle = 0
speed = 80
steps = 40000
move_dir = 1
if move == 13: # RZO
stepper_pulse = 13
X_or_Y_axle = 0
Z_axle = 1
W_axle = 0
speed = 80
steps = 40000
move_dir = 0
if move == 14: # RWF
stepper_pulse = 14
X_or_Y_axle = 0
Z_axle = 0
W_axle = 1
speed = 80
steps = 40000
move_dir = 1
if move == 15: # RWB
stepper_pulse = 15
X_or_Y_axle = 0
Z_axle = 0
W_axle = 1
speed = 80
steps = 40000
move_dir = 0
X_front = 0
X_back = 0
# acceleration
if X_or_Y_axle == 1:
delay_between_steps = get_dbs(speed)
length_of_acc_decc_delay = get_loadd(speed)
length_of_acc_decc_pulses = get_loadp(speed)
step_size = length_of_acc_decc_delay // length_of_acc_decc_pulses
startpoint_off_acc = delay_between_steps + length_of_acc_decc_delay
step_size = length_of_acc_decc_delay // length_of_acc_decc_pulses
for i in range (length_of_acc_decc_pulses):
pulse (stepper_pulse)
if move_dir == 1:
pos += 1
else:
pos -= 1
utime.sleep_us(startpoint_off_acc)
startpoint_off_acc -= step_size
if X_or_Y_axle == 1:
steps_fixed_speed = steps - (2 * length_of_acc_decc_pulses)
if Z_axle == 1:
steps_fixed_speed = 150000
if W_axle == 1:
steps_fixed_speed = 1000
get_I2C_left() # kind of reset
get_I2C_right() # kind of reset
# FIXED SPEED*
for x in range (steps_fixed_speed):
if nextion_interrupt == 1:
break
if PCF8574_interrupt == 1:
# if stepper_pulse == 1 and get_I2C_left() == 0b11111101: # if button LXB is pushed and limitswitch LXB is reached
# limit_swich_LXB_reaches = 1
# break
# if stepper_pulse == 3 and get_I2C_left() == 0b11110111: # if button LYB is pushed and limitswitch LYB is reached
# limit_swich_LYB_reaches = 1
# break
if stepper_pulse == 9 and get_I2C_right() == 0b11111101: # if button RXB is pushed and limitswitch RXB is reached
break
if stepper_pulse == 11 and get_I2C_right() == 0b11110101: # if button RYB is pushed and limitswitch RYB is reached
break
if stepper_pulse == 13 and get_I2C_right() == 0b11100101: # if button RZO is pushed and limitswitch RZO is reached
break
if stepper_pulse == 15 and get_I2C_right() == 0b11000101: # if button RWB is pushed and homeswitch RW is reached
break
reset_PCF8574_interrupt()
if X_or_Y_axle == 1:
delay_between_steps = get_dbs(speed)
time.sleep_us (delay_between_steps) # fixed speed
if man == 1:
if move == 8:
X_back = 0
if X_front == 0:
if pos >= 15000:
X_front = 1
break
if move == 9:
X_front = 0
if X_back == 0:
if pos <= 8000:
X_back = 1
break
# if move == 10 or move == 11: # Y-axle
# if move_dir == 1 and Y_top != 1:
# if pos >= 17000:
# Y_top = 1
# Y_down = 0
# break
# if move_dir == 0 and Y_down != 1:
# if pos <= 1400:
# Y_top = 0
# Y_down = 1
# break
if Z_axle == 1:
time.sleep_us (40) # fixed speed
if W_axle == 1:
time.sleep_us (200) # fixed speed
pulse (stepper_pulse)
if move_dir == 1:
pos += 1
else:
pos -= 1
# DECCELERATION
if X_or_Y_axle == 1:
delay_between_steps = get_dbs(speed)
length_of_acc_decc_pulses = get_loadp(speed)
step_size = length_of_acc_decc_delay // length_of_acc_decc_pulses
for i in range (length_of_acc_decc_pulses):
pulse (stepper_pulse)
if move_dir == 1:
pos += 1
else:
pos -= 1
utime.sleep_us(delay_between_steps)
delay_between_steps += step_size
pcf3.digital_write(PCF8574.P0, 1) # LY brake = braking
pcf3.digital_write(PCF8574.P1, 1) # RY brake = braking
X_or_Y_axle = 0
Z_axle = 0
W_axle = 0
return pos
#****************************************************************************************************
#****************************************************************************************************
#***** MAIN *****************************************************************************************
#****************************************************************************************************
#****************************************************************************************************
homing_once = 0
while True:
if uart.any(): # see or anything is comming from the touchscreen
data = uart.read()
print (data) # tester
#********************************************************************************************
#***** HOMING *******************************************************************************
#********************************************************************************************
if data == b'$0100P&' and homing_once == 0: # homing button, make the home cycle start once
for y in range (8):
# if y == 0: AFD (homing = 1, move = 20) # LX AFD = acceleration - fixed speed - decceleration
# if y == 1: AFD (homing = 3, move = 20) # LY
# if y == 2: AFD (homing = 5, move = 20) # LZ
# if y == 3: AFD (homing = 7, move = 20) # LW
if y == 4: AFD (hom = 1, man = 0, move = 9, pos = 0) # RX
time.sleep_ms (100)
if y == 5: AFD (hom = 1, man = 0, move = 11, pos = 0) # RY
time.sleep_ms (100)
if y == 6: AFD (hom = 1, man = 0, move = 13, pos = 0) # RZ
time.sleep_ms (100)
if y == 7:
AFD (hom = 1, man = 0, move = 15, pos = 0) # RW
still_in_stepper = 0
break
LX_pos = 0
LY_pos = 0
LZ_pos = 0
LW_pos = 0
RX_pos = 0
RY_pos = 0
RZ_pos = 0
RW_pos = 0
homing_once = 1
#********************************************************************************************
#***** MANUAL *******************************************************************************
#********************************************************************************************
if data== b'$0101P&': # manual button
x = 1
pos = 0
pos_RX = 0
pos_RY = 0
pos_RZ = 0
pos_RW = 0
while x == 1:
if uart.any():
data = uart.read()
print(data)
if data== b'$0216P&': # jump out of while loop when "return"is pressed
x = 0
if data== b'$0200P&': AFD (move = 0)
if data== b'$0201P&': AFD (move = 1)
if data== b'$0202P&': AFD (move = 2)
if data== b'$0203P&': AFD (move = 3)
if data== b'$0204P&': AFD (move = 4)
if data== b'$0205P&': AFD (move = 5)
if data== b'$0206P&': AFD (move = 6)
if data== b'$0207P&': AFD (move = 7)
if data== b'$0208P&': pos_RX = AFD (hom = 0, man = 1, move = 8, pos = pos_RX)
if data== b'$0209P&': pos_RX = AFD (hom = 0, man = 1, move = 9, pos = pos_RX)
if data== b'$0210P&': pos_RY = AFD (hom = 0, man = 1, move = 10, pos = pos_RY)
if data== b'$0211P&': pos_RY = AFD (hom = 0, man = 1, move = 11, pos = pos_RY)
if data== b'$0212P&': pos_RZ = AFD (hom = 0, man = 1, move = 12, pos = pos_RZ)
if data== b'$0213P&': pos_RZ = AFD (hom = 0, man = 1, move = 13, pos = pos_RZ)
if data== b'$0214P&': pos_RW = AFD (hom = 0, man = 1, move = 14, pos = pos_RW)
if data== b'$0215P&': pos_RW = AFD (hom = 0, man = 1, move = 15, pos = pos_RW)
print ("pos RX", pos_RX)
print ("pos RY", pos_RY)
print ("pos RZ", pos_RZ)
print ("pos RW", pos_RW)
homing_once = 0 # to allow homing movement
#********************************************************************************************
#***** SCANNING *****************************************************************************
#********************************************************************************************
if data== b'$0102P&': # scanning button
print("A") # tester
scanner_read_write.value(1) # 1 = write and 0 = read
command = b'\x03'
scanner_uart.write(command)
time.sleep_ms (2)
command = b'\x0F'
scanner_uart.write(command)
time.sleep_ms (2)
command = b'\xFF'
scanner_uart.write(command)