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

     1  package main
     2  
     3  import (
     4  	"machine"
     5  	"time"
     6  )
     7  
     8  // This example assumes that an analog sensor such as a rotary dial is connected to pin ADC0.
     9  // When the dial is turned past the midway point, the built-in LED will light up.
    10  
    11  func main() {
    12  	machine.InitADC()
    13  
    14  	led := machine.GPIO{machine.LED}
    15  	led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT})
    16  
    17  	sensor := machine.ADC{machine.ADC2}
    18  	sensor.Configure()
    19  
    20  	for {
    21  		val := sensor.Get()
    22  		if val < 0x8000 {
    23  			led.Low()
    24  		} else {
    25  			led.High()
    26  		}
    27  		time.Sleep(time.Millisecond * 100)
    28  	}
    29  }