Please I need help ASAP
I am reading a DHT11 sensor and I want to display it on my LCd in display mode 1. And when the button is pressed I want to switch to display mode 2 and read another sensor. The code was working yesterday and today it is not. When the button is pressed it reads nonsense values.
I have tried a simplified coed. REbuilding the circuit in case it was nudged. Using serial.print to check the mode and it is in the correct mode just the LCD is not reading the correct values. Anyone who can solve this will loterally save my life.
I have posted the entire code below, and the simplified test code below that
#include <MPU6050.h>
#include "DHT.h"
#define DHTPIN 8
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int h;
int t;
const int buttonPin = 6;
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int inPin = 6;
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW;
unsigned long time = 0; // the last time the output pin was toggled
unsigned long debounce = 200UL;
int Disp = 0;
int outPin = 13;
int T;
int help;
int Help;
#include <Wire.h>
const int MPU = 0x68; // MPU6050 I2C address
float AccX, AccY, AccZ;
float GyroX, GyroY, GyroZ;
float accAngleX, accAngleY, gyroAngleX, gyroAngleY, gyroAngleZ;
float roll, pitch, yaw;
float AccErrorX, AccErrorY, GyroErrorX, GyroErrorY, GyroErrorZ;
float elapsedTime, currentTime, previousTime;
int c = 0;
int AcX;
int AcY;
int AcZ;
int gyrox;
int gyroy;
int gyroz;
const int MPU_ADDR = 0x68;
long previousMillis = 0;
long interval = 1000;
float temperature;
uint8_t WPLg1 = 55;
uint8_t WPLg2 = 51;
uint8_t WPLg3 = 52;
uint8_t WPLg4 = 53;
uint8_t WPLg5 = 54;
uint8_t WPLt1 = 5;
uint8_t WPLt2 = 1;
uint8_t WPLt3 = 2;
uint8_t WPLt4 = 3;
uint8_t WPLt5 = 4;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
dht.begin();
// put your setup code here, to run once:
pinMode(inPin, INPUT);
lcd.clear();
pinMode(outPin, OUTPUT);
// Make reset - place a 0 into the 6B register
Wire.begin();
Wire.beginTransmission(MPU); //begin transmission to I2C slave device
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true); //ends transmission to I2C slave device
calculate_IMU_error();
Wire.beginTransmission(MPU); //begin transmission to I2C slave device
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false); //restarts transmission to I2C slave device
Wire.requestFrom(MPU, 14, true);
delay(20);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
// === Read acceleromter data === //
accSetup();
// Read 6 registers total, each axis value is stored in 2 registers
//For a range of +-2g, we need to divide the raw values by 16384, according to the datasheet
accRaw();
// Calculating Roll and Pitch from the accelerometer data
accAngleX = (atan(AccY / sqrt(pow(AccX, 2) + pow(AccZ, 2))) * 180 / PI) - 0.58; // AccErrorX ~(0.58) See the calculate_IMU_error()custom function for more details
accAngleY = (atan(-1 * AccX / sqrt(pow(AccY, 2) + pow(AccZ, 2))) * 180 / PI) + 1.58; // AccErrorY ~(-1.58)
// === Read gyroscope data === //
gyroSetup();
previousTime = currentTime; // Previous time is stored before the actual time read
currentTime = millis(); // Current time actual time read
elapsedTime = (currentTime - previousTime) / 1000; // Divide by 1000 to get seconds
// Read 4 registers total, each axis value is stored in 2 registers
GyroX = (Wire.read() << 8 | Wire.read()) / 131.0; // For a 250deg/s range we have to divide first the raw value by 131.0, according to the datasheet
GyroY = (Wire.read() << 8 | Wire.read()) / 131.0;
GyroZ = (Wire.read() << 8 | Wire.read()) / 131.0;
// Correct the outputs with the calculated error values
GyroX = GyroX - GyroErrorX; // GyroErrorX ~(-0.56)
GyroY = GyroY - GyroErrorY; // GyroErrorY ~(2)
GyroZ = GyroZ - GyroErrorZ; // GyroErrorZ ~ (-0.8)
// Currently the raw values are in degrees per seconds, deg/s, so we need to multiply by sendonds (s) to get the angle in degrees
gyroAngleX = gyroAngleX + GyroX * elapsedTime; // deg/s * s = deg
gyroAngleY = gyroAngleY + GyroY * elapsedTime;
gyroAngleZ = gyroAngleZ + GyroZ * elapsedTime;
yaw = yaw + GyroZ * elapsedTime;
// Complementary filter - combine acceleromter and gyro angle values
roll = 0.96 * gyroAngleX + 0.04 * accAngleX;
pitch = 0.96 * gyroAngleY + 0.04 * accAngleY;
h = HumSense();
t = TempSense(h);
}
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce)
{
if (state == HIGH) {
state = LOW;
Disp = 0;
}
else {
state = HIGH;
Disp = 1;
}
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
if (Disp == 1)
{
lcd.clear();
TempHum (t);
}
else {
lcd.clear();
printPitchRoll (pitch, roll);
}
if (pitch < 0 && roll >= 0) {
help = 1;
}
else if (pitch >= 0 && roll < 0) {
help = 2;
}
else if (pitch < 0 && roll < 0) {
help = 3;
}
else {
help = 0;
}
uint8_t pitchINT = static_cast<int>(pitch);
uint8_t rollINT = static_cast<int>(roll);
uint8_t T = static_cast<int>(t);
uint8_t Help = static_cast<int>(help);
uint8_t AcX = static_cast<int>(AccX);
uint8_t AcY = static_cast<int>(AccY);
uint8_t AcZ = static_cast<int>(AccZ);
uint8_t gryox = static_cast<int>(gyroAngleX);
uint8_t gryoy = static_cast<int>(gyroAngleY);
uint8_t gryoz = static_cast<int>(gyroAngleZ);
// uint8_t Temperature = static_cast<int>(temperature);
MATLAB( pitchINT, rollINT, Help, Temperature, t, WPLt1, WPLt2, WPLt3, WPLt4, WPLt5, WPLg1, WPLg2, WPLg3, WPLg4, WPLg5);
}
int MATLAB(uint8_t pitchINT,uint8_t rollINT,uint8_t Help,uint8_t Temperature,uint8_t t,uint8_t WPLt1,uint8_t WPLt2, uint8_t WPLt3, uint8_t WPLt4,uint8_t WPLt5, uint8_t WPLg1,uint8_t WPLg2,uint8_t WPLg3, uint8_t WPLg4,uint8_t WPLg5){
Serial.write(3);
Serial.write(19);
Serial.write(0);
Serial.write(2);
Serial.write(4);
Serial.write(pitchINT);
Serial.write(rollINT);
Serial.write(Help);
// Serial.write(Temperature);
Serial.write(t);
Serial.write( WPLt1);
Serial.write( WPLt2);
Serial.write( WPLt3);
Serial.write( WPLt4);
Serial.write( WPLt5);
Serial.write( WPLg1);
Serial.write( WPLg2);
Serial.write( WPLg3);
Serial.write( WPLg4);
Serial.write( WPLg5);
delay(500);
}
int HumSense() {
float h = dht.readHumidity();
return h;
}
int TempSense(int a) {
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
return t;
}
int TempHum (int y) {
lcd.setCursor(0, 0);
lcd.print(F("Atmos Temp: "));
lcd.print(y);
lcd.print(F("C "));
lcd.setCursor(0, 1);
lcd.print("GY521 Temp: ");
// lcd.print(temperature);
}
int printPitchRoll (float pitch, float roll) {
lcd.setCursor(0, 0);
lcd.print("Pitch: ");
lcd.print(pitch);
lcd.setCursor(0, 1);
lcd.print("Roll: ");
lcd.print(roll);
}
int accSetup() {
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);
}
int gyroSetup() {
Wire.beginTransmission(MPU);
Wire.write(0x43); // Gyro data first register address 0x43
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);
}
void accRaw(){
AccX = (Wire.read() << 8 | Wire.read()) / 16384.0; // X-axis value
AccY = (Wire.read() << 8 | Wire.read()) / 16384.0; // Y-axis value
AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0; // Z-axis value
}
void calculate_IMU_error() {
// We can call this funtion in the setup section to calculate the accelerometer and gyro data error. From here we will get the error values used in the above equations printed on the Serial Monitor.
// Note that we should place the IMU flat in order to get the proper values, so that we then can the correct values
// Read accelerometer values 200 times
while (c < 200) {
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);
AccX = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
AccY = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
AccZ = (Wire.read() << 8 | Wire.read()) / 16384.0 ;
// Sum all readings
AccErrorX = AccErrorX + ((atan((AccY) / sqrt(pow((AccX), 2) + pow((AccZ), 2))) * 180 / PI));
AccErrorY = AccErrorY + ((atan(-1 * (AccX) / sqrt(pow((AccY), 2) + pow((AccZ), 2))) * 180 / PI));
c++;
}
//Divide the sum by 200 to get the error value
AccErrorX = AccErrorX / 200;
AccErrorY = AccErrorY / 200;
c = 0;
// Read gyro values 200 times
while (c < 200) {
Wire.beginTransmission(MPU);
Wire.write(0x43);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);
GyroX = Wire.read() << 8 | Wire.read();
GyroY = Wire.read() << 8 | Wire.read();
GyroZ = Wire.read() << 8 | Wire.read();
// Sum all readings
GyroErrorX = GyroErrorX + (GyroX / 131.0);
GyroErrorY = GyroErrorY + (GyroY / 131.0);
GyroErrorZ = GyroErrorZ + (GyroZ / 131.0);
c++;
}
//Divide the sum by 200 to get the error value
GyroErrorX = GyroErrorX / 200;
GyroErrorY = GyroErrorY / 200;
GyroErrorZ = GyroErrorZ / 200;
}
SIMPLIFIED CODE
int inPin = 7;
// the current reading from the input pin
int previous = LOW;
unsigned long time = 0; // the last time the output pin was toggled
unsigned long debounce = 200UL;
int state = HIGH;
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
int reading = digitalRead (inPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce)
{
if (state == HIGH) {
state = LOW;
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
Serial.print(millis() / 1000);
}
else {
state = HIGH;
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print("help");
Serial.print("help");
}
time = millis();
}
previous = reading;
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
}