gobot.io/x/gobot@v1.16.0/examples/tinkerboard_yl40.go (about)

     1  // +build example
     2  //
     3  // Do not build by default.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"time"
    11  
    12  	"gobot.io/x/gobot"
    13  	"gobot.io/x/gobot/drivers/i2c"
    14  	"gobot.io/x/gobot/platforms/tinkerboard"
    15  )
    16  
    17  func main() {
    18  	// Wiring
    19  	// PWR  Tinkerboard: 1 (+3.3V, VCC), 6, 9, 14, 20 (GND)
    20  	// I2C1 Tinkerboard: 3 (SDA), 5 (SCL)
    21  	// YL-40 module: wire AOUT --> AIN2 for this example
    22  	//
    23  	// Note: temperature measurement is often buggy, because sensor is not properly grounded
    24  	//       fix it by soldering a small bridge to the adjacent ground pin of brightness sensor
    25  	board := tinkerboard.NewAdaptor()
    26  	yl := i2c.NewYL40Driver(board, i2c.WithBus(1))
    27  
    28  	work := func() {
    29  		// the LED light is visible above ~1.7V
    30  		writeVal, _ := yl.AOUT()
    31  
    32  		gobot.Every(1000*time.Millisecond, func() {
    33  			if err := yl.Write(writeVal); err != nil {
    34  				fmt.Println(err)
    35  			} else {
    36  				log.Printf(" %.1f V written", writeVal)
    37  				writeVal = writeVal + 0.1
    38  				if writeVal > 3.3 {
    39  					writeVal = 0
    40  				}
    41  			}
    42  
    43  			if brightness, err := yl.ReadBrightness(); err != nil {
    44  				fmt.Println(err)
    45  			} else {
    46  				log.Printf("Brightness: %.0f [0..1000]", brightness)
    47  			}
    48  
    49  			if temperature, err := yl.ReadTemperature(); err != nil {
    50  				fmt.Println(err)
    51  			} else {
    52  				log.Printf("Temperature: %.1f °C", temperature)
    53  			}
    54  
    55  			if ain2, err := yl.ReadAIN2(); err != nil {
    56  				fmt.Println(err)
    57  			} else {
    58  				log.Printf("Read back AOUT: %.1f [0..3.3]", ain2)
    59  			}
    60  
    61  			if potiState, err := yl.ReadPotentiometer(); err != nil {
    62  				fmt.Println(err)
    63  			} else {
    64  				log.Printf("Resistor: %.0f %% [-100..+100]", potiState)
    65  			}
    66  		})
    67  	}
    68  
    69  	robot := gobot.NewRobot("yl40Bot",
    70  		[]gobot.Connection{board},
    71  		[]gobot.Device{yl},
    72  		work,
    73  	)
    74  
    75  	robot.Start()
    76  }