Translate C code - Help Needed

Hi Guys, I am new to the Arduino community and I trying use the below C code on my sketch. I added the library but I can’t translate the C code to use on Arduino.

Your help would be highly appreciated. Thanks in advance

#include <stdio.h>
#include "spa.h"  //include the SPA header file

int main (int argc, char *argv[])
{
    spa_data spa;  //declare the SPA structure
    int result;
    float min, sec;

    //enter required input values into SPA structure

    spa.year          = 2003;
    spa.month         = 10;
    spa.day           = 17;
    spa.hour          = 12;
    spa.minute        = 30;
    spa.second        = 30;
    spa.timezone      = -7.0;
    spa.delta_t       = 67;
    spa.longitude     = -105.1786;
    spa.latitude      = 39.742476;
    spa.elevation     = 1830.14;
    spa.pressure      = 820;
    spa.temperature   = 11;
    spa.slope         = 30;
    spa.azm_rotation  = -10;
    spa.atmos_refract = 0.5667;
    spa.function      = SPA_ALL;

    //call the SPA calculate function and pass the SPA structure

    result = spa_calculate(&spa);

    if (result == 0)  //check for SPA errors
    {
        //display the results inside the SPA structure

        printf("Julian Day:    %.6f\n",spa.jd);
        printf("L:             %.6e degrees\n",spa.l);
        printf("B:             %.6e degrees\n",spa.b);
        printf("R:             %.6f AU\n",spa.r);
        printf("H:             %.6f degrees\n",spa.h);
        printf("Delta Psi:     %.6e degrees\n",spa.del_psi);
        printf("Delta Epsilon: %.6e degrees\n",spa.del_epsilon);
        printf("Epsilon:       %.6f degrees\n",spa.epsilon);
        printf("Zenith:        %.6f degrees\n",spa.zenith);
        printf("Azimuth:       %.6f degrees\n",spa.azimuth);
        printf("Incidence:     %.6f degrees\n",spa.incidence);

        min = 60.0*(spa.sunrise - (int)(spa.sunrise));
        sec = 60.0*(min - (int)min);
        printf("Sunrise:       %02d:%02d:%02d Local Time\n", (int)(spa.sunrise), (int)min, (int)sec);

        min = 60.0*(spa.sunset - (int)(spa.sunset));
        sec = 60.0*(min - (int)min);
        printf("Sunset:        %02d:%02d:%02d Local Time\n", (int)(spa.sunset), (int)min, (int)sec);

    } else printf("SPA Error Code: %d\n", result);

    return 0;
}

#include <stdio.h>
#include “spa.h” //include the SPA header file

Get rid of this whole thing
int main (int argc, char *argv[]) ~~
{ <= this too
~~ spa_data spa; //declare the SPA structure

~~ int result;~~
~~ float min, sec;~~

spa_data spa; //declare the SPA structure
int result;
float min, sec;

void setup()
{
// if you need to set up any pins or other configurations
}

void loop()
{

//enter required input values into SPA structure

spa.year = 2003;
spa.month = 10;
spa.day = 17;
spa.hour = 12;
//… rest of code below

One more thing I need to point out, printf, if you want to use the arduino Serial monitor, then in void setup(), add this line Serial.begin(9600) or what ever baud rate your want to use. And change all the “printf” to either Serial.print or Serial.println. Serial.print prints everything horizontally whereas println make everything vertically. ( new line)

Guys, please use [ code ] tags around your snippets, not plain text and not quote.

OP: You will also need to go through the .h file, and any files it includes. Drop stdio.h from the includes, everything you need is included already.

BTW, "Arduino" is C, so you're pretty close to having this work already. The only difference is that main() is written for you and calls setup() and loop() on your behalf, and some of the PC console functions like printf() are replaced with functions that apply to the hardware present on a microcontroller.

Thanks HazardsMind and SirNickity for your kind help and direction.

I followed HazardsMind code and now I have an error , I can’t “call the SPA calculate function and pass the SPA structure”

and all results are equal zero.

Below is the error and the code , how can I call the SPA calculation function from the library ?

Error : sketch_jan01a.cpp.o: In function loop': F:\ardiuno\arduino-1.0.5-windows\arduino-1.0.5/sketch_jan01a.ino:36: undefined reference to spa_calculate(spa_data*)’

#include <spa.h>

  
  spa_data spa;  //declare the SPA structure
  int result; 
  float min, sec;

void setup()
{
  Serial.begin(9600);
 // if you need to set up any pins or other configurations
}

void loop()
{
    spa.year          = 2003;
    spa.month         = 10;
    spa.day           = 17;
    spa.hour          = 12;
    spa.minute        = 30;
    spa.second        = 30;
    spa.timezone      = -7.0;
    spa.delta_t       = 67;
    spa.longitude     = -105.1786;
    spa.latitude      = 39.742476;
    spa.elevation     = 1830.14;
    spa.pressure      = 820;
    spa.temperature   = 11;
    spa.slope         = 30;
    spa.azm_rotation  = -10;
    spa.atmos_refract = 0.5667;
    spa.function      = SPA_ALL;

    //call the SPA calculate function and pass the SPA structure

result = spa_calculate(&spa);


    if (result == 0)  //check for SPA errors
    {
        //display the results inside the SPA structure

        Serial.print ("Julian Day:     ");
        Serial.println(spa.jd);
        Serial.print ("L:             ");
        Serial.println(spa.l);
        Serial.print ("B:               ");
        Serial.println(spa.b);
        Serial.print ("R:            ");
        Serial.println(spa.r);
        Serial.print ("H:            ");
        Serial.println(spa.h);
        Serial.print ("Delta Psi:    ");
        Serial.println(spa.del_psi);
        Serial.print ("Delta Epsilon:          ");
        Serial.println(spa.del_epsilon);
        Serial.print ("Epsilon:         ");
        Serial.println(spa.epsilon);
        Serial.print ("Zenith:        ");
        Serial.println(spa.zenith);
        Serial.print ("Azimuth:        ");
        Serial.println(spa.azimuth);
        Serial.print ("Incidence:      ");
        Serial.println(spa.incidence);

        

    } else Serial.print ("SPA Error Code: " );
       Serial.println(result);
    }

how can I call the SPA calculation function from the library ?

The message that you saw:

Error : sketch_jan01a.cpp.o: In function loop': F:\ardiuno\arduino-1.0.5-windows\arduino-1.0.5/sketch_jan01a.ino:36: undefined reference to spa_calculate(spa_data*)'

is telling you that the compiler can't find a function called spa_calculate() that takes a pointer to a spa_data object. Since you haven't posted the spa header file or the spa source file, you will have to look at them to determine exactly what functions, taking exactly what kinds of arguments, are available for you to use.

Thanks PaulS for your reply.
but I still can’t find a way to run this sketch on Arduino or call the SPA calculation function from the library.
I attached the header and the source code as well as the tester.
Is there is a way to use this SPA C codes on Arduino ??

spa.c (34.9 KB)

spa.h (9.06 KB)

spa_tester.c (3.74 KB)

#include spa.h in your ino file

Cheers, Jarkko

You are going to need to modify the header file to support calling C functions from a CPP file. Or, you are going to need to rename the .c file to .cpp.

The reason is that C++ supports operator overloading, and C does not. C++ supports operator overloading by using name-mangling. That is, the actual function name is made up the specified function name and the types of the arguments that the function accepts.

When the C code was compiled, the name was not mangled. The C++ compiler can then not find the mangled name in it’s lookup table.

Either modify the header file to turn off name mangling of functions in the header file, or use the C++ compiler to compile the source code (to mangle the names). Renaming the source file is easier.

Modifying the header file involves adding:

#ifdef __cplusplus
extern "C" {
#endif
  // The functions whose names are not to be mangled
#ifdef __cplusplus
}
#endif

Thanks alot PaulS :smiley:
you are a genius . It is solved and I'm moving to the next step

Hi.
I'm trying to port that code too but I don't understand the solution provided.
Egymeda, can you post the fixed library?

Thank you very much

Hello

I also try to use the code from the spa.c file. I tryed the renaming the file to .cpp method but now I get:

Sketch uses 34,964 bytes (121%) of program storage space. Maximum is 28,672 bytes.
Global variables use 9,682 bytes (378%) of dynamic memory, leaving -7,122 bytes for local variables. Maximum is 2,560 bytes.

Has anyone successfully used the code and can help me how to run it on a Yun?

Thank you so much