#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <RDA5807M.h>
#include <Arduino.h>
#include <Ds1302.h>
#include <LowPower.h>
#include <SPI.h>
#define OLED_RESET -1
#define FIX_BAND RADIO_BAND_FM
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display heightint setButton = 9;
Ds1302 rtc(8,6,7); // RST , CLK , DAT
int dsplyButton = 11;
int setButton = 9;
int backButton = 10;
int potPin = A0;
float displayStation;
int stationNum, val;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RDA5807M radio;
// Select Your own radio frequencies for your area
int stations[10] = {9110,9370,9580,9670,9740,9890,10040,10540,10590,10760};
const static char* WeekDays[] =
{
"Monday ",
"Tuesday ",
"Wednesday ",
"Thursday ",
"Friday ",
"Saturday ",
"Sunday "
};
const static char* MonthText[] =
{
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec"
};
void setup() {
rtc.init();
pinMode(dsplyButton,INPUT_PULLUP);
pinMode(setButton,INPUT_PULLUP);
pinMode(backButton, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
radio.init();
radio.setBandFrequency(FIX_BAND, 9110);
radio.setVolume(8);
radio.setMono(false);
radio.setMute(false);
display.setTextSize(3);
display.setTextColor(WHITE);
display.setCursor(20,10);
display.print("91.10");
display.display();
}
void loop() {
radio.setVolume(8);
Ds1302::DateTime now;
rtc.getDateTime(&now);
static uint8_t last_second = 0;
if (last_second != now.second)
{
last_second = now.second;
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(40,0);
if (now.hour <= 9) { //If Hour is single figures, put a 0 in front
display.print("0");
}
display.print(now.hour);
display.print(":");
if (now.minute <= 9) { //If Minute is single figures, put a 0 in front
display.print("0");
}
display.print(now.minute);
display.print(":");
if (now.second <= 9) { //If Seconds is single figures, put a 0 in front
display.print("0");
}
display.print(now.second);
display.display();
display.setTextColor(SSD1306_BLACK);
display.setCursor(40,0);
if (now.hour <= 9) {
display.print("0");
}
display.print(now.hour);
display.print(":");
if (now.minute <= 9) {
display.print("0");
}
display.print(now.minute);
display.print(":");
if (now.second <= 9) {
display.print("0");
}
display.print(now.second);
}
if (digitalRead(backButton)== LOW) {
setFrequency();
}
if (digitalRead(dsplyButton) == LOW) {
while (digitalRead(dsplyButton) == LOW) {
}
radio.term();
DplyClock();
}
}
void setFrequency(){
display.setTextColor(SSD1306_WHITE);
// radio.setMute(false);
while(digitalRead(setButton) == HIGH){
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("Select Station");
display.setTextSize(3);
display.setTextColor(WHITE);
display.setCursor(20,10);
val = analogRead(potPin);
stationNum = map(val, 0, 1023, 0, 9);
displayStation = stations[stationNum]/100.0;
display.print(displayStation);
display.display();
display.clearDisplay();
}
delay(500);
radio.setBandFrequency(FIX_BAND, stations[stationNum]);
display.setCursor(20,10);
display.print(displayStation);
display.display();
}
void DplyClock() {
display.clearDisplay();
while (digitalRead(dsplyButton) == HIGH) {
// get the current time
Ds1302::DateTime now;
rtc.getDateTime(&now);
static uint8_t last_second = 0;
if (last_second != now.second)
{
last_second = now.second;
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(15,2);
if (now.hour <= 9) { //If Hour is single figures, put a 0 in front
display.print("0");
}
display.print(now.hour);
display.print(":");
if (now.minute <= 9) { //If Minute is single figures, put a 0 in front
display.print("0");
}
display.print(now.minute);
display.print(":");
if (now.second <= 9) { //If Seconds is single figures, put a 0 in front
display.print("0");
}
display.print(now.second);
display.setTextSize(1);
display.setCursor(17,22);
display.print(WeekDays[now.dow -1]);
display.print(now.day);
display.print(" ");
display.print(MonthText[now.month -1]);
display.display();
display.setTextColor(SSD1306_BLACK);
display.setCursor(17,22);
display.print(WeekDays[now.dow -1]);
display.print(now.day);
display.print(" ");
display.print(MonthText[now.month -1]);
display.setTextSize(2);
display.setCursor(15,2);
if (now.hour <= 9) {
display.print("0");
}
display.print(now.hour);
display.print(":");
if (now.minute <= 9) {
display.print("0");
}
display.print(now.minute);
display.print(":");
if (now.second <= 9) {
display.print("0");
}
display.print(now.second);
}
LowPower.powerDown(SLEEP_250MS,ADC_OFF,BOD_OFF); // Sleep for 250MS/Sec in Low Power
}
while (digitalRead(dsplyButton) == LOW){
}
display.setTextColor(SSD1306_WHITE);
radio.init();
radio.setBandFrequency(FIX_BAND, stations[stationNum]);
display.clearDisplay();
display.setTextSize(3);
display.setCursor(20,10);
stationNum = map(val, 0, 1023, 0, 9);
displayStation = stations[stationNum]/100.0;
display.print(displayStation);
display.display();
}
Comments