The modified code in micropython is this:
from machine import Pin, ADC
import utime
import machine, neopixel
import urandom
led = 18 # GPIO18
sensor_ADC0 = 26 # GPIO26
neopixelPin = 22 #GPIO22
np = neopixel.NeoPixel(machine.Pin(neopixelPin), 4)
#Function for turning on/off Neopixels
# 1 = On
# 0 = Off
def neopixelControl(control):
if control:
np.fill((red, blue, green)) # Set all pixels to red
np.write() # Update the NeoPixel strip
np[0] = (255, 255, 255) # set to white, full brightness
np[1] = (255, 255, 255) # set to white, full brightness
np[2] = (255, 255, 255) # set to white, full brightness
np[3] = (255, 255, 255) # set to white, full brightness
else:
np.fill((0, 0, 0)) # Turn off all pixels
np.write() # Update the NeoPixel strip
np[0] = (0, 0, 0) # set power off
np[1] = (0, 0, 0) # set power off
np[2] = (0, 0, 0) # set power off
np[3] = (0, 0, 0) # set power off
ADC0 = machine.ADC(sensor_ADC0)
led = Pin(led, Pin.OUT)
led.on()
off=True
on=False
while True:
while off:
random_num1 = urandom.getrandbits(8)
random_num2 = urandom.getrandbits(8)
random_num3 = urandom.getrandbits(8)
red = random_num1
blue = random_num2
green = random_num3
sensorReading = ADC0.read_u16()
print("Sensor State: ", sensorReading)
if sensorReading < 45000:
print("Sensor detected")
neopixelControl(1)
on=True
off=False
utime.sleep(0.5)
while on:
sensorReading = ADC0.read_u16()
print("Sensor State: ", sensorReading)
if sensorReading < 45000:
print("Sensor detected")
neopixelControl(0)
off=True
on=False
utime.sleep(0.5)