Created
May 27, 2018 10:00
-
-
Save KunalGautam/ec89304813fbee04ebd503ae5053fc89 to your computer and use it in GitHub Desktop.
Testing RTC and FM all at once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include "RTClib.h" | |
#include <TEA5767N.h> | |
TEA5767N radio = TEA5767N(); | |
RTC_DS3231 rtc; | |
//Variables: | |
int signal_level; | |
double current_freq; | |
int inByte; | |
int flag = 0; | |
String frq; | |
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; | |
void setup () { | |
//Sets the radio station | |
radio.selectFrequency(104.2); | |
Serial.begin(115200); | |
if (! rtc.begin()) { | |
Serial.println("Couldn't find RTC"); | |
while (1); | |
} | |
if (rtc.lostPower()) { | |
Serial.println("RTC lost power, lets set the time!"); | |
// following line sets the RTC to the date & time this sketch was compiled | |
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | |
// This line sets the RTC with an explicit date & time, for example to set | |
// January 21, 2014 at 3am you would call: | |
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); | |
} | |
} | |
void loop () { | |
byte isBandLimitReached = 0; | |
if (Serial.available() > 0) { | |
char firstChar = Serial.read(); | |
inByte = firstChar; | |
if (inByte == '+' || inByte == '-') { //accept only + and - from keyboard | |
flag = 0; | |
} else { | |
frq = Serial.readString(); | |
frq = firstChar + frq; | |
Serial.println(frq); | |
float f1 = atof(frq.c_str()); | |
if (f1 <= 108 && f1 >= 88) { | |
radio.selectFrequency(f1); | |
flag = 0; | |
} | |
} | |
} | |
//If forward button is pressed, go up to next station | |
if (inByte == '+') { | |
radio.setSearchUp(); | |
isBandLimitReached = radio.searchNextMuting(); | |
if (radio.readFrequencyInMHz() >= 108) { | |
Serial.println("Reached to the End, Let me scan from the Start"); | |
radio.selectFrequency(88.0); | |
radio.setSearchUp(); | |
isBandLimitReached = radio.searchNextMuting(); | |
} | |
} | |
//If backward button is pressed, go down to next station | |
if (inByte == '-') { | |
radio.setSearchDown(); | |
isBandLimitReached = radio.searchNextMuting(); | |
if (radio.readFrequencyInMHz() <= 88) { | |
Serial.println("Reached to the Start ,Let me scan from the End"); | |
radio.selectFrequency(108.0); | |
radio.setSearchDown(); | |
isBandLimitReached = radio.searchNextMuting(); | |
} | |
} | |
if (flag == 0) { | |
current_freq = radio.readFrequencyInMHz(); | |
signal_level = radio.getSignalLevel(); | |
Serial.print("Current freq: "); | |
Serial.print(current_freq); | |
Serial.print("MHz( "); | |
Serial.print(radio.isStereo() ? "Stereo" : "Mono"); | |
Serial.println(")"); | |
Serial.print("Signal Strength : "); | |
Serial.print(signal_level); | |
Serial.println("/15"); | |
DateTime now = rtc.now(); | |
Serial.print(now.year(), DEC); | |
Serial.print('/'); | |
Serial.print(now.month(), DEC); | |
Serial.print('/'); | |
Serial.print(now.day(), DEC); | |
Serial.print(" ("); | |
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); | |
Serial.print(") "); | |
Serial.print(now.hour(), DEC); | |
Serial.print(':'); | |
Serial.print(now.minute(), DEC); | |
Serial.print(':'); | |
Serial.print(now.second(), DEC); | |
Serial.println(); | |
Serial.println(); | |
flag = 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment