G Meter in C

Hi guys,
i am still stuck with my g- meter code.

I am using a BNBO05 and reading out the accelerometer, which gives me the momentary G-force. I want to also show the maximum and the minimum values, until it gets reset, so the maximum stress the machine was exposed to is beeing recorded.

This is my code, that for some reason displays onbly the momentary value.

When I hard code the values, it works, i.e, if the maximum G force is higher than the momentary one, it reads the Maximum vlaue. If its the other way around, println prints nothing.

What is going on ?

I certainly appreciate any help,
truely, Johannes

momentGforce = acc.z(); // here the value comes from the sensor (60Hz)
// maxGforce = 9;
// momentGforce = 10;

textAreaGmeter.ClearArea();
textAreaGmeter.print("G");
textAreaGmeter.CursorTo(2);

if ( momentGforce > maxGforce )

{
maxGforce = momentGforce;
textAreaGmeter.println(maxGforce, 1); // reads 10 with the hard coded 10.
} // reads nothing

Post the whole program not just a snippet

UKHeliBob:
Post the whole program not just a snippet

ok, here`s the entire function:

void Gyro() {

//#define GravityArraySize 2
// int GravityArray[GravityArraySize];
// int GravityArrayIndex = 0;
// unsigned long GravityTimer = 0; // time since last altitude display change. if it is less than BufferTimer we dont refresh the display
int iGforce;
int Gforce = 1.0;
int momentGforce=0;
int maxGforce = 0;
float maxPrintForce = 0;
int MAG = 0; // to int
int gRoll, gPitch, yStart, yEnd, xStart, xEnd, xZero = 96, yZero = 26;
int yBug, xBug, xBall ;
int xm60, ym60;
int xm30, ym30;
int xp60, yp60;
int xp30, yp30;
String s_tmp;
float ball;

sensors_event_t event;
bno.getEvent(&event);

MAG = event.orientation.x;
gRoll = event.orientation.y;
gPitch = event.orientation.z;

gPitch = gPitch - 20; // Horizon down
// gRoll = gRoll - 20;

if (abs(gPitch)>90) {
if (gRoll > 0 ) {
gRoll = 180-gRoll;
} else {
gRoll = -180-gRoll;
}
}

if (gPitch>90) {
gPitch = 180 - gPitch;
}

if (gPitch<-90) {
gPitch = -180 - gPitch;
}

textAreaMAG.ClearArea();
textAreaMAG.Printf("%03d", MAG); // print 3 digit HDG

/*
s_tmp = “”;
if (abs(gRoll)<10) {
s_tmp += " ";
}

s_tmp += abs(gRoll);

if (gRoll<0) {
s_tmp += “R”;
}
if (gRoll>0) {
s_tmp += “L”;
}

/*textAreaBottomL.ClearArea();
textAreaBottomL.print(s_tmp); // s-tmp == R or L print “R” or “L”
/
/

s_tmp = “”;
if (abs(gPitch)<10) {
s_tmp += " ";
}

s_tmp += abs(gPitch);

if (gPitch<0) {
s_tmp += “U”;
}
if (gPitch>0) {
s_tmp += “D”;
}
/*
textAreaBottomR.ClearArea();
textAreaBottomR.print(s_tmp); s-temp == U or D print “U” or “D”
*/
//top marker
//GLCD.DrawLine(xZero, 5, xZero, 5); // tip of triangle = point
GLCD.DrawLine(xZero-1, 6, xZero+1, 6); // 2nd row of tiangle static triangle= 2 wide
GLCD.DrawLine(xZero-2, 7, xZero+2, 7); // 3rd row = base of triangle = 4 wide
GLCD.DrawLine(xZero, 5, xZero, 4); // vertiklal pointer line on top 1 wide, 2 high, starts at 5 goes up to 3

//left and right markers
//Left
GLCD.DrawLine(66,yZero-2, 66, yZero+2);
GLCD.DrawLine(67,yZero-1, 67, yZero+1);
GLCD.DrawLine(68,yZero, 68, yZero);
//Right
GLCD.DrawLine(126,yZero-2, 126, yZero+2);
GLCD.DrawLine(125,yZero-1, 125, yZero+1);
GLCD.DrawLine(124,yZero, 124, yZero);

// Plane picture
//tail
GLCD.DrawLine(xZero-18, yZero, xZero+18, yZero);
// wings
GLCD.DrawLine(xZero, 12, xZero, 20);
// body
GLCD.DrawCircle(xZero, yZero, 5);
GLCD.DrawLine(xZero, yZero, xZero, yZero, PIXEL_OFF);

//Arc Bug
xBug = xZero + (yZero)sin(3.14gRoll/180);
if (abs(gRoll)<90) {
yBug = yZero-(yZero)cos(3.14gRoll/180);
} else {
yBug = yZero-(yZero-12)cos(3.14gRoll/180);
}

GLCD.DrawRect(xBugOld-1, yBugOld+1,3,3, PIXEL_OFF);
GLCD.DrawRect(xBug-1, yBug+1,3,3);
xBugOld = xBug;
yBugOld = yBug;

// marker -60
xm60 = xZero + (yZero)sin(3.14(gRoll-60)/180);
if (abs(gRoll-60)<90) {
ym60 = yZero-(yZero)cos(3.14(gRoll-60)/180);
} else {
ym60 = yZero-(yZero-11)cos(3.14(gRoll-60)/180);
}

GLCD.DrawRect(xm60_old, ym60_old+1, 2, 2, PIXEL_OFF);
GLCD.DrawRect(xm60, ym60+1, 2, 2);

xm60_old = xm60;
ym60_old = ym60;

// end of marker -60

// marker -30

xm30 = xZero + (yZero-1)sin(3.14(gRoll-30)/180);
if (abs(gRoll-30)<90) {
ym30 = yZero-(yZero-1)cos(3.14(gRoll-30)/180);
} else {
ym30 = yZero-(yZero-1-11)cos(3.14(gRoll-30)/180);
}
GLCD.DrawRect(xm30_old, ym30_old+1, 2, 2, PIXEL_OFF);
GLCD.DrawRect(xm30, ym30+1, 2, 2);

xm30_old = xm30;
ym30_old = ym30;

// end of marker -30

// marker +60

xp60 = xZero + (yZero)sin(3.14(gRoll+60)/180);
if (abs(gRoll+60)<90) {
yp60 = yZero-(yZero)cos(3.14(gRoll+60)/180);
} else {
yp60 = yZero-(yZero-11)cos(3.14(gRoll+60)/180);
}
GLCD.DrawRect(xp60_old, yp60_old+1, 2, 2, PIXEL_OFF);
GLCD.DrawRect(xp60, yp60+1, 2, 2);

xp60_old = xp60;
yp60_old = yp60;

// end of marker +60

// marker +30

xp30 = xZero + (yZero-1)sin(3.14(gRoll+30)/180);
if (abs(gRoll+30)<90) {
yp30 = yZero-(yZero-1)cos(3.14(gRoll+30)/180);
} else {
yp30 = yZero-(yZero-1-11)cos(3.14(gRoll+30)/180);
}
GLCD.DrawRect(xp30_old, yp30_old+1, 2, 2, PIXEL_OFF);
GLCD.DrawRect(xp30, yp30+1, 2, 2);

xp30_old = xp30;
yp30_old = yp30;

// end of marker +30

// draw the horizon line
xStart = 66;
xEnd = 126;
if (abs(gRoll) < 90) {
yStart = yZero - 30tan(3.14gRoll/180) - yZerosin(3.14gPitch/180);
yEnd = yZero + 30tan(3.14gRoll/180) - yZerosin(3.14gPitch/180);
} else {
yStart = yZero - 30tan(3.14gRoll/180) + yZerosin(3.14gPitch/180);
yEnd = yZero + 30tan(3.14gRoll/180) + yZerosin(3.14gPitch/180);
}

if (yStart < 1 ) {
xStart = xStart + (1-yStart)/tan(3.14*gRoll/180);
yStart = 1;
}

if (yStart > 42 ) {
xStart = xStart + (42-yStart)/tan(3.14*gRoll/180);
yStart = 42;
}

if (yEnd < 1) {
xEnd = xEnd + (1-yEnd)/tan(3.14*gRoll/180);
yEnd = 1;
}

if (yEnd > 42) {
xEnd = xEnd + (42-yEnd)/tan(3.14*gRoll/180);
yEnd = 42;
}

GLCD.DrawLine(xStartOld, yStartOld, xEndOld, yEndOld,PIXEL_OFF);
GLCD.DrawLine(xStart, yStart, xEnd, yEnd);

// end of horizon line

/*
// FillTriangle dont work properly in the current version of the GLCD

if (yStart < yEnd) {
//GLCD.FillTriangle(xStart, yStart, xEnd, yEnd, xStart, yEnd);
GLCD.DrawTriangle(xStart, yStart, xEnd, yEnd, xStart, yEnd); //below
GLCD.DrawTriangle(xStart, yStart-1, xEnd, yEnd-1, xEnd, yStart, PIXEL_OFF); //above
} else {
//GLCD.FillTriangle(xStart, yStart, xEnd, yEnd, xEnd, yStart);
GLCD.DrawTriangle(xStart, yStart, xEnd, yEnd, xEnd, yStart);
GLCD.DrawTriangle(xStart, yStart-1, xEnd, yEnd-1, xStart, yEnd, PIXEL_OFF);
}
*/

xStartOld = xStart;
yStartOld = yStart;
xEndOld = xEnd;
yEndOld = yEnd;

//************************ Slip ball **************************
/*
VECTOR_MAGNETOMETER (values in uT, micro Teslas)
VECTOR_GYROSCOPE (values in rps, radians per second)
VECTOR_EULER (values in Euler angles or ‘degrees’, from 0…359)
VECTOR_ACCELEROMETER (values in m/s^2)
VECTOR_LINEARACCEL (values in m/s^2)
VECTOR_GRAVITY (values in m/s^2)
/
imu::Vector<3> acc = bno.getVector(Adafruit_BNO055::VECTOR_ACCELEROMETER);
/

Serial.print(“X: “);
Serial.print(acc.x());
Serial.print(” Y: “);
Serial.print(acc.y());
Serial.print(” Z: “);
Serial.print(acc.z());
Serial.println(””);

delay(500);
*/
ball = acc.x();

ball = ball * 4.0; // convert to number of pixels and set sensitivity

// set max deflection

if (ball > 26) {
ball = 26;
}
if (ball < -26) {
ball = -26;
}

xBall = xZero - ball;

GLCD.FillCircle(xBallOld, 58, 3, PIXEL_OFF);
GLCD.FillCircle(xBall, 58, 3);
xBallOld = xBall;

//GLCD.DrawVLine(96, 53, 10);
GLCD.DrawVLine(91, 53, 10);
GLCD.DrawVLine(101, 53, 10);

// ************************** END OF Slipball stuff *******************************

// ****************** G-Force meter ******************************
/*
Gforce = acc.z();
iGforce = round(Gforce*10/9.8);

textAreaGmeter.ClearArea();
textAreaGmeter.print(“G”);
textAreaGmeter.CursorTo(2);
textAreaGmeter.println(iGforce/10.0, 1);
*/

momentGforce = acc.z();
//maxGforce = 10;
//momentGforce = 9;

textAreaGmeter.ClearArea();
textAreaGmeter.print(“G”);
textAreaGmeter.CursorTo(2);

if ( momentGforce > maxGforce )
{
maxGforce = momentGforce;
textAreaGmeter.println(maxGforce, 1);
}

/* {
maxGforce = momentGforce; // only if momentG > than maxG, maxG gets new value
maxPrintForce = round(maxGforce*10/9.8);
textAreaGmeter.ClearArea();
textAreaGmeter.print(“G”);
textAreaGmeter.CursorTo(2);
textAreaGmeter.println(maxPrintForce/10.0, 1);
} */

}

No, post the whole code without smileys.

void Gyro() {
  int maxGforce = 0;

Your variable is local to the Gyro() function and you are initializing it to 0 each time you enter the function.

Perhaps "static int maxGforce = 0;" will produce better results. That way 'maxGforce' will be initialized to 0 when the program starts and will maintain its value between invocations of Gyro().

Hi Johnwasser ,

That was a very good idea, I did that, I also tried to place the static int maxGforce , befor the Arduinos void loop, but it didn`t work.

First nothing got printed, except the "G".
Obviously the if command did not execute. When I hard coded a value for maxGforce it printed it.

Next I tried a do while loop. It did execute, but only prints the actual G value momentGforce.

momentGforce = acc.z();
//maxGforce = 10;
//momentGforce = 9;

textAreaGmeter.ClearArea();
textAreaGmeter.print("G");
textAreaGmeter.CursorTo(2);

do
{
maxGforce = momentGforce;
textAreaGmeter.println(maxGforce, 1);
}
while (momentGforce>maxGforce);

This above code produced the momentary G value.

The one with the if loop produced nothing, except the "G".

In both cases I used the static int.

momentGforce = acc.z();
//maxGforce = 10;
//momentGforce = 9;

textAreaGmeter.ClearArea();
textAreaGmeter.print("G");
textAreaGmeter.CursorTo(2);

if ( momentGforce > maxGforce )
{
maxGforce = momentGforce;
textAreaGmeter.println(maxGforce, 1);
}

Can you think of something to try ?
Would it make sense to use an eeprom function to temporary store a value ?

Truely Johannes

To make it easy for people to help you please modify your posts and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

…R

Hallo guys,
I finally got it solved !

The first mistake was not using a static int.
After correcting for that, there was a formatting mistake, using println( int, 1)
I changed that to print( int) and it worked immediately.

Special thanks to Johnwasser !
truely Johannes