This shows you the differences between two versions of the page.
pm:prj2024:avaduva:stefan.botea [2024/05/27 02:04] stefan.botea [Software Design] |
pm:prj2024:avaduva:stefan.botea [2024/05/27 02:07] (current) stefan.botea [Software Design] |
||
---|---|---|---|
Line 73: | Line 73: | ||
Inițializare și Setare | Inițializare și Setare | ||
- | #include <Arduino.h> | + | {{:pm:prj2024:avaduva:botea.txt|}} |
- | #include <LedControl.h> | + | |
- | + | ||
- | #define NOTE_B0 31 | + | |
- | #define NOTE_C1 33 | + | |
- | #define NOTE_CS1 35 | + | |
- | #define NOTE_D1 37 | + | |
- | #define NOTE_DS1 39 | + | |
- | #define NOTE_E1 41 | + | |
- | #define NOTE_F1 44 | + | |
- | #define NOTE_FS1 46 | + | |
- | #define NOTE_G1 49 | + | |
- | #define NOTE_GS1 52 | + | |
- | #define NOTE_A1 55 | + | |
- | #define NOTE_AS1 58 | + | |
- | #define NOTE_B1 62 | + | |
- | #define NOTE_C2 65 | + | |
- | #define NOTE_CS2 69 | + | |
- | #define NOTE_D2 73 | + | |
- | #define NOTE_DS2 78 | + | |
- | #define NOTE_E2 82 | + | |
- | #define NOTE_F2 87 | + | |
- | #define NOTE_FS2 93 | + | |
- | #define NOTE_G2 98 | + | |
- | #define NOTE_GS2 104 | + | |
- | #define NOTE_A2 110 | + | |
- | #define NOTE_AS2 117 | + | |
- | #define NOTE_B2 123 | + | |
- | #define NOTE_C3 131 | + | |
- | #define NOTE_CS3 139 | + | |
- | #define NOTE_D3 147 | + | |
- | #define NOTE_DS3 156 | + | |
- | #define NOTE_E3 165 | + | |
- | #define NOTE_F3 175 | + | |
- | #define NOTE_FS3 185 | + | |
- | #define NOTE_G3 196 | + | |
- | #define NOTE_GS3 208 | + | |
- | #define NOTE_A3 220 | + | |
- | #define NOTE_AS3 233 | + | |
- | #define NOTE_B3 247 | + | |
- | #define NOTE_C4 262 | + | |
- | #define NOTE_CS4 277 | + | |
- | #define NOTE_D4 294 | + | |
- | #define NOTE_DS4 311 | + | |
- | #define NOTE_E4 330 | + | |
- | #define NOTE_F4 349 | + | |
- | #define NOTE_FS4 370 | + | |
- | #define NOTE_G4 392 | + | |
- | #define NOTE_GS4 415 | + | |
- | #define NOTE_A4 440 | + | |
- | #define NOTE_AS4 466 | + | |
- | #define NOTE_B4 494 | + | |
- | #define NOTE_C5 523 | + | |
- | #define NOTE_CS5 554 | + | |
- | #define NOTE_D5 587 | + | |
- | #define NOTE_DS5 622 | + | |
- | #define NOTE_E5 659 | + | |
- | #define NOTE_F5 698 | + | |
- | #define NOTE_FS5 740 | + | |
- | #define NOTE_G5 784 | + | |
- | #define NOTE_GS5 831 | + | |
- | #define NOTE_A5 880 | + | |
- | #define NOTE_AS5 932 | + | |
- | #define NOTE_B5 988 | + | |
- | #define NOTE_C6 1047 | + | |
- | #define NOTE_CS6 1109 | + | |
- | #define NOTE_D6 1175 | + | |
- | #define NOTE_DS6 1245 | + | |
- | #define NOTE_E6 1319 | + | |
- | #define NOTE_F6 1397 | + | |
- | #define NOTE_FS6 1480 | + | |
- | #define NOTE_G6 1568 | + | |
- | #define NOTE_GS6 1661 | + | |
- | #define NOTE_A6 1760 | + | |
- | #define NOTE_AS6 1865 | + | |
- | #define NOTE_B6 1976 | + | |
- | #define NOTE_C7 2093 | + | |
- | #define NOTE_CS7 2217 | + | |
- | #define NOTE_D7 2349 | + | |
- | #define NOTE_DS7 2489 | + | |
- | #define NOTE_E7 2637 | + | |
- | #define NOTE_F7 2794 | + | |
- | #define NOTE_FS7 2960 | + | |
- | #define NOTE_G7 3136 | + | |
- | #define NOTE_GS7 3322 | + | |
- | #define NOTE_A7 3520 | + | |
- | #define NOTE_AS7 3729 | + | |
- | #define NOTE_B7 3951 | + | |
- | #define NOTE_C8 4186 | + | |
- | #define NOTE_CS8 4435 | + | |
- | #define NOTE_D8 4699 | + | |
- | #define NOTE_DS8 4978 | + | |
- | + | ||
- | #define NOTE_REST 0 | + | |
- | #define REST 0 | + | |
- | + | ||
- | // Constants for pin numbers | + | |
- | const int DO = 10; // Pin for Digital Output - DO | + | |
- | const int DA = A0; // Pin for Analog Output - AO | + | |
- | const int threshold = 527; // Set minimum threshold for LED lit | + | |
- | const int thresholdMatrix = 527; // Set minimum threshold for MATRIX lit | + | |
- | const int buttonPin = 9; // Pin for button | + | |
- | const int buzzerPin = 8; // Pin for buzzer using digital pin 8 | + | |
- | + | ||
- | const int ledPins[] = {2, 3, 4, 5, 7, 11}; // Array of LED pins, excluding pin 9 and 8 | + | |
- | + | ||
- | int sensorValue = 0; | + | |
- | bool buttonState = HIGH; // Variable to hold the last button state | + | |
- | + | ||
- | LedControl lc = LedControl(12, 13, 6, 1); // Pins: DataIn, CLK, LOAD, numDevices | + | |
- | + | ||
- | int melody[] = { | + | |
- | NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST, | + | |
- | NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST, | + | |
- | NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST, | + | |
- | NOTE_A4, NOTE_G4, NOTE_A4, REST, | + | |
- | + | ||
- | NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST, | + | |
- | NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST, | + | |
- | NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST, | + | |
- | NOTE_A4, NOTE_G4, NOTE_A4, REST, | + | |
- | + | ||
- | NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST, | + | |
- | NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, REST, | + | |
- | NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, REST, | + | |
- | NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, REST, | + | |
- | + | ||
- | NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST, | + | |
- | NOTE_D5, NOTE_E5, NOTE_A4, REST, | + | |
- | NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, REST, | + | |
- | NOTE_C5, NOTE_A4, NOTE_B4, REST, | + | |
- | + | ||
- | NOTE_A4, NOTE_A4, | + | |
- | //Repeat of first part | + | |
- | NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST, | + | |
- | NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST, | + | |
- | NOTE_A4, NOTE_G4, NOTE_A4, REST, | + | |
- | + | ||
- | NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST, | + | |
- | NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST, | + | |
- | NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, REST, | + | |
- | NOTE_A4, NOTE_G4, NOTE_A4, REST, | + | |
- | + | ||
- | NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, REST, | + | |
- | NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, REST, | + | |
- | NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, REST, | + | |
- | NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, REST, | + | |
- | + | ||
- | NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, REST, | + | |
- | NOTE_D5, NOTE_E5, NOTE_A4, REST, | + | |
- | NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, REST, | + | |
- | NOTE_C5, NOTE_A4, NOTE_B4, REST, | + | |
- | //End of Repeat | + | |
- | + | ||
- | NOTE_E5, REST, REST, NOTE_F5, REST, REST, | + | |
- | NOTE_E5, NOTE_E5, REST, NOTE_G5, REST, NOTE_E5, NOTE_D5, REST, REST, | + | |
- | NOTE_D5, REST, REST, NOTE_C5, REST, REST, | + | |
- | NOTE_B4, NOTE_C5, REST, NOTE_B4, REST, NOTE_A4, | + | |
- | + | ||
- | NOTE_E5, REST, REST, NOTE_F5, REST, REST, | + | |
- | NOTE_E5, NOTE_E5, REST, NOTE_G5, REST, NOTE_E5, NOTE_D5, REST, REST, | + | |
- | NOTE_D5, REST, REST, NOTE_C5, REST, REST, | + | |
- | NOTE_B4, NOTE_C5, REST, NOTE_B4, REST, NOTE_A4 | + | |
- | }; | + | |
- | + | ||
- | int durations[] = { | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, | + | |
- | + | ||
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, | + | |
- | + | ||
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 8, 4, 8, | + | |
- | + | ||
- | 8, 8, 4, 8, 8, | + | |
- | 4, 8, 4, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 4, | + | |
- | + | ||
- | 4, 8, | + | |
- | //Repeat of First Part | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, | + | |
- | + | ||
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, | + | |
- | + | ||
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 8, 4, 8, | + | |
- | + | ||
- | 8, 8, 4, 8, 8, | + | |
- | 4, 8, 4, 8, | + | |
- | 8, 8, 4, 8, 8, | + | |
- | 8, 8, 4, 4, | + | |
- | //End of Repeat | + | |
- | + | ||
- | 4, 8, 4, 4, 8, 4, | + | |
- | 8, 8, 8, 8, 8, 8, 8, 8, 4, | + | |
- | 4, 8, 4, 4, 8, 4, | + | |
- | 8, 8, 8, 8, 8, 2, | + | |
- | + | ||
- | 4, 8, 4, 4, 8, 4, | + | |
- | 8, 8, 8, 8, 8, 8, 8, 8, 4, | + | |
- | 4, 8, 4, 4, 8, 4, | + | |
- | 8, 8, 8, 8, 8, 2 | + | |
- | }; | + | |
- | + | ||
- | // Melody notes and durations | + | |
- | // int melody[] = { | + | |
- | // NOTE_REST, NOTE_D4, | + | |
- | // NOTE_G4, NOTE_AS4, NOTE_A4, | + | |
- | // NOTE_G4, NOTE_D5, | + | |
- | // NOTE_C5, | + | |
- | // NOTE_A4, | + | |
- | // NOTE_G4, NOTE_AS4, NOTE_A4, | + | |
- | // NOTE_F4, NOTE_GS4, | + | |
- | // NOTE_D4, | + | |
- | // NOTE_D4, | + | |
- | + | ||
- | // NOTE_G4, NOTE_AS4, NOTE_A4, | + | |
- | // NOTE_G4, NOTE_D5, | + | |
- | // NOTE_F5, NOTE_E5, | + | |
- | // NOTE_DS5, NOTE_B4, | + | |
- | // NOTE_DS5, NOTE_D5, NOTE_CS5, | + | |
- | // NOTE_CS4, NOTE_B4, | + | |
- | // NOTE_G4, | + | |
- | // NOTE_AS4, | + | |
- | + | ||
- | // NOTE_D5, NOTE_AS4, | + | |
- | // NOTE_D5, NOTE_AS4, | + | |
- | // NOTE_DS5, NOTE_D5, | + | |
- | // NOTE_CS5, NOTE_A4, | + | |
- | // NOTE_AS4, NOTE_D5, NOTE_CS5, | + | |
- | // NOTE_CS4, NOTE_D4, | + | |
- | // NOTE_D5, | + | |
- | // NOTE_REST, NOTE_AS4, | + | |
- | + | ||
- | // NOTE_D5, NOTE_AS4, | + | |
- | // NOTE_D5, NOTE_AS4, | + | |
- | // NOTE_F5, NOTE_E5, | + | |
- | // NOTE_DS5, NOTE_B4, | + | |
- | // NOTE_DS5, NOTE_D5, NOTE_CS5, | + | |
- | // NOTE_CS4, NOTE_AS4, | + | |
- | // NOTE_G4 | + | |
- | // }; | + | |
- | + | ||
- | // int durations[] = { | + | |
- | // 2, 4, | + | |
- | // 4, 8, 4, | + | |
- | // 2, 4, | + | |
- | // 2, | + | |
- | // 2, | + | |
- | // 4, 8, 4, | + | |
- | // 2, 4, | + | |
- | // 1, | + | |
- | // 4, | + | |
- | + | ||
- | // 4, 8, 4, | + | |
- | // 2, 4, | + | |
- | // 2, 4, | + | |
- | // 2, 4, | + | |
- | // 4, 8, 4, | + | |
- | // 2, 4, | + | |
- | // 1, | + | |
- | // 4, | + | |
- | + | ||
- | // 2, 4, | + | |
- | // 2, 4, | + | |
- | // 2, 4, | + | |
- | // 2, 4, | + | |
- | // 4, 8, 4, | + | |
- | // 2, 4, | + | |
- | // 1, | + | |
- | // 4, 4, | + | |
- | + | ||
- | // 2, 4, | + | |
- | // 2, 4, | + | |
- | // 2, 4, | + | |
- | // 2, 4, | + | |
- | // 4, 8, 4, | + | |
- | // 2, 4, | + | |
- | // 1 | + | |
- | // }; | + | |
- | + | ||
- | void setup() { | + | |
- | Serial.begin(9600); | + | |
- | + | ||
- | // Initialize the MAX7219 | + | |
- | lc.shutdown(0, false); // Wake up displays | + | |
- | lc.setIntensity(0, 8); // Set intensity level (0 is min, 15 is max) | + | |
- | lc.clearDisplay(0); // Clear display register | + | |
- | + | ||
- | // Set all LED pins to OUTPUT mode, excluding pin 9 and 8 | + | |
- | for (int ledPin : ledPins) { | + | |
- | pinMode(ledPin, OUTPUT); | + | |
- | } | + | |
- | + | ||
- | // Set button pin to INPUT_PULLUP mode | + | |
- | pinMode(buttonPin, INPUT_PULLUP); | + | |
- | + | ||
- | // Set buzzer pin to OUTPUT mode | + | |
- | pinMode(buzzerPin, OUTPUT); | + | |
- | } | + | |
- | + | ||
- | void loop() { | + | |
- | sensorValue = analogRead(DA); | + | |
- | Serial.print("Analog: "); | + | |
- | Serial.print(sensorValue); // Print the analog value | + | |
- | Serial.print(" "); | + | |
- | Serial.print("Digital: "); | + | |
- | Serial.println(digitalRead(DO)); // Print the digital value | + | |
- | + | ||
- | if (sensorValue >= threshold) { // Compare analog value with threshold | + | |
- | setAllLeds(HIGH); | + | |
- | } else { | + | |
- | setAllLeds(LOW); | + | |
- | } | + | |
- | + | ||
- | if (sensorValue >= thresholdMatrix) { | + | |
- | lightUpMatrix(); | + | |
- | } else { | + | |
- | lc.clearDisplay(0); | + | |
- | } | + | |
- | + | ||
- | // Check if the button is pressed | + | |
- | bool currentButtonState = digitalRead(buttonPin); | + | |
- | if (currentButtonState == LOW && buttonState == HIGH) { // Button press detected | + | |
- | Serial.println("Button pressed!"); | + | |
- | playMelody(); // Play the melody | + | |
- | } | + | |
- | buttonState = currentButtonState; // Update button state | + | |
- | } | + | |
- | + | ||
- | void setAllLeds(int state) { | + | |
- | for (int ledPin : ledPins) { | + | |
- | digitalWrite(ledPin, state); | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | void lightUpMatrix() { | + | |
- | for (int i = 0; i < 8; i++) { | + | |
- | lc.setRow(0, i, B11111111); // Light up all LEDs in the row | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | void playMelody() { | + | |
- | for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) { | + | |
- | int duration = 1000 / durations[i]; | + | |
- | tone(buzzerPin, melody[i], duration); | + | |
- | delay(duration * 1.3); // Pause between notes | + | |
- | noTone(buzzerPin); | + | |
- | } | + | |
- | } | + | |
În funcția loop, valoarea senzorului este citită folosind analogRead(), iar valoarea este afișată pe monitorul serial. Dacă valoarea senzorului depășește pragul predefinit, toate LED-urile sunt aprinse, iar matricea MAX7219 afișează un model. Dacă valoarea este sub prag, LED-urile și matricea sunt stinse. | În funcția loop, valoarea senzorului este citită folosind analogRead(), iar valoarea este afișată pe monitorul serial. Dacă valoarea senzorului depășește pragul predefinit, toate LED-urile sunt aprinse, iar matricea MAX7219 afișează un model. Dacă valoarea este sub prag, LED-urile și matricea sunt stinse. |