Function Freezes Code??? :o

Hello. I recently got the Tea5767 radio module, along with the library. I was originally going to have it where if you press a button it goes up a station frequency by 0.1, and another button for down. Then I realized that would take forever to tune in to a station. I am trying to make an auto scan feature, that moves up the frequency by 0.1 automatically. How could I program that? Thanks in advance! :) :) :)

Oh, and by the way here is the code I have currently:

#include <TEA5767Radio.h>

#include <Wire.h>
TEA5767Radio radio = TEA5767Radio();

int y = 0;
int x = 0;
int fr = 88.2;
int up = 1;

void setup() {
Serial.begin(9600);

}

void loop() {
x = fr + up;
  delay(5);
  fr = x;
y = up + 1 ;
  delay(5);
  y = up;
  delay(5);
  radio.setFrequency(fr);
  Serial.print("FR= ");
  Serial.print(fr);
     delay(4000);
}

Aha! I have tested using this code:

#include <TEA5767Radio.h>
#include <Wire.h>

TEA5767Radio radio = TEA5767Radio();

int y = 0;
int x = 0;
long fr = 88.2;
int up = 1;

void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print("Hi");


}

void loop() {
  Serial.print("FR= ");
  Serial.print(fr);
    radio.setFrequency(fr);
      x = fr + up;
  delay(5);
  fr = x;
     delay(10);
}

When I remove radio.setFrequency(fr); the code works as normal. However, when I keep it in the code, it is almost as if the “loop” does not loop. ‘fr’ is printed ONCE on the serial monitor, and does not repeat… However, that one line of code is crucial. Why is it stopping my entire loop function?

Hello. I have the TEA5767 library. Whenever I call this function

radio.setFrequency()

My entire sketch seems to stop in its tracks. For instance, if I print "1" to the Serial monitor, then have the function, then print "2," I will get 1 on the Serial Monitor but not 2, or anything beyond 2!

Can someone tell me WHY this is happening??? I really need your help!!! And if you can, I need it quickly!

stupid-questions: Can someone tell me WHY this is happening??? I really need your help!!! And if you can, I need it quickly!

There's nothing that can be told just from one line of code. In order to find the problem one would need to see lots more about the context that line is used in. Post the whole code or there's not much anyone will be able to do.

PLEASE: Use code tags when you post it. And if the formatting isn't neat, run an auto-format (Control-T) before you copy it here.

A link to or a copy of the library you’re using would probably also be a good idea to include. Otherwise you’re restricting the number of people who can possibly respond to only those who already know that library.

Do you actually have a radio connected to your arduino? Can you talk to it with any of the other library functions? Most likely cause is improper connection....

Here is my library: http://playground.arduino.cc/Main/TEA5767Radio

Here is my full code:

#include <TEA5767Radio.h>
#include <Wire.h>

TEA5767Radio radio = TEA5767Radio();

int y = 0;
int x = 0;
long fr = 88.2;
int up = 5;

void setup() {
Serial.begin(9600);
Wire.begin();
Serial.print("Hi");
    radio.setFrequency(fr);


}

void loop() {
  Serial.print("FR= ");
  Serial.print(fr);
    radio.setFrequency(fr);
      x = fr + up;
  delay(5);
  fr = x;
     delay(10);
}

Yes, the radio module is plugged in to the Arduino.
Thanks for the help so far! :slight_smile:

long fr = 88.2;
  • ? -

Well, that code definitely needs the auto-format run across it.

I2C can be frozen indefinietly if there's a problem with the data line. Without pullups, the Arduino will wait "forever" for an acknowledgement from the slave, which never comes.

  1. Which Arduino?
  2. Which pins on that Arduino are you using for I2C?
  3. Which radio module? It may not have pullups.

MorganS: 1. Which Arduino? 2. Which pins on that Arduino are you using for I2C? 3. Which radio module? It may not have pullups.

  1. Arduino Uno
  2. A4 and A5 for SDA and SCL, (Or maybe the other way around, I don't have the board in front of me) 5V to VCC, GND to GND.
  3. TEA5767 full Geekfun Module: https://www.amazon.com/gp/product/B014QZM53Y/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1

What kind of Pullups do you mean? What do you mean by 'auto-format?'

Thanks!

Auto-format is on the Tools menu in the Arduino IDE. If it looks like it scrambled your code, then your code is wrong. It's a very good formatter (older ones could scramble correct code.)

stupid-questions: What kind of Pullups do you mean?

I2C requires that the lines be pulled HIGH with a resistor since the outputs involved can only make the pin LOW. Read up any good I2C tutorial and you will see what is meant here.

stupid-questions: What do you mean by 'auto-format?'

Press Control-T in your IDE and it will fix all the indenting so the code ends up in nice indented blocks instead of wondering all over the page. It makes the code MUCH easier to read.

@stupid-questions, do not cross-post. Threads merged.

[quote author=Coding Badly link=msg=2859216 date=1469674554] @stupid-questions, do not cross-post. Threads merged. [/quote]

Oh... Wow... Sorry! I just thought this was a seperate-ish error from the other more general post, and deserved its own thread. Will not happen again! ;)

Ok. I did the Auto Format on my code. Here is the new code:

#include <TEA5767Radio.h>
#include <Wire.h>

TEA5767Radio radio = TEA5767Radio();

int y = 0;
int x = 0;
long fr = 88.2;
int up = 5;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Serial.print("Hi");
  radio.setFrequency(fr);


}

void loop() {
  Serial.print("FR= ");
  Serial.print(fr);
  radio.setFrequency(fr);
  x = fr + up;
  delay(5);
  fr = x;
  delay(10);
}

If I run this code, is it more likely to work?? Does Auto Format perfect the functionality of the code? If not, what do I do next to make this code NOT freeze up?

That makes the code more readable, thanks.

I believe the problem may be in the connections. Specifically SDA. Does it have a pullup resistor anywhere in the system? That is, a resistor of about 4.7K (double or half this value is OK) connecting SDA to +5V. Also add an equivalent resistor to SCL.

That Amazon link doesn't show a schematic. Looking at the pictures of the PCB, it doesn't appear to have pullups.

stupid-questions: If I run this code, is it more likely to work?? Does Auto Format perfect the functionality of the code?

It just makes it so that others can more easily read the code which in turn makes it much easier for them to try to help you out.

MorganS:
I believe the problem may be in the connections.

Please everybody note. The radio IS working. This code works:

// TEA5767 Example

#include <Wire.h>
#include <TEA5767Radio.h>

TEA5767Radio radio = TEA5767Radio();

void setup()
{ 
  Wire.begin();
  radio.setFrequency(93.0); // pick your own frequency
  Serial.print("Done");
}

void loop()
{
}

The radio easily and quickly tunes into the station, as long as there is something broadcasted on it. HOWEVER. “Done” is not printed on the Serial Monitor. The radio keeps playing and playing, and “Done” is nowhere to be found on the monitor.

At least that is what I THINK stupid-questions is trying to say… Right?

WesleyRTech:
Please everybody note. The radio IS working. This code works:

// TEA5767 Example

#include <Wire.h>
#include <TEA5767Radio.h>

TEA5767Radio radio = TEA5767Radio();

void setup()
{
  Wire.begin();
  radio.setFrequency(93.0); // pick your own frequency
  Serial.print(“Done”);
}

void loop()
{
}




The radio easily and quickly tunes into the station, as long as there is something broadcasted on it. HOWEVER. "Done" is not printed on the Serial Monitor. The radio keeps playing and playing, and "Done" is nowhere to be found on the monitor.

At least that is what I THINK stupid-questions is trying to say... Right?

Wow! You read my mind WesleyRTech, that code does work! I thought that was clear, but I suppose not. So yeah, THAT code works, its just that DONE isn’t printed on the Serial Monitor.