Go Down

Topic: Cannot use array values in an if statement (Read 208 times) previous topic - next topic

TEAC

Hello everyone, I'm currently working on a project where I have to use the MPU6050 and what I tried to do was use the I2Cdevlib code to allow my MPU6050 to give stable enough values. Now I'm trying to create parameters to it, so I can write what I want in an LCD according to the position of the gyroscope but for some reason, it does not recognize the statement and does not write anything at all. This is the code I'm trying to use:

#include "MPU6050_6Axis_MotionApps20.h"
#include <LiquidCrystal.h>
LiquidCrystal lcd(3,2,11,9,7,5);
MPU6050 mpu;
uint8_t mpuIntStatus;
uint16_t packetSize;
uint16_t fifoCount;
uint8_t fifoBuffer[64];
Quaternion q;
VectorFloat gravity;
float ypr[3];
#define contra 9
#define bri 10

void setup() {


  //C√≥digo do MPU6050.
    Wire.begin();
    lcd.begin(16, 2);
    pinMode(contra, OUTPUT);
    pinMode(bri, OUTPUT);
    digitalWrite(contra, LOW);
    analogWrite(bri, 255);
    lcd.print("Inete TEAC16");
    lcd.setCursor(0, 1);
    TWBR = 24;
    mpu.initialize();
    mpu.dmpInitialize();
    mpu.setXAccelOffset(-1343);
    mpu.setYAccelOffset(-1155);
    mpu.setZAccelOffset(1033);
    mpu.setXGyroOffset(19);
    mpu.setYGyroOffset(-27);
    mpu.setZGyroOffset(16);
    mpu.setDMPEnabled(true);
    packetSize = mpu.dmpGetFIFOPacketSize();



  //Por algum motivo se utilizar "9600" aparece erro "FIFO overflow!".
    //COMENTAR_OFICIAL
    Serial.begin(115200);


}

void gyro()
{
    while (fifoCount < packetSize) {
    //do stuff
 
    fifoCount = mpu.getFIFOCount();
  }
 
  if (fifoCount == 1024) mpu.resetFIFO();
 
  else {
      fifoCount = mpu.getFIFOCount();
     
      mpu.getFIFOBytes(fifoBuffer, packetSize);


      mpu.resetFIFO();
     
      fifoCount -= packetSize;


        mpu.dmpGetQuaternion(&q, fifoBuffer);
        mpu.dmpGetGravity(&gravity, &q);
        mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  }
}

void screen()
{

  Serial.print(" ||p = "); Serial.print(ypr[1]*180/PI);
  Serial.print(" r = "); Serial.print(ypr[2]*180/PI);
  Serial.println();
  delay(200);
}

void loop() {
gyro();
screen();

if(ypr[1]>=40) //Tried this basic task, reading if the value was above 40 and eventhough it was it did not // write anything at all
{
 lcd.setCursor(0,2);
 lcd.print("Letter: A");
 Serial.println("Letter A");

}




}

UKHeliBob

Code: [Select]
  lcd.begin(16, 2);

Code: [Select]
    lcd.setCursor(0, 2);

How many rows are there on the LCD ?
How are they numbered, I wonder ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

TEAC

It's a 16x2 LCD display, 16 columns 2 lines

UKHeliBob

It's a 16x2 LCD display, 16 columns 2 lines
Yes, but how are the lines numbered ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

TEAC

They are numbered  as 1 and 2. The counting does not start on 0

Idahowalker

#5
May 30, 2019, 04:16 pm Last Edit: May 30, 2019, 04:19 pm by Idahowalker
Why not try
Code: [Select]


Serial.println( ypr[1], 6 );

if(ypr[1]>=40) //Tried this basic task, reading if the value was above 40 and eventhough it was it did not // write anything at all
{
 lcd.setCursor(0,2);
 lcd.print("Letter: A");
 Serial.println("Letter A");
}

to see what ypr[1] actually is?

Most likely, you may find that, basically, 40 is not 40.0f

PaulS

Quote
They are numbered  as 1 and 2. The counting does not start on 0
Why does column numbering start at 0, then?

UKHeliBob

They are numbered  as 1 and 2. The counting does not start on 0
Which LiquidCrystal library are you using ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up