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 52 53 54 55 56 57 58 59 60 61 62 63 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ====================================================================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: ts=.05 if servoState==0 and servoToggle==True: for i in range(0,180,1): myServo.pos(i) time.sleep(ts) servoToggle=False servoState=180 if servoState==180 and servoToggle==True: for i in range(180,0,-1): myServo.pos(i) time.sleep(ts) servoToggle=False servoState=0 print("othCore Terminated") _thread.start_new_thread(othCore,()) ts=.25 time.sleep(ts) try: while True: redButStateNow=myRedButton.value() if redButStateNow==0 and redButStateOld==1: print("Toggle") servoToggle =True redButStateOld=redButStateNow ts=.1 time.sleep(ts) except KeyboardInterrupt: print('Keyboard Interrupt') running=False ts=.1 time.sleep(ts) print("Program is Done") |