Experiment - blink multiple sequences
In this experiment, I want to flash the LED three times rapidly then blink the LED continuously.I created two functions. The first function flashes a LED rapidly three times. To accomplish this, I used a for loop and the sleep function. The second function flashed the LED every second. The 'work' variable calls the two new functions.
Here is the code
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 ( | |
"time" | |
"gobot.io/x/gobot" | |
"gobot.io/x/gobot/drivers/gpio" | |
"gobot.io/x/gobot/platforms/raspi" | |
) | |
func threeFast(led *gpio.LedDriver, delay time.Duration) { | |
for i := 0; i < 3; i++ { | |
led.On() | |
time.Sleep(delay) | |
led.Off() | |
time.Sleep(delay) | |
} | |
time.Sleep(2 * delay) | |
} | |
func blink(led *gpio.LedDriver) { | |
gobot.Every(time.Second, func() { | |
led.Toggle() | |
}) | |
} | |
func main() { | |
r := raspi.NewAdaptor() | |
led := gpio.NewLedDriver(r, "12") | |
work := func() { | |
delay, _ := time.ParseDuration("200ms") | |
threeFast(led, delay) | |
blink(led) | |
} | |
robot := gobot.NewRobot("blinkBot", | |
[]gobot.Connection{r}, | |
[]gobot.Device{}, | |
work, | |
) | |
robot.Start() | |
} |
Experiment - changing brightness
Let's try something a little bit more complex. I want to be able to control the brightness of the LED. The first thing I did grab the example code (api_led_brightness) from Gobot. My first attempt at running the code failed. The code compiled and seems to run but the LED would not change brightness. It took a bit of head scratching before I remembered the Gobot documentation said something about PWM.The Gobot documentation references pi-blaster. I found that I needed to setup pi-blaster to get the PWM working.
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
#Get Pi-blaster | |
git clone https://github.com/sarfata/pi-blaster.git | |
#Switch folders | |
cd pi-blaster | |
#Building | |
./autogen.sh | |
./configure | |
make | |
#Manually start pi-blaster | |
sudo ./pi-blaster | |
#Manually stop pi-blaster | |
sudo killall pi-blaster |
Resources
- Gobot raspi_blink example - https://gobot.io/documentation/examples/raspi_blink/
- Gobot rapi_led_brightness example - https://gobot.io/documentation/examples/raspi_led_brightness/
- Control Brightness of LED Using Raspberry Pi. The code example is in Python. The blog post has a nice description of PWM.
- Gobot documentation for Raspberry Pi - https://gobot.io/documentation/platforms/raspi/
- Pi-blaster - https://github.com/sarfata/pi-blaster
- Software PWM on a Raspberry Pi - http://ozzmaker.com/software-pwm-on-a-raspberry-pi/