Probleme on char to float conversion

Hi,

I'm having problem converting a string to float. I'm building a prototype where to goal is to control an arduino through labview to adjust the voltage of a light. To do that I have to use the "visa" block in labview, which causes my problem because to use them I have to input a string variable, which in turn i'm supposed to convert into a float to use in my arduino code but everytime i try someting the value is set to 0.00

char labviewSTR;
int pwm = 4; // assigns pin 4 to variable pwm
int t1 = 0;   // declares variable t1
int t2 = 0;   // declares variable t2

void setup() {
 Serial.begin(9600);
 pinMode(pwm,OUTPUT);
}

void loop() {
  if (Serial.available()>0)
  {
    labviewSTR = Serial.read();
    float labviewFLT = atof(labviewSTR);
    Serial.print(labviewFLT); //this line here only return 0.00
    t2= labviewFLT / (5 / 1023); // reads the voltage at pwm and saves in t2
    t1= 1000-t2;         // subtracts t2 from 1000 ans saves the result in t1
    digitalWrite(pwm, HIGH); // sets pin 12 HIGH
    delayMicroseconds(t1);   // waits for t1 uS (high time)
    digitalWrite(pwm, LOW);  // sets pin 12 LOW
    delayMicroseconds(t2);   // waits for t2 uS (low time)
    }
  else
  {
    digitalWrite(pwm,0);
  }
  

}

I'd be so grateful if someone was able to explain what I'm doing wrong here

I'm a very new user, please be patient.

Oh, and here is the labview code if it help :
image

Your labviewSTR variable is just a single character. And the atof function expects a char array.
In order to convert the data correctly, you must first read the entire string from the serial, not just the first character.

I think it would be better for you to read Serial Input Basics tutorial first

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.