Good Day All,
Tyring to take the the numeric keyboard input from the Arduino PS2 KeyBoard Library and convert from string to numberic (float) values so I may use it in calculations. With the following code I'm getting "invalid cast from type 'String' to type 'float' " error when I verify the code.
I have tried serval different methods to convert it with no luck. atof also does not work.
:~
FStepDistance = float(StepDistance);
Would anyone have any ideas how convert the string to float? Need to be able to do calculations with numbers like this 10.000883
Thanks for any help you can provide,
Don Anderson
String StepDistance = "";
float FStepDistance ='';
/* PS2Keyboard library example
for more information you can read the original wiki in arduino.cc
at http://www.arduino.cc/playground/Main/PS2Keyboard
or http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html
Like the Original library and example this is under LGPL license.
Modified by Cuninganreset@gmail.com on 2010-03-22
Modified by Paul Stoffregen <paul@pjrc.com> June 2010
*/
#include <PS2Keyboard.h>
const int DataPin = 2;
const int IRQpin = 3;
PS2Keyboard keyboard;
void setup() {
delay(1000);
keyboard.begin(DataPin, IRQpin);
Serial.begin(9600);
Serial.println("Keyboard Test:");
}
void loop() {
if (keyboard.available()) {
// read the next key
char c = keyboard.read();
// check for some of the special keys
if (c == PS2_ENTER) {
Serial.println();
Serial.print(StepDistance);
StepDistance = "";
Serial.println();
} else if (c == PS2_TAB) {
Serial.print("[Tab]");
} else if (c == PS2_ESC) {
Serial.print("[ESC]");
} else if (c == PS2_PAGEDOWN) {
Serial.print("[PgDn]");
} else if (c == PS2_PAGEUP) {
Serial.print("[PgUp]");
} else if (c == PS2_LEFTARROW) {
Serial.print("[Left]");
} else if (c == PS2_RIGHTARROW) {
Serial.print("[Right]");
} else if (c == PS2_UPARROW) {
Serial.print("[Up]");
} else if (c == PS2_DOWNARROW) {
Serial.print("[Down]");
} else if (c == PS2_DELETE) {
Serial.print("[Del]");
} else {
// otherwise, just print all normal characters
Serial.print(c);
StepDistance = StepDistance + c;
FStepDistance = float(StepDistance);
}
}
}