Go Down

Topic: PS2KeyBoard Library string or char convert to float so calculations may be made? (Read 544 times) previous topic - next topic

danders238

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


Code: [Select]

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


Cybernetician

Code: [Select]

String StepDistance = "";
float FStepDistance ='';

StepDistance = StepDistance + c;
FStepDistance = float(StepDistance);


Float Conversion

Code: [Select]

char *StepDistance = "1.900";
float FStepDistance = atof(StepDistance);

OR
char *StepDistance = "1.900";
float FStepDistance ;

sscanf(StepDistance, "%f", FStepDistance);
From Idea To Invention

PaulS

Code: [Select]
String StepDistance = "";
float FStepDistance ='';

StepDistance = StepDistance + c;
FStepDistance = float(StepDistance);

Did you even bother trying to compile that load of crap? The float() "function" is a macro that hides the "complexity" of a cast. You can NOT cast a String to a float. Initializing a float variable to an empty character makes no sense.

danders238

I'm going to assume that you are having a bad day and have no experience with the PS2_KeyBoard Library Simple_Test example or the Arduino IDE.

Correct me if I'm wrong, but when I said I verified the code (hold mouse over checkmark icon in Arduino IDE 1.01.01) I meant when compiling the code above I received the error above.  Yes, all of the code except the code related to converting string variable StepDistance to float variable FStepDistance compiles and runs correctly. I was trying to initialize FStepDistance to NULL.

All that I need help with is: How do I convert a variable of string data type to a variable of numeric data type in the Arduino IDE?

Thanks for any help you can provide,
Don Anderson

Arrch


All that I need help with is: How do I convert a variable of string data type to a variable of numeric data type in the Arduino IDE?


You're not using a string, you are using a String.

Using a string is as simple as atof(). (Not 100% sure it's implemented in Arduino's version of cstdlib)

PaulS

Quote
Not 100% sure it's implemented in Arduino's version of cstdlib

It is.

Quote
I'm going to assume that you are having a bad day and have no experience with the PS2_KeyBoard Library Simple_Test example or the Arduino IDE.

Assume anything you like. I was commenting on Cybernetician's reposting of that code. I didn't notice that you (OP) had that in your code. Please read Arrch's comment. There is a world of difference between a string and a String. Once you learn that there is, and use the proper letter, we'll get along better.

Go Up