While polling a INA419 I get updates of the power_mW data.
I am trying to get a variable (maxw) to store the highest number returned.
if (ct == 3)
{short maxw;
if (power_mW < maxw)
{
maxw = power_mW;
Panel.SetText("max1", maxw);
}
}
Simply if the number returned (power_mW) is bigger than what is stored in maxw
then update maxw to hold the larger number.
thats it, simple, but it doesnt work, and I cant see why.
The last line just prints the result in a box on my GUI.
Mike
What is the scope of maxw?
Please remember to use code tags when posting code.
but it doesnt work
Please rest assured that the code absolutely does work.
Almost certainly the problem is in the parts you didn't post.
Please do go and read the sticky and follow the instructions on how to ask a question in a way that you can actually get help.
Your code reads if power_mW is less than maxw.
Maybe try using the greater symbol?
Change
{short maxw;
to
short maxw;
{
and there'll be a world of difference.
Scope matters. Big time.
Scope matters. Big time.
The value in maxw will NOT change because you moved the declaration.
Ah, it's just a single run. Wasn't looking carefully enough.
Oh well. That's the fun of working with snippets.
Sorry for the incorrect original post. As for being just a snippet, I have included the entire function below.
Far easier than typing it all in.
short CellTest(int mux ,int sen ) // function to test each INA219 data and then return.
{
tcaselect(mux);
ina219[sen].begin();
shuntvoltage = ina219[sen].getShuntVoltage_mV();
busvoltage = ina219[sen].getBusVoltage_V();
current_mA = ina219[sen].getCurrent_mA();
power_mW = ina219[sen].getPower_mW();
// subroutine to pString setup (Data) for the 20 boxes.
Data = "Box";
Data += ct;
int wk = (shuntvoltage+6000); //added 6000 to avoid messing with neg numbers when discharging.
if (ct == 3)// using Cell 3 as an test example.
{short maxw;
if (power_mW > maxw) //if the newly read data is higher than the stored data update
{ // the holding variable "maxw". Then as this test is "True"
maxw = power_mW; //update the GUI with the new total.
Panel.SetText("max1", maxw); // If the test proves "False" skip the bracket routine.
}
}
// Second subroutine Start.
if (wk > 6005)
{
Panel.SetBackColor(buffer, "Red");
}
else if (wk < 5995)
{
Panel.SetBackColor(buffer, "Yellow");
}
else
{
Panel.SetBackColor(buffer, "Lime");
}
// Subroutine end.
return;
Serial.println("if you see this, return did not work");
}
Now we get just the subroutine... oh well.
You need to declare maxw static so it maintains its value when it goes out of scope. Or skip the learning opportunity, get lazy and just make it a global.
On a different note, this line at the end:
Serial.println("if you see this, return did not work");
Should be eliminated the optimizer. Might get “unreachable code” warning. Or not. Complete waste of ascii characters and a really bad habit. Just sayin’.
WattsThat:
Now we get just the subroutine... oh well.
You need to declare maxw static so it maintains its value when it goes out of scope. Or skip the learning opportunity, get lazy and just make it a global.
On a different note, this line at the end:
Serial.println("if you see this, return did not work");
Should be eliminated the optimizer. Might get “unreachable code” warning. Or not. Complete waste of ascii characters and a really bad habit. Just sayin’.
Did you really want the whole 435 line sketch ?
Thanks for the static tip, I would rather include all the learning I can. That's why I come here, to learn.
I havent programmed in over 38 years, and that was in basic.
This project is my first try at "Arduino" speak. Which i had not ever heard of untill 6 months ago.
Lastly, that "checkline" will not exsist in the final cut. Its just a reminant of an issue I had 3-4 months ago.
Mike.
Please do an autoformat (ctrl-T in the IDE) before posting, makes it all a lot more readable.
It's also still just a snippet.
Gremlin460:
Did you really want the whole 435 line sketch ?
Yes.
Because often enough the problem is in the part you didn't post. Wrongly declared global variables are a typical one.