Microsoft MakeCode

LED blink when light level under 100

I would like to run a function that turns on and off a LED when the light level is under 100.
I gave it a try, but it did not work. Can anyone help?
Here´s the code:

lightlevel = 0
def turn_off():
pins.digital_write_pin(DigitalPin.P1, 0)
def intervall():
pins.digital_write_pin(DigitalPin.P1, 1)
basic.pause(1000)
pins.digital_write_pin(DigitalPin.P1, 0)
basic.pause(randint(1000, 5000))

def on_forever():
global lightlevel
lightlevel = input.light_level()
if input.light_level() >= 100:
intervall()
else:
turn_off()
basic.forever(on_forever)

@Theoforus Does it need any extensions to run.

Thank you for your reply. I don´t think so. This is just a test to see if I can get a device turn on and off depending on the amount of light in the room. By now I´m just connecting a LED to P1 at the microbit.

1 Like

Here are the link to the project in makecode: https://makecode.microbit.org/_Pb5RzC5L1hp4

2 Likes

I found out. The light level was set to =0. When I removed it all was fine.