-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino code
More file actions
84 lines (68 loc) · 2.14 KB
/
Copy pathArduino code
File metadata and controls
84 lines (68 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <Wire.h>
#include <RDA5807.h>
#include <LiquidCrystal_I2C.h>
RDA5807 radio;
// --- LCD Configuration ---
LiquidCrystal_I2C lcd(0x27, 16, 2); // Try 0x27 or 0x3F depending on your module
// --- Button configuration ---
const int buttonPin = 2; // Push button connected to digital pin 2
int lastButtonState = HIGH;
int currentButtonState;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 200; // debounce time in ms
// --- Frequency list (in 100 kHz units) ---
uint16_t frequencies[] = {9350, 9830, 10640, 10140, 9970, 10650};
int freqCount = sizeof(frequencies) / sizeof(frequencies[0]);
int currentFreqIndex = 0;
void setup() {
Serial.begin(115200);
Serial.println("Initializing RDA5807M FM Receiver...");
pinMode(buttonPin, INPUT_PULLUP); // use internal pull-up resistor
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("FM Receiver");
delay(1000);
// Initialize radio
radio.setup();
radio.setVolume(10); // Volume range: 0–15
delay(100);
// Tune to the first frequency
radio.setFrequency(frequencies[currentFreqIndex]);
showFrequency();
Serial.print("Tuned to ");
Serial.print(frequencies[currentFreqIndex] / 100.0);
Serial.println(" MHz");
}
void loop() {
currentButtonState = digitalRead(buttonPin);
// Detect button press (active LOW)
if (currentButtonState == LOW && lastButtonState == HIGH && (millis() - lastDebounceTime) > debounceDelay) {
lastDebounceTime = millis();
// Change to next frequency
currentFreqIndex++;
if (currentFreqIndex >= freqCount) {
currentFreqIndex = 0; // Wrap around
}
// Tune radio
radio.setFrequency(frequencies[currentFreqIndex]);
showFrequency();
Serial.print("Changed to: ");
Serial.print(frequencies[currentFreqIndex] / 100.0);
Serial.println(" MHz");
}
lastButtonState = currentButtonState;
delay(100);
}
// --- Function to display frequency on LCD ---
void showFrequency() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("FM Receiver");
lcd.setCursor(0, 1);
lcd.print("Freq: ");
lcd.print(frequencies[currentFreqIndex] / 100.0);
lcd.print(" MHz");
}