hello,
im struggling with the code in this certain part of my project. i wrote a program that simply converts acceleration data into g force using a accelerometer. when a button is pressed, it displays your current maximum and minimum values youve reached for each axis. for some reason when the minimum Y axis meter and the maximum Z axis meter are displayed, some random (to me) number is shown (pictured below). The weird part is that it works perfectly for the x min and max values, y max values, and z min values and the code is the exact same.
ive tried isolating the min/max meter section of the program and following the logic of the math and programming as closely as i can to no avail. im thinking im just overlooking something silly and need a fresh pair of eyes to have a look. or im stoopid. still figuring it out.
thanks in advance and sorry for the unorganized code.
//#include <LCD_I2C.h>
//#include "Timer.h"
#include <LiquidCrystal.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
//Timer timer;
float maxX; //delcare maxX global var
float minX;
float maxY;
float minY;
float maxZ;
float minZ;
long starttime;
//int sec = (millis() / 1000);
//float min;
//void timer() { //trying to make a laptimer
//++sec;
//delay(1000);
//lcd.setCursor(12,1);lcd.print(sec);
//sec = 0;
// lcd.setCursor(12,1);lcd.print(millis() / 1000);
//}
int maxval1() { // displays max and min values of all axis's
lcd.clear();lcd.setCursor(0,0);lcd.print(maxX);lcd.print(",");lcd.print(minX);
lcd.setCursor(0,1);lcd.print(maxY);lcd.print(",");lcd.print(yG);
lcd.setCursor(11,0);lcd.print(maxZ);lcd.print(",");
lcd.setCursor(11,1);lcd.print(minZ);
}
void setup(void) {
lcd.begin(16, 2);
//lcd.backlight();
Serial.begin(9600);
while (!Serial) delay(10);
Serial.print("LIS3DH test!");
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address
Serial.println("Couldnt start");
while (1) yield();
}
Serial.println("LIS3DH found!");
// lis.setRange(LIS3DH_RANGE_4_G); // 2, 4, 8 or 16 G!
Serial.print("Range = "); Serial.print(2 << lis.getRange());
Serial.println("G");
// lis.setDataRate(LIS3DH_DATARATE_50_HZ);
Serial.print("Data rate set to: ");
switch (lis.getDataRate()) {
case LIS3DH_DATARATE_1_HZ: Serial.println("1 Hz"); break;
case LIS3DH_DATARATE_10_HZ: Serial.println("10 Hz"); break;
case LIS3DH_DATARATE_25_HZ: Serial.println("25 Hz"); break;
case LIS3DH_DATARATE_50_HZ: Serial.println("50 Hz"); break;
case LIS3DH_DATARATE_100_HZ: Serial.println("100 Hz"); break;
case LIS3DH_DATARATE_200_HZ: Serial.println("200 Hz"); break;
case LIS3DH_DATARATE_400_HZ: Serial.println("400 Hz"); break;
case LIS3DH_DATARATE_POWERDOWN: Serial.println("Powered Down"); break;
case LIS3DH_DATARATE_LOWPOWER_5KHZ: Serial.println("5 Khz Low Power"); break;
case LIS3DH_DATARATE_LOWPOWER_1K6HZ: Serial.println("16 Khz Low Power"); break;
}
//sensors_event_t event;
//lis.getEvent(&event);
//static float curposx = event.acceleration.x/9.80665;
//Serial.print(curposx);
lcd.print("G FORCE SENSOR");
lcd.setCursor(0, -1); lcd.print("BY AHAD");
delay(1500);
lcd.clear();
}
void loop() {
lis.read();
sensors_event_t event;
lis.getEvent(&event);
float xG = event.acceleration.x/9.80665; // x axis value in Gs
float yG = event.acceleration.y/9.80665;
float zG = event.acceleration.z/9.80665;
float G = 1; // Compensation for gravity
//static float curposx = event.acceleration.x/9.80665; //saves initial position for x in static var
//static float curposy = event.acceleration.y/9.80665;
//static float curposz = event.acceleration.z/9.80665;
//Serial.print(curposx); Serial.print(" "); Serial.print(curposy); Serial.print(" "); Serial.print(curposz); // for debugging
//lcd.setCursor(0,0);lcd.print("X:");lcd.print(xG - curposx);lcd.print(" "); // x axis data offset by initial position so accelerometer doesnt have to be level or have any certain orientain
//lcd.setCursor(0,-1);lcd.print("Y:");lcd.print(yG - curposy);lcd.print(" "); //adding spaces cuz i cant code
//lcd.setCursor(8,0);lcd.print("Z:");lcd.print(zG - curposz + G);lcd.print(" "); // offset with inital position and then add 1G to compensate for gravity that we initially subtracted
if (xG > maxX) { // if the x axis in Gs is greater than the maxX variable value then replace maxX with the value of xG
maxX = xG;
}
if (xG < minX) {
minX = xG;
}
if (yG > maxY) { // if the x axis in Gs is greater than the maxX variable value then replace maxX with the value of xG
maxY = yG;
}
if (yG < minY) {
minY = yG;
}
if (zG > maxZ) { // if the x axis in Gs is greater than the maxX variable value then replace maxX with the value of xG
maxZ = zG;
}
if (zG < minZ) {
minZ = zG + G;
}
//static int status1 = false; // reads max val button status starting with setting it to false
// static int status2 = false;
//int val=analogRead(A0); //reads a0 pin for input from pins
//Serial.println(val); // for debugging , display what buttons being pressed
//if(val>=150&&val<=300) {
//status1 = !status1; // if down button is pressed switch status
//Serial.print(status1); //debugging
//}
//if(val>=0&&val<=50) {
//starttime = millis();
//status2 = !status2; // if right button is pressed switch status of status2
//Serial.print(status2); //debugging
//}
//if (status1 == true) { // if down button is on then run maxval1 function
//sensors_event_t event;
//lis.getEvent(&event);
maxval1();
//}
//if (status2 == true) { // if right button is on then start timer
// lcd.setCursor(12,1);lcd.print((millis() - starttime) / 1000.0);
//static int sec = timer.read();
//timer.start();lcd.setCursor(12,1);Serial.print(sec / 1000);
//timer();
//lcd.setCursor(12,1);lcd.print(millis() / 1000);
// }
Serial.println();
delay(250);
}