Created
June 11, 2024 09:53
-
-
Save nobonobo/ba31820afb2f2dc63ce32f41bae64c1a to your computer and use it in GitHub Desktop.
TinyGo + MicroBit:v2 で IMUを読む
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"machine" | |
"time" | |
"tinygo.org/x/drivers/lsm303agr" | |
) | |
func main() { | |
machine.I2C0.Configure(machine.I2CConfig{}) | |
sensor := lsm303agr.New(machine.I2C0) | |
if err := sensor.Configure(lsm303agr.Configuration{}); err != nil { | |
for { | |
println(err) | |
time.Sleep(5 * time.Second) | |
} | |
} | |
for { | |
time.Sleep(time.Second) | |
if !sensor.Connected() { | |
println("LSM303AGR/MAG not connected!") | |
continue | |
} | |
x, y, z, err := sensor.ReadAcceleration() | |
if err != nil { | |
println(err) | |
continue | |
} | |
xf, yf, zf := float64(x)/100000, float64(y)/100000, float64(z)/100000 | |
fmt.Printf("Acceleration: %.1f, %.1f, %.1f\n", xf, yf, zf) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment