-1

I am having trouble with this code:

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

// Timers
unsigned long timer = 0;
float timeStep = 0.01;

// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;

int button_pin=5;
int button_state;
void setup() 
{
  Serial.begin(115200);

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

  // Calibrate gyroscope. The calibration must be at rest.
  // If you don't want calibrate, comment this line.
  mpu.calibrateGyro();

  // Set threshold sensivty. Default 3.
  // If you don't want use threshold, comment this line or set 0.
  mpu.setThreshold(3);

  pinmode(button_pin,INPUT)
}

void loop()
{
  int button_state=digitalRead(button_pin)

  timer = millis();

  // Read normalized values
  Vector norm = mpu.readNormalizeGyro();

  // Calculate Pitch, Roll and Yaw
  pitch = pitch + norm.YAxis * timeStep;
  roll = roll + norm.XAxis * timeStep;
  yaw = yaw + norm.ZAxis * timeStep;

  // Output raw
  Serial.print(" Pitch = ");
  Serial.print(pitch);
  Serial.print(" Roll = ");
  Serial.print(roll);  
  Serial.print(" Yaw = ");
  Serial.println(yaw);

  // Wait to full timeStep period
  delay((timeStep*1000) - (millis() - timer));
}

But when I add this code at the end:

If (pitch <=90 && pitch => 15 && button_state =(HIGH))
{
}

I keep getting this error:

error: lvalue required as left operand of assignment

I know the issue is about the pitch part of the if. I just need to know how to fix it.

  • 4
    The if-statement should be if not If – sa_leinad Oct 12 '17 at 5:21
  • 1
    Could you please post the error that you are getting. – sa_leinad Oct 12 '17 at 5:22
  • 4
    also button_state =(HIGH) should be button_state == HIGH or more simply button_state – sa_leinad Oct 12 '17 at 5:23
  • @sa_leinad I get this error (error: lvalue required as left operand of assignment) this iş the error I am getting – Fr1nge Oct 12 '17 at 5:57
1

You have two declarations of button_state. This won't cause a problem because the scoping rules will mean inside loop you are using the local one and everywhere else you are using the global one. Personally I would remove the global one since you aren't using it.

As mentioned in the comments you have mistyped the If keyword (but I think this is just on here not in you code) and you have used an assignment statement rather than a equivalence (= instead of ==).

Since you have tagged it as code review here are some minor points you might want to consider.

If a value doesn't change then you could mark it as const, this doesn't make much difference to the compiler, it already knows, but it will help you when you come back to the code next week. So: const int button_pin = 5;

If you only use a const int value once you might be better #defining it (more than once a const value should produce smaller code). The pre-processor (part of the compile process) will replace the defined name with the value, but you have easier to read and maintain code. So: #define button_pin 5

If you #define an int then always enclose the value in brackets to protect it from "arithmetic issues" (I can't think of a better way of describing it). So: #define button_pin (5)

  • thanks for your suggestions, but still you haven't told me how to fix the error 😀 – Fr1nge Oct 12 '17 at 7:48
  • The solution to your problem was answered by sa_leinad in the comments on you post and is covered in the second paragraph of my answer. "you have used an assignment statement rather than a equivalence". If you tell me you IP address I'll remote into your machine and fix your code for you if you like. – Code Gorilla Oct 12 '17 at 12:49
1

The first thing I noticed was that you had a capital I in your if-statement.

Secondly, button_state =(HIGH) should be button_state == HIGH. A single = sign means that you are assigning a value to the variable on the left - in this case button_state. It is a common mistake that I have made many times.

Also, because it is a boolean comparison, you can simplify the statement by removing the == HIGH leaving just button_state.

This would then change the if-statement to the following:

if ((pitch <= 90) && (pitch => 15) && button_state)

I have added extra parentheses for clarity/readability but they're technically not needed.


In addition to what Code Gorilla has already stated in his answer and a few additional semi-colons, your code should look something like this:

#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

// Timers
unsigned long timer = 0;
float timeStep = 0.01;

// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;

const int button_pin = 5;

void setup() 
{
  Serial.begin(115200);

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

  // Calibrate gyroscope. The calibration must be at rest.
  // If you don't want calibrate, comment this line.
  mpu.calibrateGyro();

  // Set threshold sensivty. Default 3.
  // If you don't want use threshold, comment this line or set 0.
  mpu.setThreshold(3);

  pinmode(button_pin,INPUT);
}

void loop()
{
  int button_state = digitalRead(button_pin);

  timer = millis();

  // Read normalized values
  Vector norm = mpu.readNormalizeGyro();

  // Calculate Pitch, Roll and Yaw
  pitch = pitch + norm.YAxis * timeStep;
  roll = roll + norm.XAxis * timeStep;
  yaw = yaw + norm.ZAxis * timeStep;

  // Output raw
  Serial.print(" Pitch = ");
  Serial.print(pitch);
  Serial.print(" Roll = ");
  Serial.print(roll);  
  Serial.print(" Yaw = ");
  Serial.println(yaw);

  if ((pitch <= 90) && (pitch => 15) && button_state)
  {
      // Code goes here
  }

  // Wait to full timeStep period
  delay((timeStep*1000) - (millis() - timer));
}
0

In the if statement:

if (pitch <=90 && pitch => 15 && button_state =(HIGH))

you assign a new value to button_state, which is illegal.

This should read:

if (pitch <=90 && pitch => 15 && button_state == HIGH)
  • Your first statement is incorrect. The condition in the first if is parsed as (pitch <= 90 && pitch => 15 && button_state) = HIGH (which does not compile), not as pitch <= 90 && pitch => 15 && (button_state = HIGH) (which would compile and is what your answer implies). Hence the compile error “lvalue required as left operand of assignment”. – Edgar Bonet Oct 12 '17 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.