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);
}
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.