Modify mpu6050 display

I am using mpu6050 with arduino and oled to display titled anglation

here the code and it is working so good

//Import all required libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
const int MPU6050_addr = 0x68;
int16_t axis_X,axis_Y,axis_Z;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
  Serial.begin(9600);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { //Ive changed the address //already chill
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds
  // Clear the buffer
  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  Wire.begin();
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  display.clearDisplay();
  display.setTextSize(1.5);
  display.setTextColor(WHITE);
  display.setRotation(0);
}

void loop() {
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU6050_addr, 14, true);
  axis_X = Wire.read() << 8 | Wire.read();
  axis_Y = Wire.read() << 8 | Wire.read();
  axis_Z = Wire.read() << 8 | Wire.read();
  int xAng = map(axis_X,minVal,maxVal,-90,90);
  int yAng = map(axis_Y,minVal,maxVal,-90,90);
  int zAng = map(axis_Z,minVal,maxVal,-90,90);
  x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
  y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
  z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("X axis = ");
  display.print(x);
  display.print("\n");
  display.print("y axis = ");
  display.print(y);
  display.print("\n");
  display.print("z axis = ");
  display.print(z);
  display.println("");
  display.display();
  delay(200); // Pause for 2 seconds
}

but i need to remove the two number after (.) (i marked it with red circle), how can i do this?

Hello,
Modify the display code to print the integer values

display.print((int)x); // Cast the variables to int
display.print("\n");
display.print("y axis = ");
display.print((int)y); // Cast the variables to int
display.print("\n");
display.print("z axis = ");
display.print((int)z); // Cast the variables to int
display.println("");```
1 Like

Would you want to add .5 to each value to round to the nearer integer, or truncate?

Turncate

Why turncate [sic]?

Why so many prints?

use the optional second parameter of print to set the number of decimal places, and add 0.5 to round the truncation:

  display.print("X axis = ");
  display.print(x+0.5,0);
  display.print("\n");
  display.print("y axis = ");
  display.print(y+0.5,0);
  display.print("\n");
  display.print("z axis = ");
  display.print(z+0.5,0);

Or simply use println on the previous print!

1 Like

I moved your topic to an appropriate forum category @mazeen .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

thank you all for helping me, i was wanting to remove the number after (.), and fatmazahr solution working successfully

So, please mark the topic "solved", so we don't waste any more time on it.

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