#include #include #include #include // TEA5767 I2C address #define TEA5767_ADDR 0x60 // Button pins #define BUTTON_UP 4 #define BUTTON_DOWN 2 #define BUTTON_MINUTE 6 #define BUTTON_TEN_MINUTES 7 // Buzzer pin #define BUZZER_PIN 13 // Frequency variables float frequency = 101.3; // Initial frequency in MHz const float frequencyStep = 0.1; // Step size in MHz const float minFrequency = 87.5; // Minimum FM frequency in MHz const float maxFrequency = 108.0; // Maximum FM frequency in MHz // Timer variables unsigned long timer = 0; // Timer in seconds unsigned long previousMillis = 0; // Last time the timer was updated const unsigned long interval = 1000; // Interval for timer countdown in milliseconds bool timerEnded = true; // Flag to indicate if the timer has ended bool timerStarted = false; // Flag to indicate if the timer has started unsigned long blinkStartMillis = 0; // Time when blinking started const unsigned long blinkDuration = 3000; // Duration of the blinking effect in milliseconds // OLED display settings #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 32 #define OLED_RESET -1 // Reset pin (or -1 if sharing Arduino reset pin) #define OLED_I2C_ADDRESS 0x3C // I2C address for the OLED display Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); Tone buzzer; // Create a Tone object for the buzzer void setup() { Serial.begin(9600); Wire.begin(); // Initialize buttons pinMode(BUTTON_UP, INPUT_PULLUP); pinMode(BUTTON_DOWN, INPUT_PULLUP); pinMode(BUTTON_MINUTE, INPUT_PULLUP); pinMode(BUTTON_TEN_MINUTES, INPUT_PULLUP); // Initialize the TEA5767 setFrequency(frequency); // Initialize the OLED display if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for (;;); // Don't proceed, loop forever } buzzer.begin(BUZZER_PIN); display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.print("Frequency: "); display.print(frequency, 1); display.println(" MHz"); display.setCursor(0, 10); display.print("Alarm: 0 min 0 sec"); display.display(); } void loop() { // Check if the UP button is pressed if (digitalRead(BUTTON_UP) == LOW) { increaseFrequency(); delay(300); // Debounce delay } // Check if the DOWN button is pressed if (digitalRead(BUTTON_DOWN) == LOW) { decreaseFrequency(); delay(300); // Debounce delay } // Check if the MINUTE button is pressed if (digitalRead(BUTTON_MINUTE) == LOW) { increaseTimer(5); // Increase timer by 1 minute delay(300); // Debounce delay } // Check if the TEN MINUTES button is pressed if (digitalRead(BUTTON_TEN_MINUTES) == LOW) { increaseTimer(600); // Increase timer by 10 minutes delay(300); // Debounce delay } // Update the timer unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; if (timer > 0) { timer--; updateDisplay(); } else if (timer == 0 && timerStarted) { timerEnded = false; timerStarted = false; blinkStartMillis = currentMillis; } } if (!timerEnded) { if (currentMillis - blinkStartMillis <= blinkDuration) { // Blink the screen if ((currentMillis / 100) % 2 == 0) { display.clearDisplay(); } else { updateDisplay(); } display.display(); buzzer.play(NOTE_C4); // Play a tone (middle C) } else { // Stop blinking and reset timerEnded flag timerEnded = true; buzzer.stop(); // Stop the tone updateDisplay(); } } } void setFrequency(float frequency) { byte frequencyH = 0; byte frequencyL = 0; int frequencyB; // Calculate PLL word frequencyB = 4 * (frequency * 1000000 + 225000) / 32768; // Extract high and low bytes frequencyH = frequencyB >> 8; frequencyL = frequencyB & 0XFF; // Send the frequency to the TEA5767 Wire.beginTransmission(TEA5767_ADDR); Wire.write(frequencyH); Wire.write(frequencyL); Wire.write(0xB0); // High side injection, PLL ref enable, Mute disable Wire.write(0x10); // Search mode disable, Search stop level 0 (lowest), Soft mute disable Wire.write(0x00); // PLL disable Wire.write(0x00); // Reserved Wire.endTransmission(); Serial.print("Set frequency to: "); Serial.print(frequency); Serial.println(" MHz"); updateDisplay(); } void increaseFrequency() { if (frequency < maxFrequency) { frequency += frequencyStep; setFrequency(frequency); Serial.print("Frequency increased to: "); Serial.print(frequency); Serial.println(" MHz"); } else { Serial.println("Maximum frequency reached"); } } void decreaseFrequency() { if (frequency > minFrequency) { frequency -= frequencyStep; setFrequency(frequency); Serial.print("Frequency decreased to: "); Serial.print(frequency); Serial.println(" MHz"); } else { Serial.println("Minimum frequency reached"); } } void increaseTimer(unsigned long seconds) { timer += seconds; Serial.print("Timer increased by "); Serial.print(seconds / 60); Serial.println(" minutes"); timerStarted = true; updateDisplay(); } void updateDisplay() { unsigned long minutes = timer / 60; unsigned long seconds = timer % 60; display.clearDisplay(); display.setCursor(0, 0); display.print("Frequency: "); display.print(frequency, 1); display.println(" MHz"); display.setCursor(0, 10); display.print("Alarm: "); display.print(minutes); display.print(" min "); display.print(seconds); display.println(" sec"); display.display(); }