From 3fba273897c943380c34ade639cd42e3d5fe124c Mon Sep 17 00:00:00 2001 From: dennisdebel Date: Sun, 6 Oct 2019 17:00:44 +0200 Subject: [PATCH] Added explicit osc example --- .../simple-osc-explained.ino | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 modules/simple-osc-explained/simple-osc-explained.ino diff --git a/modules/simple-osc-explained/simple-osc-explained.ino b/modules/simple-osc-explained/simple-osc-explained.ino new file mode 100644 index 0000000..f6ddbc9 --- /dev/null +++ b/modules/simple-osc-explained/simple-osc-explained.ino @@ -0,0 +1,45 @@ +/* + * + * 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 +}