# Exercise 12(e)
from machine import Pin, ADC, Timer
from neopixel import NeoPixel
from time import sleep
# set colours all to zero initially
red=0
green=0
blue=0
# 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
switch = Pin(19,Pin.IN,Pin.PULL_UP) # configure the target GPIO line as an input
def my_timer_ISR (timer): # internal interrupt service routine (ISR)
global red,green,blue
red = int(pot1.read()/16)
green = int(pot2.read()/16)
blue = int(pot3.read()/16)
my_timer = Timer(0)
my_timer.init(mode = Timer.PERIODIC, period = 100, callback = my_timer_ISR )
def My_ISR(pin): # external interrupt Service Routine (ISR)
global direction # global variable the ISR code can update
if direction == True:
direction = False
else:
direction =True
# Attach the interrupt to the GPIO line using the irq() method
switch.irq(handler= My_ISR, trigger = Pin.IRQ_FALLING)
# starting condition:- clockwise direction starting at element number 0
direction = True
position = 0
while True:
if direction==True: # clockwise direction
position=position+1
if position==16: # need to reset position to 0 to start next rotation
position=0
else: # anti-clockwise direction
position=position-1
if position==-1: # need to set position to 15 to start next rotation
position=15
ring[position] = (red, green, blue)
ring.write()
sleep(0.25)