Generate tones with the Arduino and a speaker
FunduinoLast updated on
Vom einzelnen Piepton bis zur Melodie mit dem passiven Lautsprecher
Active and passive speakers – a brief overview
Connect the speaker
How the tone() command works
tone(8, 100); // The speaker on pin 8 is activated at a pitch of "100" delay(1000); // Pause for 1000 milliseconds, or 1 second - the speaker remains active for this long. noTone(8); // The speaker on pin 8 is deactivated.
Simple sound generation
Program code - Simple tone generation
// Im Setup werden keine Informationen benötigt.
// Die Spannungsausgabe für den Piezo-Lautsprecher legt der Arduino-Befehl "tone" automatisch fest.
void setup()
{
}
void loop()
{
tone(8, 100); // Im Hauptteil wird nun mit dem Befehl "tone(x, y)" ein Ton abgegeben.
delay(1000); // mit einer Dauer von 1 Sekunde
noTone(8); // Der Ton wird abgeschaltet
delay(1000); // Der Lautsprecher bleibt eine Sekunde aus
}Generate alternating pitches
Program Code - Alternating Pitches
void setup()
{
}
void loop()
{
tone(8, 100);
delay(1000);
noTone(8); // The first tone stops here.
tone(8, 200); // The second tone with a pitch of "200" sounds.
delay(1000); // ... for one second ...
noTone(8); // The second tone stops here, and the loop starts over.
}Generate a tone at the press of a button
Program code - sound at the press of a button
int Button = 6;
int ButtonStatus = 0;
void setup()
{
pinMode(Button, INPUT);
}
void loop()
{
ButtonStatus = digitalRead(Button);
if (ButtonStatus == HIGH) // If the button is pressed ...
{
tone(8, 300); // ... play this tone ...
delay(1000); // ... for one second ...
noTone(8); // turn off the tone
}
}Generate tones depending on different keys
Program Code – Two Buttons, Two Tones
int Taster1 = 6; // Button 1 connected to pin 6
int Taster2 = 7; // Button 2 connected to pin 7
int Tasterstatus1 = 0; // Variable to store the status of button 1.
int Tasterstatus2 = 0; // Variable to store the status of button 2.
void setup()
{
pinMode(Taster1, INPUT); // Set button 1 as an input
pinMode(Taster2, INPUT); // Set button 2 as an input
}
void loop()
{
Tasterstatus1 = digitalRead(Taster1); // Read the status of button 1 (HIGH or LOW)
Tasterstatus2 = digitalRead(Taster2); // Read the status of button 2 (HIGH or LOW)
if (Tasterstatus1 == HIGH) // If button 1 is pressed, then ...
{
tone(8, 100); // Output a tone with a pitch of 100.
delay(1000); // lasting one second
noTone(8); // Turn off the tone.
}
if (Tasterstatus2 == HIGH) // If button 2 is pressed, then ...
{
tone(8, 200); // Output a tone with a pitch of 200.
delay(1000); // lasting one second
noTone(8); // Turn off the tone.
}
}Create melodies with the Arduino
1 / 2
Play notes from pitches.h in program code
#include "pitches.h"
void setup()
{
pinMode(8, OUTPUT); // Speaker on pin 8
}
void loop()
{
tone(8, NOTE_C4, 1000); // Play note C4 on pin 8 for 1000 ms
delay(3000); // The sketch pauses for 3 seconds. Since the tone lasts only 1 second, two seconds of silence remain afterward.
tone(8, NOTE_G3, 1000); // Play note G3 on pin 8 for 1000 ms
delay(3000); // The sketch pauses for 3 seconds. Since the tone lasts only 1 second, two seconds of silence remain afterward.
}Rate the instructions