Merge branch 'master' of github.com:adafruit/Adafruit_Learning_System_Guides
This commit is contained in:
commit
4464d6de71
8 changed files with 81 additions and 0 deletions
0
LED_Masquerade_Masks/LED_Masquerade_Masks.ino
Normal file
0
LED_Masquerade_Masks/LED_Masquerade_Masks.ino
Normal file
0
LED_Masquerade_Masks/LED_Masquerade_Masks.py
Normal file
0
LED_Masquerade_Masks/LED_Masquerade_Masks.py
Normal file
48
Trinket_Gemma_Mini_Theramin/Trinket_Gemma_Mini_Theramin.ino
Normal file
48
Trinket_Gemma_Mini_Theramin/Trinket_Gemma_Mini_Theramin.ino
Normal 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);
|
||||
}
|
||||
}
|
||||
33
Trinket_Gemma_Mini_Theramin/Trinket_Gemma_Mini_Theramin.py
Normal file
33
Trinket_Gemma_Mini_Theramin/Trinket_Gemma_Mini_Theramin.py
Normal 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)
|
||||
Loading…
Reference in a new issue