Different values on power up and after manual reset - Arduino Nano

I come to seek help for a strange situation. I have a code installed on an Arduino Nano where I use an I2C display and an MPU6050 to measure G forces on 3 axis. When I power up via USB the values are wrong. About twice as much. However, if I do not turn off the power, and manually reset the button, it starts and the values are normal.

Any idea?

Perhaps if you posted your code and a schematic ...

Another good thing to do if your sketch acts unexpectedly is to make sure you set the "Compiler warnings:" preference to "All" and look for any warning messages in the text box below your sketch after a Verify or Upload. These will point out things that look funny, like using a local variable before it is initialized.

Thanks

I have no warnings

this is the code

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <MPU6050.h>

//#define SCREEN_WIDTH 128
//#define SCREEN_HEIGHT 64
#define OLED_RESET     4

Adafruit_SH1106 display(OLED_RESET);
MPU6050 mpu;


int dot_x;
int dot_y;
int dot_z;

float gx, gy, gz;
float ix = 0, iy = 0, iz = 0;
unsigned long tim = 0;
unsigned long tim_disp = 0;
void setup()
{

  Serial.begin(9600);
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  Serial.println(F("Initialize MPU6050"));
  while (!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_8G))
  {
    Serial.println(F("Could not find a valid MPU6050 sensor, check wiring!"));
    delay(500);
  }

  display.display();

  tim = millis();
  tim_disp = millis();

   

 Vector rawAccel = mpu.readRawAccel();
  ix = (float)rawAccel.XAxis  / 4096;
  iy = (float)rawAccel.YAxis  / 4096;
  iz = (float)rawAccel.ZAxis  / 4096;

  Serial.println(F("Initial g forces"));
  Serial.print(F("X:"));
  Serial.print(ix);
  Serial.print(F("\tY:"));
  Serial.print(iy);
  Serial.print(F("\tZ:"));
  Serial.println(iz);
}

void loop()
{
  if (millis() - tim > 150)
  {
    tim = millis();
    Vector rawAccel = mpu.readRawAccel();
   dot_x = round(rawAccel.XAxis / 4096 * 20 + 21);
   dot_y = round(rawAccel.YAxis / 4096 * 20 + 43);
   dot_z = round(rawAccel.ZAxis / 4096 * 20 - 20);

    gx = (float)rawAccel.XAxis  / 4096 ;
    gy = (float)rawAccel.YAxis  / 4096 ;
    gz = (float)rawAccel.ZAxis  / 4096 ;



    if(gx > ix) ix = gx;
    if(gy > iy) iy = gy;
    if(gz > iz) iz = gz;
    //    Serial.print(dot_x);
    //    Serial.print("\t");
    //    Serial.print(dot_y);
    //    Serial.print("\t");
    //    Serial.print(dot_z);
    Serial.print(F("\tG:"));
    Serial.print(gx);
    Serial.print(F("\t"));
    Serial.print(gy);
    Serial.print(F("\t"));
    Serial.println(gz);
  }

  if (millis() - tim_disp > 16)
  {
    display.clearDisplay();
    //display.display();
    draw_main_circle();
    draw_dot(dot_x, dot_y);
    print_acc_data(gx, gy, gz);
    display.display();
  }
}


void draw_main_circle()
{
  display.drawCircle(21, 43, 20, WHITE);
  display.drawFastVLine(21, 23, 40, WHITE);
  display.drawFastHLine(1, 43, 40, WHITE);
}

void draw_dot(int dt_x, int dt_y)
{
  if (dt_y > 63) dt_y = 63;
  else if (dt_y < 23) dt_y = 23;
  if (dt_x > 40) dt_x = 40;
  else if (dt_x < 1) dt_x = 1;
  display.fillCircle(dt_x, dt_y, 3 , WHITE);
  //display.fillCircle(dt_x, dt_y, 5 , WHITE); // ponto maior
}

void print_acc_data(float _x, float _y, float _z)
{
  char vtd[5] = "";
  display.setCursor(1, 4);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("G-Meter");
  
  display.setCursor(48, 4);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("ACT");

  display.setCursor(90, 4);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("MAX");

  //  display.setCursor(68, 18);
  //  display.setTextSize(1);
  //  display.setTextColor(WHITE);
  //  display.print("X " + String(_x));
  //
  //  display.setCursor(68, 32);
  //  display.setTextSize(1);
  //  display.setTextColor(WHITE);
  //  display.print("Y " + String(_y));
  //
  //  display.setCursor(68, 46);
  //  display.setTextSize(1);
  //  display.setTextColor(WHITE);
  //  display.print("Z " + String(_z));

  dtostrf(_x, 0, 1, vtd);
  display.setCursor(48, 18);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("X " + String(vtd));

  dtostrf(_y, 0, 1, vtd);
  display.setCursor(48, 32);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("Y " + String(vtd));

  dtostrf(_z, 0, 1, vtd);
  display.setCursor(48, 46);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print("Z " + String(vtd));

  dtostrf(ix, 0, 1, vtd);
  display.setCursor(90, 18);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print(vtd);

  dtostrf(iy, 0, 1, vtd);
  display.setCursor(90, 32);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print(vtd);
  
  dtostrf(iz, 0, 1, vtd);
  display.setCursor(90, 46);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.print(vtd);
}

First picture is just after an upload shows on Z axis 1 G . And this is the value that i whant

Second picture is when i only disconnect Arduino from power and reconect again. Shows 4 G on Z axis !!! :frowning:

Any clue?

X-TREM:
Any clue?

Sorry. I tried to install a few libraries to attempt to compile your sketch and ended up guessing the wrong libraries. The Library Manager doesn’t show an “Adafruit_SH1106” library. The closest they have is “Adafruit_SH110X” and that doesn’t work on your sketch. I tried the “Adafruit_MPU6050” library among the three provided by Library Manager but that doesn’t work with your sketch either.
Perhaps someone else will get your sketch to compile.