I2C LCD causes problems

OK folks, I loaded the code to the milling machine powerfeed this morning and happy to report it works just fine. I never could have done it without ya'll's help. THANK YOU!!

Now I do have a couple of questions. In the code below

char rpmStr[6];
        sprintf(rpmStr, "%4d", rpm);
        //dtostrf(rpm, 4, 0, rpmStr);
        lcd.setCursor(7, 0);
        lcd.print(rpmStr);
        lastRPM = rpm;

The line "sprintf(.......); " is the way @groundFungus wrote the code. I thought the"sprintf" why up in the "setup()" the "serial.begin(115200);" statement was made. BUT - when I comment that line out the code still works just fine. Why? Is the "serial.begin(115200);" needed for some reason?

Next question, the line "sprintf(rpmStr, "%4d", rpm);" works very good (I changed the "%5d" to "%4d" spacing the printed text where I wanted it. Since I don't fully understand (at all) the %5d" function, I tried the line that's commented out, which is the same as the ips printing section. The commented line also works, and I sorta understand it. Is there an advantage of one line vs the other line?

Again, allow me to say how much I appreciate the patience ya'll have shown helping and teaching me.

The Serial.begin(115200); is used to initialize and open the serial port. Since this code does not really use serial, that line is unnecessary. I just put that line in all the code that I write, by default, because I almost always use the serial port. The serial port is the best debugging tool that one has when working with the Arduino. Serial.begin() and sprintf() have nothing to do with each other.

Those 2 lines are pretty much equivalent. The 4 in sprintf line is 4 characters for the number and %d is a place holder for an int data type number. In the dtostrf the 4 is minimum 4 characters wide and 0 places after the decimal point, which is, effectively, an integer. The dtostrf() function, like @bperrybap explained in reply #31, is used to format float data types to ASCII text because sprintf() in the AVR processors does not support the float data type (%f). So if you want to convert float data and print decimal places use dtostrf, if you don't want decimal places (integer) you can use sprintf.

Now, if you change the width in the sprintf() function (the 4 or 5) you also must be sure that the buffer that you are writing to (rpmStr) has enough room to hold that width + 1 for the (invisible) terminating null character that terminates all strings. Fortunately, in this case, it does. But if you increase the width to 6 rpmStr will no longer be wide enough and you will write beyond the bounds of the rpmStr array and interesting (usually BAD) things are likely to happen.

1 Like

@groundFungus: an excellent explanation you've provided. Thank you my teacher :slight_smile: Between you and @bperrybap I have learned LOTS. As I said, the powerfeed is working nicely on the mill now.

1 Like

Here's a couple of short videos showing the power feed in operation. One is using a flycutter, other is showing the different speeds and how the table and hand wheel are moving.

Thank ya'll again for all the help.

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