Merge branch 'master' of github.com:adafruit/Adafruit_Learning_System_Guides

This commit is contained in:
ladyada 2017-10-01 20:21:12 -04:00
commit 4464d6de71
8 changed files with 81 additions and 0 deletions

View file

@ -0,0 +1,48 @@
/* Adafruit Trinket/Gemma Example: Simple Theramin
Read the voltage from a Cadmium Sulfide (CdS) photocell voltage
divider and output a corresponding tone to a piezo buzzer
Wiring: Photocell voltage divider center wire to GPIO #2
(analog 1) and output tone to GPIO #0 (digital 0)
Note: The Arduino tone library does not work for the ATTiny85 on the
Trinket and Gemma. The beep function below is similar. The beep
code is adapted from Dr. Leah Buechley at
http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html
*/
#define SPEAKER 0 // Speaker connected to this DIGITAL pin #
#define PHOTOCELL 1 // CdS photocell connected to this ANALOG pin #
#define SCALE 2.0 // You can change this to change the tone scale
void setup() {
// Set SPEAKER pin to output to drive the piezo buzzer (important)
pinMode(SPEAKER, OUTPUT);
}
void loop() {
// Read PHOTOCELL analog pin for the current CdS divider voltage
int reading = analogRead(PHOTOCELL);
// Change the voltage to a frequency. You can change the values
// to scale your frequency range.
int freq = 220 + (int)(reading * SCALE);
// Output the tone to SPEAKER pin. You can change the '400'
// to different times (in milliseconds).
beep(SPEAKER, freq, 400);
delay(50); // Delay a bit between notes (also adjustable to taste)
}
// The sound-producing function
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds)
{ // http://web.media.mit.edu/~leah/LilyPad/07_sound_code.html
int x;
long delayAmount = (long)(1000000 / frequencyInHertz);
long loopTime = (long)((timeInMilliseconds * 1000) / (delayAmount * 2));
for (x = 0; x < loopTime; x++) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin, LOW);
delayMicroseconds(delayAmount);
}
}

View file

@ -0,0 +1,33 @@
# Adafruit Trinket/Gemma Example: Simple Theramin
# Read the voltage from a Cadmium Sulfide (CdS) photocell voltage
# divider and output a corresponding tone to a piezo buzzer
#
# Photocell voltage divider center wire to GPIO #2 (analog 1)
# and output tone to GPIO #0 (digital 0)
import board
import analogio
import digitalio
import time
speaker_pin = board.D0 # Speaker is connected to this DIGITAL pin
photocell_pin = board.A1 # CdS photocell connected to this ANALOG pin
scale = 0.03 # Change this to adjust tone scale
readpin = analogio.AnalogIn(photocell_pin)
writepin = digitalio.DigitalInOut(speaker_pin)
writepin.direction = digitalio.Direction.OUTPUT
while True: # Loop forever...
# Read photocell analog pin and convert voltage to frequency
freq_hz = 220 + readpin.value * scale
delay_amount = 0.5 / freq_hz # 1/2 Frequency
note_start = time.monotonic() # Time when note started
while time.monotonic() - note_start < 0.4: # For 400 milliseconds...
writepin.value = True # Set pin high
time.sleep(delay_amount) # Wait for 1/2 of note frequency
writepin.value = False # Set pin low
time.sleep(delay_amount) # Wait for other 1/2 of note freq.
time.sleep(0.05) # Delay 50 ms between notes (also adjustable)