Reputation: 57
I created a small model plane with arduino, and now want to create a simple FCS page on the serial monitor. I used printf to print all the variables in one line, but it isn't printing everything in the printf line.
char buf[150];
sprintf(buf, "\t LEFT \t RIGHT \n AIL \t %d \t %d \n STAB \t %d \t %d \n TEF \t %d \t %t \n LEF \t %d \t %d \n RUD \t %d \n\n SPD %f KTS %f M \n AOA %f", leftAilPos, rightAilPos, leftStabPos, rightStabPos, TEFPos, TEFPos, LEFPos, LEFPos, RuddPos, Spd, AoA);
Serial.println(buf);
I thought this would print something like this:
Left Right
AIL 20 -15
STAB 12 12
TEF 5 5
LEF 3 3
RUD 0
SPD 250KTS 0.37M
AOA 3.5
However, on the serial monitor, only the first few lines get printed:
Left Right
AIL 20 -15
STAB 12 12
TEF 5
Is there something I'm doing wrong or missing?
Upvotes: 2
Views: 733
Reputation: 57
So I realized where my error was, I typed %t instead of %d in one of the variables, here;
TEF \t %d \t %t \n
This wasn't popping up as an error but it kinda stopped showing the rest of the line. Fixing this now actually shows everything.
Thanks to everyone else for trying to help, and I'll actually still use some of those solutions hoping to make my program a bit more efficient.
Upvotes: 0
Reputation: 3243
Since you are just printing out to Serial, the best thing for you to do is to remove the sprintf altogether and simply use a bunch of Serial.print and Serial.println statements. The code will take more lines on the page, but will actually be a lot smaller since you don't have to bring sprintf in, it's a huge function. It's more efficient that way too, sprintf is an expensive function. Anything on the other end of the line will have no way of knowing if you printed it all with one line or with a million Serial.print one for each letter. Serial output is buffered so there's no difference as far as the receiver is concerned.
Upvotes: 1
Reputation: 733
Your code is absolutely fine. What you are not considering is the serial port buffer size which is just 64 Bytes. Anything larger than that will truncate. So, you need to split your data into two parts to successfully send it over the serial port.
Or else you can modify the size of the serial buffer by altering the Arduino core library files.
Update: In newer version of Arduino IDE, they have changed the location of the SERIAL_BUFFER_SIZE
Here is the code snippet from my
[Local Drive](C:)\[Installation Path](Program Files (x86))\Arduino\hardware\arduino\avr\cores\arduino
In File USBAPI.h
#ifndef SERIAL_BUFFER_SIZE
#if ((RAMEND - RAMSTART) < 1023)
#define SERIAL_BUFFER_SIZE 16
#else
#define SERIAL_BUFFER_SIZE 64
#endif
#endif
#if (SERIAL_BUFFER_SIZE>256)
#error Please lower the CDC Buffer size
#endif
change to
#ifndef SERIAL_BUFFER_SIZE
#if ((RAMEND - RAMSTART) < 1023)
#define SERIAL_BUFFER_SIZE 16
#else
#define SERIAL_BUFFER_SIZE 256
#endif
#endif
#if (SERIAL_BUFFER_SIZE>256)
#error Please lower the CDC Buffer size
#endif
Note: Don't increase beyond 256 Bytes as there are some issues and the serial port tends to behave erratically.
Upvotes: 3