In this lesson we demonstrate using both cores on the Raspberry Pi Pico W. The program controls a servo from a push button. The servo is controlled on the second core by a thread, and the push button is monitored in the main program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
from machine import Pin import time import _thread import SERVO servoToggle=False running = True servoPin=17 myServo=SERVO.servo(servoPin) redButPin=16 myRedButton=Pin(redButPin,Pin.IN,Pin.PULL_UP) redButStateNow=1 redButStateOld=1 def othCore(): global servoToggle global running servoState=0 while running: if servoState==0 and servoToggle==True: for i in range(0,180,1): myServo.pos(i) time.sleep(.05) servoToggle=False servoState=180 if servoState==180 and servoToggle==True: for i in range(180,0,-1): myServo.pos(i) time.sleep(.05) servoToggle=False servoState=0 print("othCore Terminated") _thread.start_new_thread(othCore,()) time.sleep(.25) try: while True: redButStateNow=myRedButton.value() if redButStateNow==0 and redButStateOld==1: print("Toggle") servoToggle =True redButStateOld=redButStateNow time.sleep(.1) except KeyboardInterrupt: print('Keyboard Interrupt') running=False time.sleep(.1) print("Program is Done") |