Search code examples
javascriptbbc-microbit

Light to go from value 0 to 255 then to 0 again


I'm working on a light puls code, i want the light to go from value dark (0) then increase to bright (255).

I have been trying to use a "for" to get this to work but i am really unsure. The code starts with the light on 0, then i want to increase it to 255, then have a pause of 0.50 seconds then repeat

basic.forever(function () {
    led.setBrightness(input.lightLevel())
    for led.setBrightness >255 
    
    basic.showIcon(IconNames.Heart)
    basic.pause(500)
})

Solution

  • You seem to be using the MakeCode editor and one of the great advantages of the MakeCode editor is that you can switch between blocks and the language.

    This means you can start by doing the for loop in blocks: enter image description here

    And then switch to the JavaScript view to see what the code looks like:

    basic.forever(function () {
        for (let brightness = 0; brightness <= 255; brightness++) {
            led.setBrightness(brightness)
            basic.showIcon(IconNames.Heart)
        }
        basic.pause(2000)
    })