Storing radio presets

Greetings, Forum members!

I am still a newbie, but I'm making great progress and loving the Arduino with each victory! :slight_smile:

My latest victory was discovering a Library on GitHub that allows my Arduino Pro Mini to communicate with a TEA5757 chip-based radio! It uses an LCD to show the frequency, band (AM/FM) , "Stereo" or "Mono" and a signal strength meter. :slight_smile:

I presently have the start-up frequency and band hard-coded into the sketch.

From there, I can tune up or down with 2 buttons, and a 3'rd changes the band for AM/FM. Because the start frequency & band is hard-coded, every time power is applied, the radio tunes in to that particular station. I want it to store the selected frequency and band into memory, so that the next time the radio is powered-up, it "remembers" that last station and goes there at power-up.

I ALSO want to add the ability to STORE a number of preset stations. I couldn't find anything related to this by searching the Forum. (It's very possible I didn't use the right terms) so if anyone here knows of a thread where that is discussed, please share a link. :slight_smile:

Here is where I found this library (THE ONLY ONE on the WWW!)

I took his "example" sketch and modified it as described above, using DIGITAL INPUTS for the buttons. :slight_smile:

Thanks in advance! You folks here are awesome! :slight_smile:

Willie...

Have a look at this link and read about storing data in EEPROM. You can write your values to the Memory, and read it back after a restart.

Thanks for the suggestion! :slight_smile:

Hmmm... another library to include and have to learn how to use. :wink:

I see the examples there... I guess "Trial & Error" are going to be in my future, unless someone has already come up with a nifty way to store, say, a 16 word array (8 AM and 8 FM frequencies) The values range from 875~1080 for FM and 52~172 for AM. (Also storing the current frequency once, so it will be ready for power-on recall.)

Here is my sketch, as it exists right now:

/*

Simple AM/FM radio with Philips TEA5757 Radio-on-chip IC

This sketch should also work with most TEA5757-based audio systems 
tuner modules (found in old Philips sound systems).

This code was modded by William Barnett 10/28/16 

TEA5757 to Arduino connections:
Arduino              TEA5757
A5                  MO/ST (24)
A4                  Clock (27)
A3                  Data (28)
A2                  WR_EN (29)
GND                 GND

Libraries found on GitHub "github.com/RodLophus/TEA5757" 
*/

#include <inttypes.h>
#include <LiquidCrystal.h>
#include <TEA5757.h>

// This example uses an analog Signal Strength input tied to A0 (NEW feature!)
// SCAN is currently not being used, only UP/DOWN and BAND
// Displaying Stereo/Mono is supported
#define Signal_PIN A0 // Analog signal meter input from radio

// Control keys (SCAN not implemented)
#define KEY_BAND      5
#define KEY_SCAN_DOWN 4
#define KEY_DOWN      3
#define KEY_UP        2
#define KEY_SCAN_UP   1
#define KEY_NONE      0

#define TNR_ERROR          2
#define TNR_POOR_RECEPTION 1
#define TNR_NORMAL         0

// Initial frequencies (Eventually to be stored when tuned, then recalled
// from NVRAM at power-up)
uint16_t FMFrequency = 1073;  // kHz / 100 (Hard-coded initial FM freq. is 107.3 Mhz)
uint16_t AMFrequency = 130;   // kHZ / 10  (Hard-coded initial AM freq. is 1300 Khz)
uint16_t frequency = FMFrequency; // (Hard-coded BAND is FM)

uint8_t tunerStatus = TNR_NORMAL;

// Initial band
uint8_t band = TEA5757_BAND_FM; // (Hard-coded power-on BAND is FM)
// USEAGE 2-line ctrl: LiquidCrystal lcd(rs, enable, d4, d5, d6, d7) Tie R/W LOW
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

TEA5757 Radio(A5, A4, A3, A2);  // MO/ST, Clock, Data, WR_EN

/*******************************************************\
 *                       getKey()                      *
 *                                                     *
\*******************************************************/
uint8_t getKey(uint8_t SignalPin) {
  uint16_t Signal;
  uint8_t key = KEY_NONE; // (Defaults to no key being pressed)
  
Signal = (analogRead(SignalPin)/10)-3; //Measure the analog signal strength & convert (0~8)
if (Signal > 10) Signal = 0; //Negative "underflow" means signal is too low to receive.
            lcd.print(Signal); //Display the "signal number" (0~8) on the screen (NEW FEATURE)

if (digitalRead(2)==LOW) key=KEY_DOWN;  //Manual tune DOWN
if (digitalRead(3)==LOW) key=KEY_UP;    //Manual tune UP
if (digitalRead(10)==LOW) key=KEY_BAND; //Switch AM/FM
  
  //
  // key = KEY_SCAN_DOWN;  (Not implemented yet)
  // key = KEY_SCAN_UP;    (Not implemented yet)
  
  return key;
}

/*******************\
 * Arduino setup() *
\*******************/
void setup() {

pinMode(2,INPUT_PULLUP);  //Tune DOWN button to gnd
pinMode(3,INPUT_PULLUP);  //Tune UP button to gnd
pinMode(10,INPUT_PULLUP); //BAND button to gnd

// (Eventually want to add STORE/RECALL of PRESETS)

  lcd.begin(40, 2); // 40 char, 2-line display is what I have, but a 16x2 is fine

  Radio.init();

  Radio.preset(frequency, band);
}

/******************\
 * Arduino loop() *
\******************/
void loop() {
  uint16_t previousFrequency;
  uint8_t key = getKey(Signal_PIN); //Leftover form original, not modded
  uint8_t searchDirection = 0;
  int8_t delta = 0;
  uint8_t i;

  lcd.setCursor(0, 0);
  
  // Frequency
  switch(band) {
    case TEA5757_BAND_FM:
      lcd.print("  FM ");
      if(frequency < 1000) lcd.print(' ');
      lcd.print(frequency / 10);
      lcd.print('.');
      lcd.print(frequency % 10);
      lcd.print(" MHz  ");
      lcd.print("Signal"); //NEW FEATURE

      break;
    case TEA5757_BAND_AM:
      lcd.print("  AM ");
      if(frequency < 100) lcd.print(' ');
      lcd.print(frequency * 10);
      lcd.print(" kHz   ");
      break;
  }
  
  // Status / Stereo
  lcd.setCursor(0, 1);
  switch(tunerStatus) {
    case TNR_POOR_RECEPTION:
      lcd.print("Poor Reception!!");
      break;
    case TNR_ERROR:
      lcd.print("  Tuner Error!  ");
      break;
    default:
      if(band == TEA5757_BAND_AM)
        lcd.print("                "); //Erases the Stereo/Mono indication
      else
        if(Radio.isStereo())
          lcd.print("    [STEREO]    ");
        else
          lcd.print("    [ MONO ]    "); //Modded from original (was blank space)
  }

  if(key != KEY_NONE) { // Some key has been pressed (SCAN not implemented)
    delay(200);
    switch(key) {
      case KEY_UP:        searchDirection = TEA5757_SEARCH_NONE; delta = +1; break;
      case KEY_DOWN:      searchDirection = TEA5757_SEARCH_NONE; delta = -1; break;
      case KEY_SCAN_UP:   searchDirection = TEA5757_SEARCH_UP;   delta = +1; break;
      case KEY_SCAN_DOWN: searchDirection = TEA5757_SEARCH_DOWN; delta = -1; break;
      case KEY_BAND:
        switch(band) {
          case TEA5757_BAND_FM:
            FMFrequency = frequency;
            frequency = AMFrequency;
            band = TEA5757_BAND_AM;
            break;
          case TEA5757_BAND_AM:
            AMFrequency = frequency;
            frequency = FMFrequency;
            band = TEA5757_BAND_FM;
            break;
        }
        Radio.preset(frequency, band);
        return;
    }
    
    if(searchDirection == TEA5757_SEARCH_NONE) { // No search mode = manual tuning
      // There is no way to detect "Poor Reception" in manual tuning mode with TEA5757
      tunerStatus = TNR_NORMAL;
      frequency += delta;
      switch(band) {
        case TEA5757_BAND_FM:
          if(frequency > 1080) frequency = 875; //Band limits of radio 87.5~108.0 Mhz
          if(frequency < 875)  frequency = 1080;
          break;
        case TEA5757_BAND_AM:
          if(frequency > 172) frequency = 52; //Band limits of radio 520~1720 Khz
          if(frequency < 52)  frequency = 172;
          break;
      }  
      Radio.preset(frequency, band);

    } else { // Automatic tuning (search)

      previousFrequency = frequency;
      // Keep reading the TEA5757 register until get a non-zero frequency.
      // According to the datasheet, the register MSB should be "0" when the
      // TEA5757 is tuned, but apparently it is always "1"
      search:
      Radio.search(band, TEA5757_LVL_4, searchDirection);
      for(i = 0; i < 50; i++) {
        delay(100);
        frequency = Radio.getPLLFrequency();
        if(frequency) { // Tuned!
          i = 50; // Will exit the loop
          switch(band) {
            // Convert PLL frequency to "actual" frequency
            case TEA5757_BAND_FM: frequency = (frequency / 8) - 107; break;
            case TEA5757_BAND_AM: frequency = (frequency / 10) - 45; break;
          }
        }
      }
      
      if(frequency > 2000) {
        // Tuner error (not connected?): quit searching
        tunerStatus = TNR_ERROR;
        frequency = previousFrequency;
        return;
      }

      if(frequency == 0) {
        // No station found (antenna problem?): quit searching
        tunerStatus = TNR_POOR_RECEPTION;
        frequency = previousFrequency;
        Radio.preset(frequency, band);
        return;
      }
     
      if(frequency == previousFrequency) {
        // Issue a pre-step and try again
        frequency += delta;
        Radio.preset(frequency, band);
        delay(100);
        goto search;
      }

      tunerStatus = TNR_NORMAL;

      switch(band) {
        case TEA5757_BAND_FM:
          if((frequency > 1080) || (frequency < 875)) {
            // Got out of bounds or found no station: go to the other end of the scale
             if(searchDirection == TEA5757_SEARCH_UP)
                Radio.preset(875, TEA5757_BAND_FM);
              else
                Radio.preset(1080, TEA5757_BAND_FM);
              delay(100);
              goto search;
          }
          break;
        case TEA5757_BAND_AM:
          if((frequency > 172) || (frequency < 52)) {
            if(searchDirection == TEA5757_SEARCH_UP)
              Radio.preset(52, TEA5757_BAND_AM);
            else
              Radio.preset(172, TEA5757_BAND_AM);
            delay(100);
            goto search;
          }
          break;
      } // switch(band)

    } // else (automatic tunning)
  } // if (key pressed)
}

Willie...

unless someone has already come up with a nifty way to store, say, a 16 word array

http://playground.arduino.cc/Code/EEPROMWriteAnything