Favorite station code for FM receiver using tea5767, nokia 5110 lcd, arduino uno

Hi, I am building an FM receiver (pictured similarly below) using arduino uno, tea5767, and nokia 5110 lcd. I’m currently using a program I found on instructables that works with all the components I mentioned. I would like the make adjustments to the current code by having my receiver pick up a range of selective radio sections I have chosen (90.5, 94.9, 98.1, 102.5, etc - essentially a list of my favorite radio stations). Currently the program only allows the frequency to increment or decrement by 0.10 at the push of a button. Any suggestions what I can add or change to the current code to make it scan to my favorite stations? Thanks.

#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_PCD8544.h>
#include <SPI.h>

// D7 - Serial clock out (CLK oder SCLK)
// D6 - Serial data out (DIN)
// D5 - Data/Command select (DC oder D/C)
// D4 - LCD chip select (CE oder CS)
// D3 - LCD reset (RST)
Adafruit_PCD8544 lcd = Adafruit_PCD8544(7, 6, 5, 4, 3);

#include <Wire.h>

#define button_frequency_up     13
#define button_frequency_down   12
#define button_mute             11

#define TEA5767_mute_left_right  0x06
#define TEA5767_MUTE_FULL        0x80
#define TEA5767_ADC_LEVEL_MASK   0xF0
#define TEA5767_STEREO_MASK      0x80


int old_frequency=-1;
int frequency=9450;       //CHANGE STATION 94.5 9450

byte old_stereo=0;
byte stereo=1;

byte old_mute=1;
byte mute=0;

byte old_signal_level=1;
byte signal_level=0;

unsigned long last_pressed;

void setup(void) {
  
  pinMode(button_frequency_up, INPUT);
  pinMode(button_frequency_down, INPUT);
  pinMode(button_mute, INPUT);
  
  Wire.begin();
  
  TEA5767_set_frequency();

  lcd.begin();
  lcd.setContrast(60);
  lcd.clearDisplay();
   
  set_text(1,2,"FM Radio",BLACK,1);  
  //set_text(14,147,"blog.simtronyx.de",BLACK,1);
  
}


void loop() {

    if(frequency!=old_frequency){
      set_text(old_frequency>=10000?6:14,17,value_to_string(old_frequency),WHITE,2);
      set_text(frequency>=10000?6:14,17,value_to_string(frequency),BLACK,2);
      old_frequency=frequency;
    }
    
    TEA5767_read_data();
      
    if(old_stereo!=stereo){ 
        set_text(old_stereo?22:28,39,old_stereo?"Stereo":"Mono",WHITE,1);
        set_text(stereo?22:28,39,stereo?"Stereo":"Mono",BLACK,1);
        old_stereo=stereo;
    }
    
    if(old_signal_level!=signal_level){
        set_text(old_signal_level<10?76:70,39,String((int)old_signal_level),WHITE,1);
        set_text(signal_level<10?76:70,39,String((int)signal_level),BLACK,1);
        old_signal_level=signal_level;
        show_signal_level(signal_level);
    }
    
    if(old_mute!=mute){
        set_text(1,39,old_mute?"M":"S",WHITE,1);
        set_text(1,39,mute?"M":"S",BLACK,1);
        old_mute=mute;
    }
      
    delay(50);
    
  


if(digitalRead(button_frequency_down)==HIGH){
    
    frequency=frequency-10;
    if(frequency<8750)frequency=10800;
    TEA5767_set_frequency();
  }
  if(digitalRead(button_frequency_up)==HIGH){
    
    frequency=frequency+10;
    if(frequency>10800)frequency=8750;
    TEA5767_set_frequency();
  }

  if(digitalRead(button_mute)==HIGH){
    
    TEA5767_mute();
  }
  
  delay(50);
  
}

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;

unsigned int frequencyB;

unsigned char TEA5767_buffer[5]={0x00,0x00,0xB0,0x10,0x00};

void TEA5767_write_data(byte data_size){
   
  delay(50);
  
  Wire.beginTransmission(0x60);
  
  for(byte i=0;i<data_size;i++)
    Wire.write(TEA5767_buffer[i]);
  
  Wire.endTransmission();
  
  delay(50);
}

void TEA5767_mute(){ 
  
  if(!mute){   
    mute = 1;   
    TEA5767_buffer[0] |= TEA5767_MUTE_FULL;
    TEA5767_write_data(2);
//    TEA5767_buffer[0] &= ~TEA5767_mute;
//    TEA5767_buffer[2] |= TEA5767_mute_left_right;
  }   
  else{
    mute = 0;   
    TEA5767_buffer[0] &= ~TEA5767_MUTE_FULL;
    TEA5767_write_data(2);
//    TEA5767_buffer[0] |= TEA5767_mute;
//    TEA5767_buffer[2] &= ~TEA5767_mute_left_right;
  }
    
//  TEA5767_write_data(3);
}

void TEA5767_set_frequency()
{
  frequencyB = 4 * (frequency * 10000 + 225000) / 32768;
  TEA5767_buffer[0] = frequencyB >> 8;
  if(mute)TEA5767_buffer[0] |= TEA5767_MUTE_FULL;
  TEA5767_buffer[1] = frequencyB & 0XFF;
  
  TEA5767_write_data(5);
}

int TEA5767_read_data() {
  
  unsigned char buf[5];
  memset (buf, 0, 5);
  
  Wire.requestFrom (0x60, 5); 

  if (Wire.available ()) {
    for (int i = 0; i < 5; i++) {
      buf[i] = Wire.read ();
    }
        
    stereo = (buf[2] & TEA5767_STEREO_MASK)?1:0;
    signal_level = ((buf[3] & TEA5767_ADC_LEVEL_MASK) >> 4);
    
    return 1;
  } 
  else return 0;
}

void show_signal_level(int level){
  
  byte xs=68;
  byte ys=8;
  for(int i=0;i<15;i++){
    if(i%2!=0)lcd.drawLine(xs+i,ys,xs+i,ys-i/2,level>=i?BLACK:WHITE);
  }
}

void set_text(int x,int y,String text,int color,int textsize){
  
  lcd.setTextSize(textsize);
  lcd.setTextColor(color); 
  lcd.setCursor(x,y);     
  lcd.println(text);      
  lcd.display();         
}


String value_to_string(int value){
  
  String value_string = String(value / 100);
  value_string = value_string + '.' + ((value%100<10)?"0":"") + (value % 100);
  return value_string;
}

This section of code reads the up and down buttons and changes the frequency

if(digitalRead(button_frequency_down)==HIGH){
    
    frequency=frequency-10;
    if(frequency<8750)frequency=10800;
    TEA5767_set_frequency();
  }
  if(digitalRead(button_frequency_up)==HIGH){
    
    frequency=frequency+10;
    if(frequency>10800)frequency=8750;
    TEA5767_set_frequency();
  }

Instead of altering the frequency something similar could be used to increment or decrement the index to an array containing the required frequencies. You would need to act when the button becomes pressed rather than when it is pressed.

if(digitalRead(button_frequency_down)==HIGH){
   
    frequency=frequency-10;
    if(frequency<8750)frequency=10800;
    TEA5767_set_frequency();
  }

Here, you set the frequency 10 lower than what it was. You could put your favorite frequencies in an array, and have this code set frequency to the next lower position in the array. The up switch could do the same thing,

I appreciate the feedback. Would you suggest writing the array like this?

if(digitalRead(button_frequency_down)==HIGH){
    
    frequency=frequency [9050, 9490, 9810, 10250];
    if(frequency<8750)frequency=10800;
    TEA5767_set_frequency();
  }
  if(digitalRead(button_frequency_up)==HIGH){
    
    frequency=frequency[10250, 9810, 9490, 9050];
    if(frequency>10800)frequency=8750;
    TEA5767_set_frequency();
  }

I’m not exactly sure what i need to to make the array to read out from a for loop (that is if that’s a better option)

Would you suggest writing the array like this?

No
Declare the array and the index to it as global variables something like this

int frequencies[] = {9050, 9490, 9810, 10250};
numberOfFrequencies = 4
int frequencyIndex = 0;

Then (in pseudo code) read the up/down buttons like this

if upButton becomes pressed
  increment frequencyIndex
  if frequencyIndex = numberOfFrequencies
    frequencyIndex = numberOfFrequencies - 1
  end if
end if
else 
if downButton becomes pressed
  decrement frequencyIndex;
  if frequencyIndex < zero
    frequencyIndex = zero
  end if
end if
frequencyToUse = frequencies[frequencyIndex]

Thanks for your feedback. I tried to translate your pseudo code onto my code.

#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_PCD8544.h>
#include <SPI.h>

// D7 - Serial clock out (CLK oder SCLK)
// D6 - Serial data out (DIN)
// D5 - Data/Command select (DC oder D/C)
// D4 - LCD chip select (CE oder CS)
// D3 - LCD reset (RST)
Adafruit_PCD8544 lcd = Adafruit_PCD8544(7, 6, 5, 4, 3);

#include <Wire.h>

#define button_frequency_up     13
#define button_frequency_down   12
#define button_mute             11

#define TEA5767_mute_left_right  0x06
#define TEA5767_MUTE_FULL        0x80
#define TEA5767_ADC_LEVEL_MASK   0xF0
#define TEA5767_STEREO_MASK      0x80


int old_frequency = -1;
int frequency[] = {9050, 9450, 9490, 9810, 10250};   
int numOfFrequencies = 5;  i got an error if I left it as numOfFrequencies = 5; hence the int numOfFrequencies

int frequencyIndex = 0;
byte old_stereo = 0;
byte stereo = 1;

byte old_mute = 1;
byte mute = 0;

byte old_signal_level = 1;
byte signal_level = 0;

unsigned long last_pressed;

void setup(void) {

  pinMode(button_frequency_up, INPUT);
  pinMode(button_frequency_down, INPUT);
  pinMode(button_mute, INPUT);

  Wire.begin();

  TEA5767_set_frequency();

  lcd.begin();
  lcd.setContrast(60);
  lcd.clearDisplay();

  set_text(1, 2, "FM Radio", BLACK, 1);
  //set_text(14,147,"blog.simtronyx.de",BLACK,1);

}


void loop() {

  if (frequency[5] != old_frequency) {
    set_text(old_frequency >= 10000 ? 6 : 14, 17, value_to_string(old_frequency), WHITE, 2);
    set_text(frequency[5] >= 10000 ? 6 : 14, 17, value_to_string(frequency[5]), BLACK, 2);
    old_frequency = frequency[5];
  }

  TEA5767_read_data();

  if (old_stereo != stereo) {
    set_text(old_stereo ? 22 : 28, 39, old_stereo ? "Stereo" : "Mono", WHITE, 1);
    set_text(stereo ? 22 : 28, 39, stereo ? "Stereo" : "Mono", BLACK, 1);
    old_stereo = stereo;
  }

  if (old_signal_level != signal_level) {
    set_text(old_signal_level < 10 ? 76 : 70, 39, String((int)old_signal_level), WHITE, 1);
    set_text(signal_level < 10 ? 76 : 70, 39, String((int)signal_level), BLACK, 1);
    old_signal_level = signal_level;
    show_signal_level(signal_level);
  }

  if (old_mute != mute) {
    set_text(1, 39, old_mute ? "M" : "S", WHITE, 1);
    set_text(1, 39, mute ? "M" : "S", BLACK, 1);
    old_mute = mute;
  }

  delay(50);


  if (digitalRead(button_frequency_down) == HIGH) {
    frequencyIndex = frequencyIndex + 1;
    if (frequencyIndex = numOfFrequencies)
      frequencyIndex = numOfFrequencies - 1;
    TEA5767_set_frequency();
  }

  else if (digitalRead(button_frequency_down) == HIGH) {
    frequencyIndex = frequencyIndex - 1;
    if (frequencyIndex < 0)
      frequencyIndex = 0;
frequencyIndex = numOfFrequencies - 1;
    TEA5767_set_frequency();
  }



  if (digitalRead(button_mute) == HIGH) {

    TEA5767_mute();
  }

  delay(50);

}

unsigned char frequencyH = 0;
unsigned char frequencyL = 0;

unsigned int frequencyB;

unsigned char TEA5767_buffer[5] = {0x00, 0x00, 0xB0, 0x10, 0x00};

void TEA5767_write_data(byte data_size) {

  delay(50);

  Wire.beginTransmission(0x60);

  for (byte i = 0; i < data_size; i++)
    Wire.write(TEA5767_buffer[i]);

  Wire.endTransmission();

  delay(50);
}

void TEA5767_mute() {

  if (!mute) {
    mute = 1;
    TEA5767_buffer[0] |= TEA5767_MUTE_FULL;
    TEA5767_write_data(2);
    //    TEA5767_buffer[0] &= ~TEA5767_mute;
    //    TEA5767_buffer[2] |= TEA5767_mute_left_right;
  }
  else {
    mute = 0;
    TEA5767_buffer[0] &= ~TEA5767_MUTE_FULL;
    TEA5767_write_data(2);
    //    TEA5767_buffer[0] |= TEA5767_mute;
    //    TEA5767_buffer[2] &= ~TEA5767_mute_left_right;
  }

  //  TEA5767_write_data(3);
}

void TEA5767_set_frequency()
{
  frequencyB = 4 * (frequency * 10000 + 225000) / 32768;
  TEA5767_buffer[0] = frequencyB >> 8;
  if (mute)TEA5767_buffer[0] |= TEA5767_MUTE_FULL;
  TEA5767_buffer[1] = frequencyB & 0XFF;

  TEA5767_write_data(5);
}

int TEA5767_read_data() {

  unsigned char buf[5];
  memset (buf, 0, 5);

  Wire.requestFrom (0x60, 5);

  if (Wire.available ()) {
    for (int i = 0; i < 5; i++) {
      buf[i] = Wire.read ();
    }

    stereo = (buf[2] & TEA5767_STEREO_MASK) ? 1 : 0;
    signal_level = ((buf[3] & TEA5767_ADC_LEVEL_MASK) >> 4);

    return 1;
  }
  else return 0;
}

void show_signal_level(int level) {

  byte xs = 68;
  byte ys = 8;
  for (int i = 0; i < 15; i++) {
    if (i % 2 != 0)lcd.drawLine(xs + i, ys, xs + i, ys - i / 2, level >= i ? BLACK : WHITE);
  }
}

void set_text(int x, int y, String text, int color, int textsize) {

  lcd.setTextSize(textsize);
  lcd.setTextColor(color);
  lcd.setCursor(x, y);
  lcd.println(text);
  lcd.display();
}


String value_to_string(int value) {

  String value_string = String(value / 100);
  value_string = value_string + '.' + ((value % 100 < 10) ? "0" : "") + (value % 100);
  return value_string;
}

Now I am getting an error “invalid operands of types ‘const int [5]’ and ‘int’ to binary ‘operator*’” under this section:

void TEA5767_set_frequency()
{
  frequencyB = 4 * (frequency * 10000 + 225000) / 32768;
  TEA5767_buffer[0] = frequencyB >> 8;
  if (mute)TEA5767_buffer[0] |= TEA5767_MUTE_FULL;
  TEA5767_buffer[1] = frequencyB & 0XFF;

  TEA5767_write_data(5);
}

I tried playing around the code in this section but got additional errors.

    if (frequencyIndex = numOfFrequencies)How many equals signs are used when comparing values ?

Although you increment/decrement frequencyIndex you never actually use it to get the actual frequency from the array. There are, however, several places in the code where you use frequency[5] but the array only has levels 0 to 4 so all bets are off as to what will happen.

You have also ignored the need to act when the buttons [u]become[/u] pressed rather than when the [u]are[/u] pressed.