LCD display double button press changing modes

Hi there,
i was wondering if you could aid in a project i have been working on however, have came across a problem i am not able to solve.
One of the goals of the project is to display the MPU6050 values(pitch and roll values) on the LCD display however i can only display one of the modes on the display at a time(pitch and roll). Further, the other aim is for what is displaying on the LCD display to change from “pitch to roll” then from “roll to pitch” after a double click of the button. Fortunately i was able to accomplish most that i wanted but came across a slight problem when running code below:

#include <Wire.h>
#include <MPU6050.h>
#include <LiquidCrystal.h>
MPU6050 mpu;


//global variables for LCD
LiquidCrystal lcd{13,12,11,10,9,8};  //LiquidCrystal(RS, E, D4, D5, D6, D7);

const int button1 = 4;
int buttonValue;

int counter = 0;
boolean rollmode=false;
boolean pitchmode=true;


void setup() 
{
   lcd.begin(16,2);                  //intialize LCD monitor
 
  Serial.begin(115200);

  Serial.println("Initialize MPU6050");

  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    
    delay(500);
  }
}


void loop()
{
  // Read normalized values 
  Vector normAccel = mpu.readNormalizeAccel();

  // Calculate Pitch & Roll
  float pitch = -(atan2(normAccel.XAxis, sqrt(normAccel.YAxis*normAccel.YAxis + normAccel.ZAxis*normAccel.ZAxis))*180.0)/M_PI;
  float roll = (atan2(normAccel.YAxis, normAccel.ZAxis)*180.0)/M_PI;

 
  // Output
  Serial.print(" Pitch = ");
  Serial.print(pitch);
  Serial.print(" Roll = ");
  Serial.print(roll);
  
  Serial.println();
  delay(10);

  buttonValue = digitalRead(button1);
  
  if(buttonValue==HIGH){
    delay(250);
   if(counter==0){
    
    counter=counter+1;
  }
  else if(counter==1){
  
  if (rollmode==false && pitchmode==true){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("roll");
      lcd.setCursor(6,0);
      lcd.print(roll);
      lcd.setCursor(13,0);
      lcd.print("deg");
      Serial.println(roll);
      rollmode=true;
      pitchmode=false;
      
  }else{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("pitch");
    lcd.setCursor(6,0);
    lcd.print(pitch);
    lcd.setCursor(13,0);
    lcd.print("deg");
    
    rollmode=false;
    pitchmode=true;
      
  }
  counter=0;
  }
  
  }
  
  


       
}

The problem that i am having is that when the pitch and roll values are displaying on the LCD it is not updating when the MPU6050 position is altered. The values do change when pressing the button twice changing what is displaying on the LCD display between roll and pitch. When i checked the pitch and roll values in the serial monitor the values altered when the gyroscope position was changed.

Can you help me please?

It’s hard to see in the messy formatting of your code (do an autoformat in the IDE, that’ll clean it up a lot) but my suspicion is that the problem lies in the way you handle the counter variable.

Some good commenting on what the code is doing and why you’re coding it the way you did it will be very helpful as well.

How is you button wired? What does it read when pressed or not pressed?

The entire lcd display routine is within this condition

 if(buttonValue==HIGH){

If the buttonValue is LOW when not pressed, then the display will not change. The lcd display has some internal memory and it will hold and display the last values.

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