/********************************************************************************
for arduino create audios of different sound waves using c plus
In this code, we first define the pin on which the speaker is
connected (in this case, pin 9). In the setup() function,
we set the speaker pin as an output. In the loop() function,
we use the tone() function to play a sine wave, a square wave,
and a triangle wave with frequencies of 440 Hz, 220 Hz,
and 880 Hz, respectively. We use the delay() function to
wait for one second between each sound. Note that for the
triangle wave, we use the analogWrite() function to
generate a varying voltage on the speaker pin, which produces
a triangular wave.
*********************************************************************************/
// Define pins
int speakerPin = 9;
void setup() {
// Set the speaker pin as an output
pinMode(speakerPin, OUTPUT);
}
void loop() {
// Play a 440 Hz sine wave for one second
tone(speakerPin, 440, 1000);
// Wait for one second
delay(1000);
// Play a 220 Hz square wave for one second
tone(speakerPin, 220, 1000);
// Wait for one second
delay(1000);
// Play a 880 Hz triangle wave for one second
for (int i = 0; i < 1000; i++) {
int value = map(i, 0, 1000, 0, 255);
analogWrite(speakerPin, value);
delayMicroseconds(1000 / 880);
}
// Wait for one second
delay(1000);
}