//#include <TEA5767.h> // include Si4703_Breakout library to control FM Tuner
#include <Wire.h> // include Wire library to use I2C to communicate with FM Tuner
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
// initialize the LedControl library with the numbers of the interface pins with 4-digit 7-segment display
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW //FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 13 // or SCK
#define DATA_PIN 11 // or MOSI
#define CS_PIN 10 // or SS
// initialize the Si4703_Breakout library with the numbers of the interface pins with FM Tuner
//TEA5767 Radio;
//define Arduino pins connected to encoder switch
int encoder_PB = 4; // Push Button
int encoder_A = 2; // Phase A
int encoder_B = 3; // Phase B
int encoder_A_Last = HIGH; // last phase A state
int state_A; // current phase A state
int state_PB; // current Push Button state
float channel_min = 87.25; // Min. FM channel number = 87.5 MHz
float channel_max = 107.75; // Max. FM channel number = 108.0 MHz
float channel; // used to store current FM channel number
//favorite statios - total 22 channels
float station[7]= {87.00, 87.50, 97.50, 98.00, 101.00, 105.00, 108.00};
int list = 3; // power on channel = [list+1] = 18th channel = 104.1
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
void setup()
{
Serial.begin(9600);
Wire.begin();
pinMode (encoder_PB,INPUT); // define pin mode for encoder switch
pinMode (encoder_A,INPUT);
pinMode (encoder_B,INPUT);
myDisplay.begin();
myDisplay.setIntensity(3);
myDisplay.displayClear();
// Radio.init();
channel = station[list]; // set current channel number = (list + 1) channel
// Radio.set_frequency(channel);
// initialize 4-digit 7-segment display
displayFM(channel); // show current channel number
delay(2000);
}
void loop()
{
state_A = digitalRead(encoder_A); //reading encoder phase A state
state_PB = digitalRead(encoder_PB); //reading encoder push button state
if ((encoder_A_Last == LOW) && (state_A == HIGH)) // if phase A transit from LOW to HIGH
{
if (digitalRead(encoder_B) == LOW) // ** if phase B = LOW ==> clockwise rotation**
{
if(channel > channel_max) // if channel higher than max channel number
channel = channel_min; // starts over from min channel number
else // otherwise, increase 0.2 channel number (200KHz)
channel = channel + 0.25;
//Radio.set_frequency(channel);
displayFM(channel); // display current channel number
}
else // **if phase B = HIGH ==> counter-clockwise rotation**
{
if(channel < channel_min) // if channel lower than min channel number
channel = channel_max; // starts over from max channel number
else // otherwise, decrease 0.2 channel number (200KHz)
channel = channel - 0.25;
//Radio.set_frequency(channel);
displayFM(channel); // display current channel number
}
Serial.println(channel);
}
else if (state_PB == HIGH) // if detecting Push Button being pressed
{
list++; // select next favorite station
if(list == 7) // if favorite statio reaches max number of favorite station
list = 0; // return to first list of favorite station numbe
channel = station[list]; // set current favorite channel list number to current channel
//Radio.set_frequency(channel);
displayFM(channel); // display current channel number
delay(200);
Serial.println(channel);
}
encoder_A_Last = state_A; // update last Phase A state form current Phase A state
}
void displayFM(float number) // subroutine for display current channel number
{
/*
int num1,num2,num3,num4,num5;
num1=number/10000; // digit for thousands
num2=(number%10000)/1000; // digit for hundreds
num3=(number%1000)/100; // digit for tens
num4=(number%100)/10; // digit for units
num5=number%10; // digit for unit
*/
myDisplay.setTextAlignment(PA_RIGHT);
myDisplay.print(String(number));
}