//#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>
#include "Parola_Fonts_data.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
#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 = 8; // 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
//some variables for FM
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, 89.25, 98.00, 101.00, 105.00, 108.00};
int list = 2; // power on channel = [list+1] = 18th channel = 104.1
MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
char tChar[8];
/*
textEffect_t effect[] =
{
PA_RANDOM,
PA_PRINT,
PA_SCAN_HORIZ,
PA_SCROLL_LEFT,
PA_WIPE,
PA_SCAN_VERTX,
PA_SCROLL_UP_LEFT,
PA_SCROLL_UP,
PA_FADE,
PA_OPENING_CURSOR,
PA_GROW_UP,
PA_SCROLL_UP_RIGHT,
PA_BLINDS,
PA_SPRITE,
PA_CLOSING,
PA_GROW_DOWN,
PA_SCAN_VERT,
PA_SCROLL_DOWN_LEFT,
PA_WIPE_CURSOR,
PA_SCAN_HORIZX,
PA_DISSOLVE,
PA_MESH,
PA_OPENING,
PA_CLOSING_CURSOR,
PA_SCROLL_DOWN_RIGHT,
PA_SCROLL_RIGHT,
PA_SLICE,
PA_SCROLL_DOWN,
};
*/
void setup()
{
//Serial.begin(9600);
Wire.begin();
Serial.begin(9600);
pinMode (encoder_PB,INPUT); // define pin mode for encoder switch
pinMode (encoder_A,INPUT);
pinMode (encoder_B,INPUT);
myDisplay.begin();
myDisplay.setFont(fontArabic);
myDisplay.setInvert (false); // true
myDisplay.setIntensity(3);
myDisplay.displayClear();
// initialize FM Tuner - from the setup, the FM radio play FM 104.1 after power up
//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
if (myDisplay.displayAnimate()) {
/*if (i < sizeof(texteffect)) {
i++;
}
else {
i = 0;
}*/
//myDisplay.setZone(0, MAX_DEVICES - 2, MAX_DEVICES - 2);
//myDisplay.displayZoneText(0, tChar[4], PA_CENTER, 20, 1000, PA_SCROLL_UP , PA_SCROLL_UP);
//myDisplay.displayText(tChar[0], PA_CENTER, 20, 0, PA_SCROLL_RIGHT, PA_SCROLL_RIGHT);
//myDisplay.displayReset();
}
}
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));
//myDisplay.print("FM"+String(number));
*/
String text = "" + String(number);
text.toCharArray(tChar, 8);
myDisplay.displayText(tChar, PA_RIGHT, 50, 0, PA_BLINDS, PA_NO_EFFECT); //PA_NO_EFFECT
myDisplay.displayReset();
Serial.println(tChar);
Serial.println(tChar[0]);
Serial.println(tChar[1]);
Serial.println(tChar[2]);
Serial.println(tChar[3]);
Serial.println(tChar[4]);
Serial.println(tChar[5]);
delay(50);
}