 # Solar position

Hello everyone! Nowadays i’m curios about arduino. However for now i don’t have any arduino board. I have made some calculations on visual c# to find the solar position and i suceeded. Now I’m trying to apply these codes to arduino. I want you to check my code

``````void loop() {
double YSZM, d, B, ZD, ZDF, YSS, YS, SA, EgimA, Alti, Zenit, SunR, SunS, AzimA;
double Lati = 35.85163; // "-" for south
double Longi = 37.64879; // "-" for west
double hour,minute,second; //Localtime
hour=15;
minute=5;
second=54;
d = 315; //Day of The Year
int utcD=2; //GMT
//These values are just sample, these can be get from RTC module

//Begining the calculation...

YSZM = 15 * utcD;

YS = (hour) + (minute/60) + (second/3600); //converting the local time into hour format...

//these are some time correction equations..
B = ((double)360 / 365) * (d - 81);
ZD = (9.87 * SinD(2 * B)) - (7.53 * CosD(B)) - (1.5 * SinD(B));
ZDF = 4 * (Longi - YSZM) + ZD;

YSS = YS + (ZDF / 60); // Local Solar Time (in hours)
SA = 15 * (YSS - 12); //Hour Angle
EgimA = ASinD(SinD(23.45) * SinD(B)); // Declination angle
Alti = ASinD(SinD(EgimA) * SinD(Lati) + CosD(EgimA) * CosD(Lati) * CosD(SA)); //Elevation angle or Altitude... 0(zero) is the horizon. 90 is the top.
Zenit = 90 - Alti; //Zenith angle...

//Azimuth Angle calculation.. 0 is North, 90 is East, 180 is South, 270 is West,
AzimA = ACosD(  ((SinD(EgimA) * CosD(Lati)) + (-CosD(EgimA) * SinD(Lati) * CosD(SA))) / CosD(EgimA));
if (SA > 12)
{
AzimA = 360 - AzimA;
}
SunR = 12 - (double)1 / 15 * ACosD(-TanD(Longi) * TanD(EgimA)) - (double)ZDF / 60; //SUNRISE in hours
SunS = 12 + (double)1 / 15 * ACosD(-TanD(Longi) * TanD(EgimA)) - (double)ZDF / 60; //SUNSET in hours

}

//these are the converted version of sin cos tan acos asin in degrees...
double SinD(double deg){
return sin(deg*Pi/180);
}
double CosD(double deg){
return cos(deg*Pi/180);
}
double ACosD(double deg){
return acos(deg) * 180 / Pi;
}
double ASinD(double deg){
return asin(deg) * 180 / Pi;
}
double TanD(double deg){
return tan(deg*Pi/180);
}
``````

waiting for your suggestions to improve the code. And a question… Can i use Azimuth and elevation angles to point a solar cell with pan tilt servo motors?

Can i use Azimuth and elevation angles to point a solar cell with pan tilt servo motors?

sertaylan: However for now i don't have any arduino board. ... Now I'm trying to apply these codes to arduino.

In case you don't know already: It's completely clueless to adapt these codes to Arduino if you don't have any Arduino board.

sertaylan: I want you to check my code

Check!

If you don't have any Arduino, you cannot run the code on an Arduino.

If you want to run code on Arduino, get yourself an Arduino board. Then you can compare the results against values published on astronomical websites.

Only 32-bit controllers provide real "double" variables, the 8-bit Atmegas take "double" and treat them as "float" only, so a loss of precision is to be expected after 6-7 digits.

Hi,

Arduino supports C++ code, C# is unfortunately not supported ...

As a result, in order to move forward with your project , you need to get an Arduino Nano board or Arduino Mini or Nano module, and you also need to adapt your code to C++.

• dan

sertaylan:
I want you to check my code

The best way to check code is to run it and see what happens. Then if it does not work and you can’t figure out what is wrong you can describe the symptoms and we can try to help.

But first you need an Arduino board.

…R

I'll have a board soon and check what happens. :)) changing doubles to floats. as soon as checking the code i will inform you. thank you everyone...

jurs: In case you don't know already: It's completely clueless to adapt these codes to Arduino if you don't have any Arduino board.

Check!

If you don't have any Arduino, you cannot run the code on an Arduino.

Or, you can use a website that will emulate an Arduino... something like:

https://123d.circuits.io

jurs: If you want to run code on Arduino, get yourself an Arduino board. Then you can compare the results against values published on astronomical websites.

Only 32-bit controllers provide real "double" variables, the 8-bit Atmegas take "double" and treat them as "float" only, so a loss of precision is to be expected after 6-7 digits.

sertaylan: I'll have a board soon and check what happens. :)) changing doubles to floats. as soon as checking the code i will inform you. thank you everyone...

... and you don't have to change your "double" datatypes to "float" The compiler is smart enough to do that for you. In fact, many programmers would advise that you keep them as "double" in order to make your code more portable.

Typically, modern compilers will adapt your int and double datatypes for your specific hardware (i.e. 16bit, 32bit, etc).