Converting double and using charAt()

Hi everyone. I'm trying to make a talking voltmeter where the function converts a double into a string. the string is then read using charAt() method.....

    if(voltage >11.5){
      char convert[10];
      dtostrf(voltage,2,2,convert);
      voltString = String(convert);
      char firstC = voltString.charAt(0);
      voice(firstC); delay(1000);
      Serial.print(firstC);
      char secondC = voltString.charAt(1);
      voice(secondC); delay(1000);
      Serial.print(secondC);
      voice(23); delay(1000);
      Serial.print(".");
      char fourthC = voltString.charAt(3);
      voice(fourthC);delay(1000);
      Serial.println(fourthC);
      voice(24); delay(350);
void voice(int track)

is another function that points the program to audio files which are stored such as 1.wav
The serial monitor prints out just as i expect e.g: 13.9

Now the variable 'firstC' is a char right and i'm trying to pass a char where it is expecting an int:
void voice(int Track)...so will this need converting again?

or is it possible the use char instead of int as a variable initialiser in the void voice function?
can you even use an integer in a char variable?

:frowning:

did you try toInt()?
It should get a string and return an int, but I am pretty sure it should work with char...

thanks for the reply i'll look into it

so i've tried this:

    if(voltage >11.5){
      char convert[10];
      dtostrf(voltage,2,2,convert);
      voltString = String(convert);
      char firstC = voltString.charAt(0);
      int a = firstC.toInt();
      voice(a); delay(1000);
      Serial.print(a);

but it gives this error:

request for member 'toInt' in 'firstC', which is of non-class type 'char'

does this mean toInt does not accept char variables?

Avoid Strings, they cause the Arduino to crash. Use C-strings, or zero terminated character arrays instead.

However, there is no need to use either Strings or C-strings in your application.

A voltmeter works with numerical digits, and those digits can be used to cause a device, or the Arduino itself, to say numbers or words. See this example.

char convert[10];
      dtostrf(voltage,2,2,convert);
      voltString = String(convert);
      char firstC = voltString.charAt(0);
      int a = firstC.toInt();
      voice(a); delay(1000);

If what you really need is a number, why in the world are you converting to a String first? Don't you realize that all the trouble you are going through with charAt is just simple third grade mathematics with a number?

apologies to Delta_G, aradarbel10 and jremington

this done it as pointed out by Delta_G:

if (voltage<=11.5){
int volt = voltage*100;
int firstDigit = (volt/1000)%10;
...

…even third grade maths escapes me!

thank you for your advice.

........even third grade maths escapes me!

You used dtostrf() to convert the number to a string, like "12.5".

The first character is convert[0], which is a '1'. The integer 1 is the character '1' minus the character '0'.

   int tens = convert[0] - '0';

The second character is convert[1], which is a '2'. The integer 2 is the character '2' minus the character '0'.

   int ones = convert[1] - '0';

Or, since it is only the integer portion that you care about,

   int iVoltage = voltage;

   int tens = iVoltage / 10;
   int ones = iVoltage % 10;

with no conversion to a string required, and NO stupid Strings.

..even third grade maths escapes me!

You might want to review that, if you are taking up computing with microcontrollers as a hobby.

aradarbel10:
did you try toInt()?
It should get a string and return an int, but I am pretty sure it should work with char...

toInt() is a member of the String class.
When using strings (so lower case s, aka null terminated char arrays) , you can use the atoi() function or one of its siblings.

Thanks everyone for your input,

I've dropped the whole dtostrf and charAt objects, never needed them!

...just simple math...

@jremington I've reviewed my tagline

''even third grade maths defeats me - but god loves a trier......right''

Arrays next!