File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
libraries/I2S/examples/SimpleTone Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < I2S.h>
2+
3+ const int frequency = 440 ; // frequency of square wave in Hz
4+ const int amplitude = 500 ; // amplitude of square wave
5+ const int sampleRate = 8000 ; // sample rate in Hz
6+
7+ const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
8+
9+ short sample = amplitude; // current sample value
10+ int count = 0 ;
11+
12+ void setup () {
13+ Serial.begin (9600 );
14+ Serial.println (" I2S simple tone" );
15+
16+ if (I2S.begin (I2S_PHILIPS_MODE, sampleRate, 16 )) {
17+ Serial.println (" Failed to initialize I2S!" );
18+ while (1 ); // do nothing
19+ }
20+ }
21+
22+ void loop () {
23+ if (count % halfWavelength == 0 ) {
24+ // invert the sample every half wavelength count multiple to generate square wave
25+ sample = -1 * sample;
26+ }
27+
28+ // write the same sample twice, once for left and once for the right channel
29+ I2S.write (sample);
30+ I2S.write (sample);
31+
32+ // increment the counter for the next sample
33+ count++;
34+ }
You can’t perform that action at this time.
0 commit comments