Created
July 28, 2013 07:11
-
-
Save ZachMassia/6097785 to your computer and use it in GitHub Desktop.
Concurrent blinking.
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 ( | |
"github.com/ZachMassia/gofirmata" | |
"log" | |
"time" | |
) | |
func main() { | |
a, err := firmata.NewBoard("/dev/ttyACM0", 57600) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer a.Close() | |
// Output LED pins | |
red := byte(23) | |
grn := byte(3) | |
// Set modes | |
a.SetPinMode(red, firmata.OUTPUT) | |
a.SetPinMode(grn, firmata.OUTPUT) | |
// Start flashing two LEDs concurently. | |
Blink(a, red, time.Tick(250*time.Millisecond)) | |
Blink(a, grn, time.Tick(500*time.Millisecond)) | |
// Run for 10 seconds | |
quit := time.After(10 * time.Second) | |
for { | |
select { | |
case <-a.Reader: | |
// Needed for now to keep the message loop from blocking. | |
continue | |
case <-quit: | |
return | |
} | |
} | |
} | |
// Blink flashes a LED every tick. | |
func Blink(b *firmata.Board, pin byte, tick <-chan time.Time) { | |
state := firmata.LOW | |
go func() { | |
for _ = range tick { | |
if state == firmata.LOW { | |
state = firmata.HIGH | |
} else { | |
state = firmata.LOW | |
} | |
b.DigitalWrite(pin, state) | |
} | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment