I want to use a keypad to control a Robot Arm. I found code for how to program a keypad. I am able to read what key is pressed. However, when I use the input function, it apparently doesn't read the key pressed on the keypad. I have done a search and I haven't seen anyone trying to use the keypad like this. The code is as follows. Thanks for any assistance!
Code:
from machine import Pin, PWM, ADCimport utime,timefrom keypad import Keypadfrom time import sleep# Define GPIO pins for rowsrow_pins = [Pin(16),Pin(17),Pin(18),Pin(19)]# Define GPIO pins for columnscolumn_pins = [Pin(20),Pin(21),Pin(22),Pin(26)]# Define keypad layoutkeys = [ ['1', '2', '3', 'A'], ['4', '5', '6', 'B'], ['7', '8', '9', 'C'], ['*', '0', '#', 'D']]keypad = Keypad(row_pins, column_pins, keys)key_pressed = keypad.read_keypad()Button = Pin(13, Pin.IN, Pin.PULL_UP)button_cw = Pin(14, Pin.IN, Pin.PULL_DOWN)button_acw = Pin(15, Pin.IN, Pin.PULL_DOWN)# Servo for grippers1 = PWM(Pin(16))s1.freq(50)# Stepper Motor 1 output pinss1pin_a = Pin(0, Pin.OUT)s1pin_b = Pin(1, Pin.OUT)s1pin_c = Pin(2, Pin.OUT)s1pin_d = Pin(3, Pin.OUT)#set Stepper Motor 1 pins lows1pin_a.low()s1pin_b.low()s1pin_c.low()s1pin_d.low()# Stepper Motor 2 output pinss2pin_a = Pin(4, Pin.OUT)s2pin_b = Pin(5, Pin.OUT)s2pin_c = Pin(6, Pin.OUT)s2pin_d = Pin(7, Pin.OUT)#set Stepper Motor 2 pins lows2pin_a.low()s2pin_b.low()s2pin_c.low()s2pin_d.low()# Stepper Motor 3 output pinss3pin_a = Pin(8, Pin.OUT)s3pin_b = Pin(9, Pin.OUT)s3pin_c = Pin(10, Pin.OUT)s3pin_d = Pin(11, Pin.OUT)#set Stepper Motor 3 pins lows3pin_a.low()s3pin_b.low()s3pin_c.low()s3pin_d.low()# rotate clockwise dir adding step to listrotate_steps_cw = [[1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]]rotate_steps_acw = list(reversed(rotate_steps_cw))potentiometer = machine.ADC(26)timing_min = 0.002timing_max = 0.1timerange = timing_max - timing_minconversion_factor = timerange / 65535# define direction of rotation on startrotation = "acw"print("Servo 1 = base, Servo 2 = shoulder joint, Servo 3 = elbow joint, Servo 4 = gripper")print(" ")while True: key_pressed = keypad.read_keypad() select_servo = input("Enter Servo : ") select_rotation = input("Enter Motor Rotation : ") #global rotation print(rotation, button_cw.value(), button_acw.value()) if select_servo == "1": while True: #print(rotation, button_cw.value(), button_acw.value()) #read cw button if select_rotation == "c": rotation = "cw" print("CW") #read acw button if select_rotation == "a": rotation = "acw" print("ACW") adc_read = (potentiometer.read_u16() * conversion_factor) + timing_min rotating_speed = round(adc_read, 3) print(rotation) # rotate clockwise if ON pressed if rotation == "cw": for step in rotate_steps_cw: s1pin_a.value(step[0]) s1pin_b.value(step[1]) s1pin_c.value(step[2]) s1pin_d.value(step[3]) utime.sleep(rotating_speed) print(rotating_speed, rotation) # rotate counter clockwise if ON pressed if rotation == "acw": for step in rotate_steps_acw: s1pin_a.value(step[0]) s1pin_b.value(step[1]) s1pin_c.value(step[2]) s1pin_d.value(step[3]) utime.sleep(rotating_speed) print(rotating_speed, rotation) if key_pressed is '#': print("Stop pressed") rotating_speed = 0 print("Current motor: ", select_servo) print("Current motor direction: ", select_rotation) break if select_servo == "2": while True: #read cw button if select_rotation == "c": rotation = "cw" print("CW") #read acw button if select_rotation == "a": rotation = "acw" print("ACW") adc_read = (potentiometer.read_u16() * conversion_factor) + timing_min rotating_speed = round(adc_read, 3) #print(select_servo, rotating_speed, rotation,Button.value(),pin_a) # rotate clockwise if c pressed if rotation == "cw": for step in rotate_steps_cw: s2pin_a.value(step[0]) s2pin_b.value(step[1]) s2pin_c.value(step[2]) s2pin_d.value(step[3]) utime.sleep(rotating_speed) print(rotating_speed, rotation) # rotate counter clockwise if ON pressed if rotation == "acw": for step in rotate_steps_acw: s2pin_a.value(step[0]) s2pin_b.value(step[1]) s2pin_c.value(step[2]) s2pin_d.value(step[3]) utime.sleep(rotating_speed) print(rotating_speed, rotation) if Button.value() == 0: print("Button pressed") print("Current motor: ", select_servo) print("Current motor direction: ", select_rotation) rotating_speed = 0 break if select_servo == "3": while True: #print(rotation, button_cw.value(), button_acw.value()) #read cw button if select_rotation == "c": rotation = "cw" print("CW") #read acw button if select_rotation == "a": rotation = "acw" print("ACW") adc_read = (potentiometer.read_u16() * conversion_factor) + timing_min rotating_speed = round(adc_read, 3) print(rotation) # rotate clockwise if ON pressed if rotation == "cw": for step in rotate_steps_cw: s3pin_a.value(step[0]) s3pin_b.value(step[1]) s3pin_c.value(step[2]) s3pin_d.value(step[3]) utime.sleep(rotating_speed) print(rotating_speed, rotation) # rotate counter clockwise if ON pressed if rotation == "acw": for step in rotate_steps_acw: s3pin_a.value(step[0]) s3pin_b.value(step[1]) s3pin_c.value(step[2]) s3pin_d.value(step[3]) utime.sleep(.002) print(rotating_speed, rotation) if Button.value() == 0: print("Button pressed") rotating_speed = 0 print("Current motor: ", select_servo) print("Current motor direction: ", select_rotation) break if select_servo == "4": while True: value = int(1350 + (potentiometer.read_u16() / 9.57)) s1.duty_u16(value) time.sleep(0.02) print(value) if Button.value() == 0: print("Button pressed") print("Current motor: ", select_servo) print("Current motor direction: ", select_rotation) s1.duty_u16(100) breakwhile True: run()
Statistics: Posted by ajax12 — Wed Aug 21, 2024 7:50 pm — Replies 0 — Views 36