jadvancek

Pianino

Aug 17th, 2023 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.59 KB | Source Code | 0 0
  1. // Deklaracje pinów dla brzęczyka i przycisków oraz stanów przycisków
  2. int buzzerPin = 8;
  3. int buttonPin1 = 2;
  4. int buttonPin2 = 3;
  5. int buttonPin3 = 4;
  6. int buttonPin4 = 5;
  7. int buttonPin5 = 6;
  8. int buttonPin6 = 7;
  9.  
  10. int buttonState1 = 0;
  11. int buttonState2 = 0;
  12. int buttonState3 = 0;
  13. int buttonState4 = 0;
  14. int buttonState5 = 0;
  15. int buttonState6 = 0;
  16.  
  17. // Tablica z częstotliwościami dźwięków do odtwarzania na brzęczyku
  18. int notes[] = {262, 294, 330, 349, 392, 440};
  19.  
  20. void setup() {
  21.   // Konfiguracja pinów jako wejścia z wewnętrznym pull-up
  22.   pinMode(buttonPin1, INPUT_PULLUP);
  23.   pinMode(buttonPin2, INPUT_PULLUP);
  24.   pinMode(buttonPin3, INPUT_PULLUP);
  25.   pinMode(buttonPin4, INPUT_PULLUP);
  26.   pinMode(buttonPin5, INPUT_PULLUP);
  27.   pinMode(buttonPin6, INPUT_PULLUP);
  28.  
  29.   // Konfiguracja pinu brzęczyka oraz pinów diod LED
  30.   pinMode(buzzerPin, OUTPUT);
  31.   pinMode(LED1, OUTPUT);
  32.   pinMode(LED2, OUTPUT);
  33.   pinMode(LED3, OUTPUT);
  34.   pinMode(LED4, OUTPUT);
  35.   pinMode(LED5, OUTPUT);
  36.   pinMode(LED6, OUTPUT);
  37. }
  38.  
  39. void loop() {
  40.   // Odczyt stanu przycisków
  41.   buttonState1 = digitalRead(buttonPin1);
  42.   buttonState2 = digitalRead(buttonPin2);
  43.   buttonState3 = digitalRead(buttonPin3);
  44.   buttonState4 = digitalRead(buttonPin4);
  45.   buttonState5 = digitalRead(buttonPin5);
  46.   buttonState6 = digitalRead(buttonPin6);
  47.  
  48.   // Obsługa przycisków i brzęczyka
  49.   if (buttonState1 == LOW) {
  50.     // Włącz diodę LED i odtwórz pierwszą nutę
  51.     digitalWrite(LED1, HIGH);
  52.     tone(buzzerPin, notes[0], 100);
  53.     delay(100);
  54.     noTone(buzzerPin);
  55.     // Wyłącz diodę LED
  56.     digitalWrite(LED1, LOW);
  57.   }
  58.   else if (buttonState2 == LOW) {
  59.     // Analogicznie dla drugiego przycisku i nuty
  60.     digitalWrite(LED2, HIGH);
  61.     tone(buzzerPin, notes[1], 100);
  62.     delay(100);
  63.     noTone(buzzerPin);
  64.     digitalWrite(LED2, LOW);
  65.   }
  66.   // Analogicznie dla pozostałych przycisków i nut
  67.   else if (buttonState3 == LOW) {
  68.     digitalWrite(LED3, HIGH);
  69.     tone(buzzerPin, notes[2], 100);
  70.     delay(100);
  71.     noTone(buzzerPin);
  72.     digitalWrite(LED3, LOW);
  73.   }
  74.   else if (buttonState4 == LOW) {
  75.     digitalWrite(LED4, HIGH);
  76.     tone(buzzerPin, notes[3], 100);
  77.     delay(100);
  78.     noTone(buzzerPin);
  79.     digitalWrite(LED4, LOW);
  80.   }
  81.   else if (buttonState5 == LOW) {
  82.     digitalWrite(LED5, HIGH);
  83.     tone(buzzerPin, notes[4], 100);
  84.     delay(100);
  85.     noTone(buzzerPin);
  86.     digitalWrite(LED5, LOW);
  87.   }
  88.   else if (buttonState6 == LOW) {
  89.     digitalWrite(LED6, HIGH);
  90.     tone(buzzerPin, notes[5], 100);
  91.     delay(100);
  92.     noTone(buzzerPin);
  93.     digitalWrite(LED6, LOW);
  94.   }
  95. }
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment