github.com/aykevl/tinygo@v0.5.0/src/examples/pwm/pwm.go (about)

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  )
     7  
     8  // This example assumes that an RGB LED is connected to pins 3, 5 and 6 on an Arduino.
     9  // Change the values below to use different pins.
    10  const (
    11  	redPin   = 3
    12  	greenPin = 5
    13  	bluePin  = 6
    14  )
    15  
    16  // cycleColor is just a placeholder until math/rand or some equivalent is working.
    17  func cycleColor(color uint8) uint8 {
    18  	if color < 10 {
    19  		return color + 1
    20  	} else if color < 200 {
    21  		return color + 10
    22  	} else {
    23  		return 0
    24  	}
    25  }
    26  
    27  func main() {
    28  	machine.InitPWM()
    29  
    30  	red := machine.PWM{redPin}
    31  	red.Configure()
    32  
    33  	green := machine.PWM{greenPin}
    34  	green.Configure()
    35  
    36  	blue := machine.PWM{bluePin}
    37  	blue.Configure()
    38  
    39  	var rc uint8
    40  	var gc uint8 = 20
    41  	var bc uint8 = 30
    42  
    43  	for {
    44  		rc = cycleColor(rc)
    45  		gc = cycleColor(gc)
    46  		bc = cycleColor(bc)
    47  
    48  		red.Set(uint16(rc) << 8)
    49  		green.Set(uint16(gc) << 8)
    50  		blue.Set(uint16(bc) << 8)
    51  
    52  		time.Sleep(time.Millisecond * 500)
    53  	}
    54  }