# Exercise 12(b)
from machine import Pin, ADC
from neopixel import NeoPixel
from time import sleep
# Initialize 16-pixel ring on GPIO pin 12
ring = NeoPixel(Pin(12), 16)
pot1 = ADC(Pin(36)) # pin 36 will be used as an ADC input line
pot2 = ADC(Pin(39)) # pin 39 will be used as an ADC input line
pot3 = ADC(Pin(34)) # pin 34 will be used as an ADC input line
r=1000
direction = True
switch = Pin(19,Pin.IN,Pin.PULL_UP) # configure the target GPIO line as an input
def My_ISR(pin): # Interrupt Service Routine (ISR)
global direction,r # global variable the ISR code can update
if direction ==True:
direction =False
else:
direction =True
print(f'Direction: {direction}')
r=r+1
print(r)
# Attach the interrupt to the GPIO line using the irq() method
switch.irq(handler= My_ISR, trigger = Pin.IRQ_FALLING)
position = 0
while True:
pot1_value = int(pot1.read()/16)
#print(pot1_value)
pot2_value = int(pot2.read()/16)
#print(pot2_value)
pot3_value = int(pot3.read()/16)
#print(pot3_value)
if direction==True:
position=position+1
if position==16:
position=0
ring[position] = (pot1_value, pot2_value, pot3_value) # Set pixel 0 to red
else:
position=position-1
if position==-1:
position=15
ring[position] = (pot1_value, pot2_value, pot3_value) # Set pixel 0 to red
# Update all pixels simultaneously
ring.write()
sleep(.2)