Created
September 3, 2017 08:16
-
-
Save rexhex/9ffc0e99cffc0a1617a2c6904b18e4de to your computer and use it in GitHub Desktop.
pwm and for loops to create a simple sound
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
// hook up a speakers positive wire to your pwm pin | |
// speaker ground goes to ground | |
// you can add in a potentiometer for volume control | |
// just put the pwm pin to one end lead of the pot | |
// the the middle pin on pot goes to the speakers (+) | |
// define the speaker pin. (pin 4 is D2 on the wemos mini) | |
#define speaker 4 | |
// define some frequencies | |
int freq1 = 528; | |
int freq2 = 420; | |
int freq3 = 518; | |
int freq4 = 430; | |
//function using pwm and for loops to create a simple sound. | |
void bleep() | |
{ | |
for (int i = 0; i < 15; i++) | |
{ | |
freq1 += i; | |
analogWriteRange(1023); | |
analogWriteFreq(freq1); | |
analogWrite(speaker,512); | |
delay(10); | |
} | |
freq1 = 580; | |
for (int i = 5; i > 0; i--) | |
{ | |
freq1 -= i; | |
analogWriteRange(1023); | |
analogWriteFreq(freq1); | |
analogWrite(speaker,512); | |
delay(25); | |
} | |
for (int i = 0; i < 10; i++) | |
{ | |
freq3 += i; | |
analogWriteRange(1023); | |
analogWriteFreq(freq3); | |
analogWrite(speaker,512); | |
delay(10); | |
} | |
analogWriteRange(1023); | |
analogWriteFreq(freq1); | |
analogWrite(speaker,512); | |
delay(100); | |
freq3 = 425; | |
freq1 = 415; | |
} | |
void setup() { | |
pinMode (speaker, OUTPUT); | |
} | |
void loop() { | |
bleep(); | |
//pause the sound for 5 seconds | |
analogWrite(speaker,0); | |
delay(5000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment