Serial.println produces square instead of numbers

Hello community,

I am new to arduino. Before using arduino I used R during my studies,
So I am not completly new to writing code/ syntax.

I am trying my first serious project. I want to analyze sound frequencies
from an analog input in order to check if a tone is being sung correctly.
Like some kind of singing-totur-box.

Therefore I am using fix_fft.h like everybody else. However, in the process
of reading out the frequency values I ran into problems regarding the
Serial.prinln() function. I wrote a simpel test code, to recreate and isolate
the error.

I have no problem printing the val-variable, which gives me a value around 24.
However, when I save the val-variable in the data[]-array, and try to print it
with Serial.println(), I only get symbols (],,⸮,#,...) instead of numbers.
The symbols change as I blow in to the microphone
to create noise.

I am pretty sure it's an easy beginner misunderstanding of the programming
environment. However I wasn't able to fix the problem.

I am using an arduino UNO and Windows 10.

Hope you can help me.

char im[128], data[128];                              //variables for the FFT
int i = 0, val;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (i = 0; i < 128; i++) {
   val = analogRead(A0);
   //Serial.println(val);
   data[i]= val;
   Serial.println(data[i]);
  }
}
1 Like

The Arduino is assuming you wish to print characters, rather than a number.

Declare the data array to be type "int" rather than "char" so that it can store the values from analogRead() properly, and your program should work as expected.

Therefore I am using fix_fft.h like everybody else

Well, some people use it. For AVR-based Arduinos, I recommend the OpenMusicLabs FFT, or FHT. It is much better.

What data type analogRead() return ?
What data type is the array in which you are storing them ?

Ok, I can answer my question myself.

The type char saves numbers as characters according
to the ascii table. I assume that I get a square, when
the number is not represented in the ascii chart.
For exampe my value 24.

You learn quickly Mr Schuler :wink:

I am sorry! I didn't want to be ungreatful.
I didn't see your replies since i was just
watching "your posts"

Thanks to everyone.

I think the analogue read function output
is just a number? And the array is a character.

However I i am wondering. What would make
the original author of the code (eg. here Project#20 OLED Spectrum Analyzer using Fixed-point FFT; FHT on free-running mode - myscratchbooks)
choose a char array. I will dig into it :smiley:

The type char saves numbers as characters according to the ascii table.

No.

char x=1; //stores the binary value 1 in a variable of type char, named x
char x='1'; //stores the ASCII value representing the digit 1

The analog read value is a binary number between 0 and 1023 (for 10 bit ADCs). You can store that range of values in a variable declared integer, but values greater than 255 do not fit into a variable declared char.

You sometimes see a square appear on the serial monitor when you use Serial.print(), because Serial.print() assumes that a char variable contains an ASCII character.

To see the numerical representation of a char variable, use the following.

 Serial.print(x,BIN); //for binary representation, HEX for hexadecimal, DEC for decimal
2 Likes

Thanks! That helps me a lot!

@jremington

The original code converts the outpul value using
this equation (x/4)-128. It makes the 10 Bit output
fit into the 8 bit variable,

I mean it was a good Idea for learning.
It made the code simpler.

But I understand that my version of the code wasn't
able to save all the values. Values > 128 were gone
missing. I am not exactly sure what arduino does to
a number that cannot be stored. Is it just being ignored?

But I will have to use the conversion from now on :slight_smile:

It is the programmer's responsibility to understand the limitations of variable types and deal correctly with them when storing numbers.

You MUST be aware of the range of valid values associated with each data type, and write your program accordingly. If you don't, the program won't work as you expect.

You can have an unsigned char variable, which stores values 0 to 255, or a signed char variable, which stores values -128 to 127.

jremington:
The analog read value is a binary number between 0 and 1023 (for 10 bit ADCs). You can store that range of values in a variable declared int, but values greater than 255 do not fit into a variable declared char.

char is an integer datatype.

www.asciitable.com to see where the weird characters are from.