Projekt Arduino: GAMA
W tym projekcie nauczysz się komponować proste melodyjki i uruchamiać je za pomocą switcha. Ustawisz także diodę led, by migała w rytm granej melodii.
const int buttonPin = 7;
const int buzzerPin = 8;
const int ledPin = 9;
int buttonState = HIGH;
// Nuty (C D E F G A B C)
int melody[] = {262, 294, 330, 349, 392, 440, 494, 523};
int noteDurations[] = {4, 4, 4, 4, 4, 4, 4, 4};
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
for (int i = 0; i < 8; i++) {
int noteDuration = 1000 / noteDurations[i];
digitalWrite(ledPin, HIGH); // LED włączony z dźwiękiem
tone(buzzerPin, melody[i], noteDuration);
delay(noteDuration); // gra nutę
digitalWrite(ledPin, LOW); // LED gaśnie po nucie
delay(noteDuration * 0.3); // krótka przerwa między nutami
}
// Czekaj aż puścisz przycisk
while (digitalRead(buttonPin) == LOW);
delay(200);
}
}
Opis działania
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Podłączenie układu
Podłącz komponenty zgodnie ze schematem poniżej.
Brak komentarzy:
Prześlij komentarz