A lot of code here pasted together. I was checking a lot of the forums and even though this may be a simple mistake I could use some help especially since i might add on to this to finish out my project. Basically in sort getting the error message was not declared in this scope init_registers() in the code below. Any help would be greatly appreciated!
RadioTEA5767-v1.ino (4.44 KB)
You have code not in a function. Where does setup() end, for instance ?
Auto Format is your friend. Use it often
You also forgot the "}" at the end of loop() and setFreq().
And I notice you don't initialize freqRead in setFreq().
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Wire.h>
#include <TEA5767Radio.h>
TEA5767Radio radio = TEA5767Radio();
const byte FREQ_DIGITS = 5;
byte FREQUENCY[FREQ_DIGITS];
int freq_pos = 0;
#define FREQ_LOW 87
#define FREQ_INIT 94.9
#define FREQ_HIGH 108
char buf[14];
float curr_freq = 0;
#define PIN_IN_FREQ A1
#define NUM_DATABYTE 5
#define TEA5767_ADR 0x60
byte registers[NUM_DATABYTE];
byte registers_read[NUM_DATABYTE];
struct radio
{
boolean rf;
boolean blf;
unsigned int pll;
float freq;
boolean stereo;
byte lev;
byte ci;
} radioTEA5767;
#define button 3
#define radio 5
int state = 0;
int old = 0;
int buttonPoll = 0;
void setup()
{
{
pinMode(button, INPUT);
pinMode(radio, OUTPUT);
digitalWrite(radio, HIGH);
}
radioTEA5767.freq = FREQ_INIT;
Wire.begin();
Serial.begin(9600);
lcd.begin(20, 4);
lcd.clear();
lcd.setBacklight(HIGH);
lcd.setCursor(7, 0);
lcd.print("Joker's Grin");
init_registers();
lcd.setCursor(7, 1);
lcd.print(printFreq(radioTEA5767.freq));
lcd.setCursor ( 0, 2 );
lcd.print(" 20 cols, 4 rows ");
lcd.setCursor ( 0, 3 );
lcd.print(" www.sunfounder.com ");
void loop()
{
buttonPoll = digitalRead(button);
if (buttonPoll == 1)
{
delay(50);
buttonPoll = digitalRead(button);
if (buttonPoll == 0)
{
state = old + 1;
}
}
else
{
delay(100);
}
switch (state)
{
case 1:
digitalWrite(radio, HIGH);
old = state;
break;
case 2:
digitalWrite(radio, LOW);
old = state;
break;
default:
digitalWrite(radio, LOW);
old = 0;
break;
}
{
read_registers();
Serial.println (printFreq(radioTEA5767.freq));
delay(500);
}
// print frequency
char *printFreq(float frq)
{
int integral_part = (int) frq;
int decimal_part = (frq - integral_part) * 100;
sprintf (buf, "%d.%d FM", integral_part, decimal_part);
return buf;
}
// initialize registers
void init_registers()
{
// set registers 1 && 2 (index 0 and 1)
setPLL(radioTEA5767.freq);
// set register 3 (index 2)
registers[2] = 0xD0; // search up, mid volume, high side injection, stereo, no mute
// set register 4 (index 3)
registers[3] = 0x13; // eu band,clock 32768, stero noise cancelling on, ready flag SWPORT1
// set register 5 (index 4)
registers[4] = 0x00;
write_registers();
delay(10);
}
// read registers
void read_registers()
{
byte c;
Serial.println();
Wire.requestFrom (TEA5767_ADR, NUM_DATABYTE);
for (int i = 0; i < NUM_DATABYTE; i++)
if (Wire.available())
{
c = Wire.read();
//Serial.println(c, HEX);
registers_read[i] = c;
}
}
// write registers
void write_registers()
{
Wire.beginTransmission(TEA5767_ADR);
for (int i = 0; i < NUM_DATABYTE; i++)
{
Wire.write(registers[i]);
}
Wire.endTransmission();
}
float getRF()
{
boolean rf, blf;
rf = false;
blf = false;
if (registers_read[0] & 0x80)
rf = true;
if (registers_read[0] & 0x40)
blf = true;
radioTEA5767.rf = rf;
radioTEA5767.blf = blf;
return rf;
}
unsigned int getPLL()
{
unsigned int pll;
float frq;
radioTEA5767.pll = 0;
pll = registers_read[0] & 0x3F;
pll = pll << 8;
pll = pll + registers_read[1];
radioTEA5767.pll = pll;
frq = ((pll * 32768) / 4) - 225000.;
radioTEA5767.freq = frq / 1000000;
lcd.setCursor(7, 1);
lcd.print(printFreq(radioTEA5767.freq));
return pll;
}
unsigned int setPLL(float frq)
{
unsigned int pll;
radioTEA5767.pll = 0;
pll = (4 * (frq * 1000000 + 225000)) / 32768;
registers[0] = pll >> 8;
registers[1] = pll & 0x00FF;
radioTEA5767.pll = pll;
frq = ((pll * 32768) / 4) - 225000.;
radioTEA5767.freq = frq / 1000000;
lcd.setCursor(7, 1);
lcd.print(printFreq(radioTEA5767.freq));
write_registers();
return pll;
}
void seekDown()
{
registers[0] |= 0x40; // SM = 1
registers[2] &= 0x7F; // SUD = 0
write_registers();
getPLL();
delay(500);
}
void seekUp()
{
registers[0] |= 0x40; // SM = 1
registers[2] |= 0x80; // SUD = 1
write_registers();
getPLL();
delay(500);
}
void setFreq()
{
float freqRead;
const float step = (FREQ_HIGH - FREQ_LOW) / 1024.;
float freqSet = 0;
// read analog input from A1
// output from potientemeter in 0...1023
for (int i = 0; i < 10; i++)
freqRead += analogRead(PIN_IN_FREQ);
freqRead /= 10;
freqSet = FREQ_LOW + (step * freqRead);
if (abs(freqSet - radioTEA5767.freq) > 0.1)
{
setPLL(freqSet);
}