0

I am having difficulties when printing any type of variable using the Serial.print(ln) command.

The Serial Monitor becomes inactive after printing for approximately 7 - 15 times.

I currently have the STM32 Blue Pill Board, running Mac OS Mojave and Arduino IDE v 1.8.9

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

}

void loop() {
  float raw = analogRead(PA0);
  float voltage = (raw * 3.3) / 4096;
  Serial.println(voltage);
  delay(500);
}
  • isolate the area of the error .... start by changing the print command to Serial.println("voltage"); .... it will print the word "voltage" .... just let it run to see if it also hangs up – jsotola Aug 12 at 14:45
0

Printing a float should work (at least with an Arduino), but to test if it cannot handle it correctly, I suggest you convert the float to a string (e.g., with the C function ftoa, and print the characters one by one or as a string.

Since this C function is not available on Arduino, use the function implementation of this article, credits to Martin Waller:

char *ftoa(char *buffer, double d, int precision) {

    long wholePart = (long) d;

    // Deposit the whole part of the number.

    itoa(wholePart,buffer,10);

    // Now work on the faction if we need one.

    if (precision > 0) {

        // We do, so locate the end of the string and insert
        // a decimal point.

        char *endOfString = buffer;
        while (*endOfString != '\0') endOfString++;
        *endOfString++ = '.';

        // Now work on the fraction, be sure to turn any negative
        // values positive.

        if (d < 0) {
            d *= -1;
            wholePart *= -1;
        }

        double fraction = d - wholePart;
        while (precision > 0) {

            // Multipleby ten and pull out the digit.

            fraction *= 10;
            wholePart = (long) fraction;
            *endOfString++ = '0' + wholePart;

            // Update the fraction and move on to the
            // next digit.

            fraction -= wholePart;
            precision--;
        }

        // Terminate the string.

        *endOfString = '\0';
    }

   return buffer;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.