You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
995 B
Arduino
46 lines
995 B
Arduino
5 years ago
|
/*
|
||
|
*
|
||
|
* Simple (PWM) Oscillator Explained
|
||
|
*
|
||
|
Instead of using the Tone() function, you can create a square wave oscillator by
|
||
|
turning the digital pin 11 (speakerpin) on (HIGH) and off (LOW) very fast:
|
||
|
|
||
|
|
||
|
|
|
||
|
HIGH | ___ ___
|
||
|
| | | | | 50% duty cycle
|
||
|
LOW | ____| |____| | (50% on, 50 off) Determined by delayMicroseconds()
|
||
|
|
|
||
|
|____________________
|
||
|
TIME
|
||
|
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
|
||
|
#define SPEAKER_PIN 11
|
||
|
|
||
|
void setup() {
|
||
|
// set pin 11 as output
|
||
|
pinMode(SPEAKER_PIN, OUTPUT);
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
|
||
|
|
||
|
digitalWrite( SPEAKER_PIN, LOW );
|
||
|
delayMicroseconds( analogRead(A2) ); // wait
|
||
|
digitalWrite( SPEAKER_PIN, HIGH );
|
||
|
delayMicroseconds( analogRead(A2) ); // wait
|
||
|
|
||
|
|
||
|
|
||
|
//note: delayMircoseconds wont pause your whole code like normal delay() would do
|
||
|
//so, the code inside the while loop is executed simultaniously with the code in the
|
||
|
//main loop. See usage in simple-poly-osc.ino
|
||
|
}
|