Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update printf value on same line instead of new one

Tags:

c

I would like to know if there is a way in C to overwrite an existing value that has already been printed instead of creating a new line every time or just moving over a space. I need to obtain real time data from a sensor and would like it to just sit there and keep updating the existing values without any scrolling. Is this possible?

UPDATE: ADDED CODE

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>

#include <wiringPi.h>
#include <wiringPiI2C.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23


int fd;
short x = 0;
short y = 0;
short z = 0;
int main (){



    fd = wiringPiI2CSetup(0x69); // I2C address of gyro
    wiringPiI2CWriteReg8(fd, CTRL_REG1, 0x1F); //Turn on all axes, disable power down
    wiringPiI2CWriteReg8(fd, CTRL_REG3, 0x08); //Enable control ready signal
    wiringPiI2CWriteReg8(fd, CTRL_REG4, 0x80); // Set scale (500 deg/sec)
    delay(200);                    // Wait to synchronize

void getGyroValues (){
    int MSB, LSB;

    LSB = wiringPiI2CReadReg8(fd, 0x28);
    MSB = wiringPiI2CReadReg8(fd, 0x29);
    x = ((MSB << 8) | LSB);

    MSB = wiringPiI2CReadReg8(fd, 0x2B);
    LSB = wiringPiI2CReadReg8(fd, 0x2A);
    y = ((MSB << 8) | LSB);

    MSB = wiringPiI2CReadReg8(fd, 0x2D);
    LSB = wiringPiI2CReadReg8(fd, 0x2C);
    z = ((MSB << 8) | LSB);
}
    for (int i=0;i<10;i++){
    getGyroValues();
    // In following Divinding by 114 reduces noise
    printf("Value of X is: %d\r", x/114);
//  printf("Value of Y is: %d", y/114);
//  printf("Value of Z is: %d\r", z/114);
    int t = wiringPiI2CReadReg8(fd, 0x26);
    t = (t*1.8)+32;//convert Celcius to Fareinheit
    int a = wiringPiI2CReadReg8(fd,0x2B);
    int b = wiringPiI2CReadReg8(fd,0x2A);
//  printf("Y_L equals: %d\r", a);
//  printf("Y_H equals: %d\r", b);
    int c = wiringPiI2CReadReg8(fd,0x28);
    int d = wiringPiI2CReadReg8(fd,0x29);
//  printf("X_L equals: %d\r", c);
//  printf("X_H equals: %d\r", d);
    int e = wiringPiI2CReadReg8(fd,0x2C);
    int f = wiringPiI2CReadReg8(fd,0x2D);
//  printf("Z_L equals: %d\r", e);
//  printf("Z_H equals: %d\r", f); 

//  printf("The temperature is: %d\r", t); 
    delay(2000);
}
};
like image 719
Yamaha32088 Avatar asked Mar 03 '13 23:03

Yamaha32088


People also ask

Does printf always print a new line?

The printf statement does not automatically append a newline to its output. It outputs only what the format string specifies. So if a newline is needed, you must include one in the format string.

How do I add a new line in printf?

The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen.

Does printf change value?

@pm100: Yes.


2 Answers

You should add \r to your printf as others have said. Also, make sure you flush stdout, as stdout stream is buffered & will only display what's in the buffer after it reaches a newline.

In your case:

for (int i=0;i<10;i++){
    //...
    printf("\rValue of X is: %d", x/114);
    fflush(stdout);
    //...
}
like image 139
Zaxter Avatar answered Oct 12 '22 03:10

Zaxter


You're looking for a carriage return. In C, that's \r. This will take the cursor back to the start of the current line without starting a new line (linefeed)

like image 26
Yaniv Avatar answered Oct 12 '22 05:10

Yaniv