more explaining

master
dennisdebel 5 years ago
parent ec2c5601e0
commit 656e500138

@ -31,15 +31,31 @@ void setup() {
void loop() {
//in simple-osc.ino we use tone(outputpin, 1000); to make a 1000Hz tone
//1000Hz means 1000x on and off in one second.
//instead of using tone(), we can write out what happens:
//turn on a pin on and of on/off 1000x per second:
digitalWrite( SPEAKER_PIN, LOW );
delayMicroseconds( analogRead(A2)*2 ); // wait
digitalWrite( SPEAKER_PIN, HIGH );
delayMicroseconds( analogRead(A2)*2 ); // wait
digitalWrite( SPEAKER_PIN, LOW ); //turn pin off
delayMicroseconds( 500 ); // wait
digitalWrite( SPEAKER_PIN, HIGH );//tunr pin on
delayMicroseconds( 500 ); // wait
//the wait period determains how long it takes to loop through the
//code, and thus how fast the code runs, and thus how fast
//the pin turns on and off and thus at what frequency this happens:
//we get an oscillation (on-off)!
//Maths:
// for 1Hz, the pin goes on and off in one second. So the delay should be 0.5 second
// for 1000Hz the pin goes on/off 1000x in 1 sec. So the delay should be 1000x smaller: 0.0005 seconds
// wich is 500 micro seconds (dont confuse milliseconds with microseconds).
//To control the frequency with a knob we can use analogRead, uncomment the code below (and comment out the one above):
// digitalWrite( SPEAKER_PIN, LOW ); //turn pin off
// delayMicroseconds( analogRead(A2) ); // wait
// digitalWrite( SPEAKER_PIN, HIGH );//tunr pin on
// 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
}

Loading…
Cancel
Save