Simple Chars to String to Float

This is part of bigger sketch.
Need to input the diameter of the piece of steel (using a 4x4 keypad) in my lathe and then by using the speed of the lathe(determined by another piece of this sketch with interrupt) calculate the cutting speed. Then show if speed is too fast or too slow. later will fit DC motor and sketch will then control the speed of the motor.

For now I am battling to enter the value of the diameter into the sketch. I simplified the sketch to see where I am going wrong. So entering 25.43 as 5 different chars, then combining them, first problem result is "252"

Then I can find by searching a function toInt (nothing to float), it goes orange when I enter it but I still get compile error saying it is "not declared" I usually get this if I have typed in my variable name wrong.

char OneChar0;          //Array where single characters will be stored in
char OneChar1;          //Array where single characters will be stored in
char OneChar2;          //Array where single characters will be stored in
char OneChar3;          //Array where single characters will be stored in
char OneChar4;          //Array where single characters will be stored in

int i = 0;
String JoinTheChars;        //Variable to combine all the chars
float StringToNumber;     //Variable when String is changed to Number

void setup() {
  Serial.begin(9600);
  OneChar0 = '2';
  OneChar1 = '5';
  OneChar2 = '.';
  OneChar3 = '4';
  OneChar4 = '3';
}     //Curley bracket for end of SETUP

void loop() {
  Serial.print(OneChar0);
  Serial.print(OneChar1);
  Serial.print(OneChar2);
  Serial.print(OneChar3);
  Serial.println(OneChar4);
  delay(1000);

  JoinTheChars = (OneChar0 + OneChar1 + OneChar2 + OneChar3 + OneChar4);
  Serial.println(JoinTheChars);  //Records a value of "252"
  StringToNumber = toInt(JoinTheChars); //ERROR 'toInt' was not declared in the scope
  delay(1000);

}

Is this one of those situations where I have to convert to Hex and then add everything and then convert back again???

Capital S Strings are discouraged on Arduino due to causing crashes. The tutorial Serial Input Basics should prove instructive, especially example #5.

I modified your sketch to print the desired value as a string and as a float. There are likely better ways and refinements but this works to show the principle.

char OneChar0;          //Array where single characters will be stored in
char OneChar1;          //Array where single characters will be stored in
char OneChar2;          //Array where single characters will be stored in
char OneChar3;          //Array where single characters will be stored in
char OneChar4;          //Array where single characters will be stored in

char allChars[7]; // added
char* pEnd; // added
float floatChars; // added

int i = 0;
String JoinTheChars;        //Variable to combine all the chars
float StringToNumber;     //Variable when String is changed to Number

void setup() {
  Serial.begin(9600);
  OneChar0 = '2';
  OneChar1 = '5';
  OneChar2 = '.';
  OneChar3 = '4';
  OneChar4 = '3';

  allChars[0] = OneChar0;
  allChars[1] = OneChar1;
  allChars[2] = OneChar2;
  allChars[3] = OneChar3;
  allChars[4] = OneChar4;
  allChars[5] = '\0'; // add a terminating null and now allChars has a legitimate C string
}     //Curley bracket for end of SETUP

void loop() {
  Serial.print("One at a time - ");
  Serial.print(OneChar0);
  Serial.print(OneChar1);
  Serial.print(OneChar2);
  Serial.print(OneChar3);
  Serial.println(OneChar4);
  // added conversions
  Serial.print("allChars (string) - "); // added
  Serial.println(allChars); // added
  floatChars = atof(allChars); // added
  Serial.print("floatChars - "); // added
  Serial.println(floatChars); // added
  delay(1000);

  JoinTheChars = (OneChar0 + OneChar1 + OneChar2 + OneChar3 + OneChar4);
  Serial.println(JoinTheChars);  //Records a value of "252"
  //  StringToNumber = toInt(JoinTheChars); //ERROR 'toInt' was not declared in the scope
  delay(1000);

}

Thanks...

So "String JoinTheChars;" should not be used as a string variable but rather "char allChars[7]; // added" as the string variable?

Cleaned up a bit. THANKS again :wink:

char allChars[7];       // added
char* pEnd;             // added
float floatChars;       // added
float Circumf;           //holds circumference in mm
void setup() {
  Serial.begin(9600);
 }                      //Curly bracket for end of SETUP

void loop() {
  allChars[0] =  '0';
  allChars[1] =  '1';
  allChars[2] =  '0';
  allChars[3] =  '.';
  allChars[4] =  '0';
  allChars[5] = '\0';                       // add a terminating null and now allChars has a legitimate C string

  Serial.print("Input as a String  - ");     
  Serial.println(allChars);                 
     floatChars = atof(allChars);           // Convert String to Float
  Serial.print("Converted to Value - ");    
  Serial.print(floatChars);                 
  Serial.println("   mm");                  

  Serial.print("Circumference      - "); 
     Circumf = floatChars * PI;         // Convert Diameter to Circumference length     
  Serial.print(Circumf);            
  Serial.println("   mm");                  
  while(1);
}                                           //Curly bracket for end of LOOP

@OP

This is a solution to receiving ASCII coded digits from 4x4 Keypad, save them in a character type array and retrieve the float value from the array.
kbblk-41x.png

The Codes:

const byte rows = 4;   //four rows
const byte cols = 4;  //four columns
char myDia[6] = ""; //to store five key stroke's ASCII codes and null-byte                
int arrayTracker = 0;
char keys[rows][cols] = { //2-dimensional array contains ASCII codes for the labels of Keys
                          {'1','2','3','A'},
                          {'4','5','6','B'},
                          {'7','8','9','C'},
                          {'.','0','#','D'}
                        };

byte rowPins[rows] = {9,8,7,6};    //R1R2R3R4 connection Pin = 9876
byte colPins[cols] = {5,4,3,2};     //C1C2C3C4 connection Pin = 5432
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,rows,cols);  //Library Function

void setup()
{
  Serial.begin(9600);
  Serial.println("Enter digits and ppoint for diameter from Keypad..!");
}

void loop()
{
  char input = keypad.getKey();  //scan keypad
  if(input != 0x00)        //if scan code is non-zer0
  {
    myDia[arrayTracker] = input;   //store the ASCII code of the lable of key in array
    Serial.print(myDia[arrayTracker]);//show the label of key in LCD
    arrayTracker++;                   //adjust next key position
   }
   
   if(arrayTracker == 5)
   {
      float dia = atof(myDia);     //converting string array into numerical value of type float
      Serial.println();
      Serial.println("Enetered ASCII coded digits are converted to float and saved in dia"); 
      Serial.print("Diameter: ");
      Serial.println(dia, 2);
      while(1)  ;
   }
 }

The Screenshot:
sm130.png

kbblk-41x.png

sm130.png

Thanks, :slight_smile: did my whole sketch.

I have been reading the suggested posts. :o

How should this sketch be broken up in "functions" so the loop only have the names of the functions and the functions are smaller sketches that can be reused in a different sketch?

Interrupt part is written and working and displaying speed on LCD, so need to break that one up and can then copy those parts to this sketch.

Also more parts to get to a menu to add the type of material being machined.

so makes a lot of sense to start this one right.