For whatever reason, I can't seem to figure out what the problem is with the code below. It's my first time programming in Arduino... perhaps I don't have the syntax correct for the if statement. Anyway, this is a segment of code from a sun tracking system I'm working on.
//Calculate True Solar Time
double TST = fmod(1440*6/24+EOT+4*lng-60*timez,1440);
//Calculate Hour Angle based on TST conditions
if ((TST/4) < 0)
{
double hAngle = (180 + (TST/4));
}
else
{
double hAngle = (180 - (TST/4));
}
//Calculate Zenith
//double Z = acos(sin(degToRad(lat))*sin(degToRad(sdec))+cos(degToRad(lat))*cos(degToRad(sdec))*cos(degToRad(HA)));
Serial.println(hAngle,DEC);
The code prior to this segment & up to the variable TST returns values as I would expect them when I test it using the serial monitor. When I try to print the hour angle "hAngle" I get the error: "'hAngle' was not declared in this scope". I'm stumped... didn't I define hAngle correctly in my if/else statement?
Thanks for any help... I'm sure this is a very simple mistake.
So... today I learned that the variable used within the if statement is only accessible within that same if statement.
If I want to use a condition (in this case, if variable TST < 0 or >= 0) to change a second variable (in this case, hAngle)... is there a more appropriate way to structure the code? It seems I either need to declare my variables differently or find a different way to handle this process.
Edit: ... or do I just have to move all of the subsequent code which uses hAngle into the if statement?
is there a more appropriate way to structure the code? It seems I either need to declare my variables differently or find a different way to handle this process.
Edit: ... or do I just have to move all of the subsequent code which uses hAngle into the if statement?
Declare the variable first. It's scope is still limited to the function it is defined in, but not as limited as a block-scoped varaible.
Yes, define your variable types before the
void setup()
section of code.
Otherwise, its like declaring a new variable every time you write
double hAngle = ....
same with TST, etc,.
double hAngle = 0;
at the top of the sketch before
void setup()
//Calculate True Solar Time
double TST = fmod(1440*6/24+EOT+4*lng-60*timez,1440);
double hAngle;
//Calculate Hour Angle based on TST conditions
if ((TST/4) < 0)
{
hAngle = (180 + (TST/4));
}
else
{
hAngle = (180 - (TST/4));
}
//Calculate Zenith
//double Z = acos(sin(degToRad(lat))*sin(degToRad(sdec))+cos(degToRad(lat))*cos(degToRad(sdec))*cos(degToRad(HA)));
Serial.println(hAngle,DEC);
With all due respect to CrossRoads, you don't need to be as radical as declaring your hAngle variable before your setup() function. Anytime you declare a variable between { } the scope is limited to within that bracketed code. The goal is to declare your variables so that have as much scope as they need but no more. Global variables are evil. Arduino kind of forces them in some cases, but this is not such a case.
Well, that just shows my software ignorance. To be fair tho, I've had cases where my code didn't do what I wanted because I accidentally redeclared it and it lost its value on me.
So it depends how you are using them.