Garmaine Staff asked 2 years ago

I am building a thermostat with arduino mega 2560.I would like to adjust the desired temperature and present it at two seperate seven segment displays. My problem is that it doesn't work.I think the problem is that when I call the function.I would like someone to help me with this project please. Here is my void loop code

void loop() {
  buttonStateP = digitalRead(buttonPinP);

  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
  Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
  Wire.requestFrom(MPU_ADDR, 7*2, true); // request a total of 7*2=14 registers

  // "Wire.read()<<8 | Wire.read();" means two registers are read and stored in the same variable
  accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
  accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
  accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
  temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
  gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
  gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
  gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)





  // print out data
  //Serial.print("aX = "); Serial.print(convert_int16_to_str(accelerometer_x));
 // Serial.print(" | aY = "); Serial.print(convert_int16_to_str(accelerometer_y));
 // Serial.print(" | aZ = "); Serial.print(convert_int16_to_str(accelerometer_z));
  // the following equation was taken from the documentation [MPU-6000/MPU-6050 Register Map and Description, p.30]
  //Serial.print(" | tmp = "); Serial.println(temperature/340.00+36.53);
 // Serial.print(" |      |"); Serial.print(counter);
 // Serial.print(" | gX = "); Serial.print(convert_int16_to_str(gyro_x));
 // Serial.print(" | gY = "); Serial.print(convert_int16_to_str(gyro_y));
 // Serial.print(" | gZ = "); Serial.print(convert_int16_to_str(gyro_z));
 // Serial.println();
  // delay
 delay(1000);

  // compare the buttonState to its previous state
  if (buttonStateP != lastButtonStateP) {
    // if the state has changed, increment the counter
    if (buttonStateP == LOW) {
      // if the current state is HIGH then the button went from off to on:
      bPress = true;
      buttonPushCounterP++;
      if( buttonPushCounterP > 99) buttonPushCounterP =0 ;
      Serial.println("buttonPushCounterP");

    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
 // save the current state as the last state, for next time through the loop
  lastButtonStateP = buttonStateP;

  if( bPress ){
     turnOff();

  }


if (buttonPushCounterP = 0){

  zero1();
  zero2();
  }





dt = temperature;
 if (buttonPushCounterP < dt){
  analogWrite(ledON,1000);
}
 else if(buttonPushCounterP > dt){
  analogWrite(ledON,0);
}

}
void turnOff()
{
  digitalWrite(Aa,LOW);
  digitalWrite(Ba,LOW);
  digitalWrite(C1,LOW);
  digitalWrite(D1,LOW);
  digitalWrite(E1,LOW);
  digitalWrite(F1,LOW);
  digitalWrite(G1,LOW);

  digitalWrite(Ab,LOW);
  digitalWrite(Bb,LOW);
  digitalWrite(C2,LOW);
  digitalWrite(D2,LOW);
  digitalWrite(E2,LOW);
  digitalWrite(F2,LOW);
  digitalWrite(G2,LOW);
}```