1

I have two files in Arduino IDE. One is the .ino file and one is a .c file.

main.ino:

#include "somefile.c"

void setup(){
    Serial.begin(9600);
    // Do something
}

void loop(){
    // Do something
}

And in somefile.c I want to call Serial.print(). How can I do that? Thanks!

2
  • wrap Serial.print in a function in some cpp or in ino and then call that function from C Commented Mar 15, 2021 at 6:22
  • Can you show me how to do it more clearly??? Commented Mar 15, 2021 at 11:14

2 Answers 2

6

create a my_logging.h file with

void my_log(const char *msg);

create a my_logging.cpp file with

#include <Arduino.h>
extern "C" {
  #include "my_logging.h"
}

void my_log(const char *msg) {
  Serial.println(msg);
}

then in in your c file include the my_logging.h file and you can use the my_log function

Sign up to request clarification or add additional context in comments.

1 Comment

you can also add void my_log(int msg); if you want to print numbers
-3

You don't need to warp anything. Just use Serial.begin(somebaudhere) and then in the somefile.c you need to #include <Arduino.h>. It will work fine.

1 Comment

really? C++ object in C file?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.