Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added analog inputs and buzzer with example
- Loading branch information
1 parent
59cd692
commit f8b6b59
Showing
2 changed files
with
56 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#! /usr/bin/env python | ||
""" | ||
This is an example that demonstrates how to use a | ||
photoresistor to control a buzzer (piezo element) | ||
using breakfast_serial. It assumes you have an | ||
photoresistor (or some equivalent analog input) | ||
wired up to pin A0 and a buzzer on pin 8. | ||
""" | ||
from breakfast_serial import Arduino, Buzzer, Sensor, setInterval | ||
from time import sleep | ||
|
||
board = Arduino() | ||
buzzer = Buzzer(board, "8") | ||
sensor = Sensor(board, "A0") | ||
|
||
def loop(): | ||
value = sensor.value or 1 # value is initially None | ||
value = value / 2 | ||
|
||
buzzer.on() | ||
sleep(value) | ||
buzzer.off() | ||
sleep(value) | ||
|
||
setInterval(loop, 0) | ||
|
||
# Run an interactive shell so you can play (not required) | ||
import code | ||
code.InteractiveConsole(locals=globals()).interact() |