Yes, you can use C code in Arduino programming. In fact, Arduino sketches (the programs you write for Arduino) are primarily written in a subset of C/C++.
Here’s how it works:
1. **Arduino Sketches**: Arduino sketches are written in a simplified version of C/C++. They use a set of pre-defined functions like `setup()` and `loop()`, which are specific to the Arduino environment. The core libraries provided by Arduino simplify many tasks, so you don’t need to deal with some of the more complex aspects of C/C++ programming.
2. **C/C++ Code**: You can include C/C++ code in your Arduino sketches by using the `#include` directive to include C/C++ libraries. You can also write your own C/C++ functions and include them in your sketches. For example, you might create a separate `.cpp` file for more complex logic and include it in your Arduino sketch.
3. **Libraries**: Many Arduino libraries are written in C/C++ and can be used directly in your sketches. These libraries provide additional functionality and are easy to include using the Arduino IDE’s library manager.
4. **Using C/C++ Files**: If you have specific C/C++ code you want to use, you can place it in a `.cpp` file (for C++) and a corresponding header `.h` file. Then, you can include these files in your Arduino sketch just like you would in a standard C/C++ project.
Here’s a basic example:
```cpp
// MyLibrary.h
#ifndef MYLIBRARY_H
#define MYLIBRARY_H
void myFunction();
#endif
// MyLibrary.cpp
#include "MyLibrary.h"
#include <Arduino.h>
void myFunction() {
Serial.println("Hello from myFunction!");
}
// main.ino (Arduino Sketch)
#include <MyLibrary.h>
void setup() {
Serial.begin(9600);
myFunction();
}
void loop() {
// Your code here
}
```
In this example:
- `MyLibrary.h` is a header file declaring the function.
- `MyLibrary.cpp` contains the implementation of the function.
- `main.ino` is the Arduino sketch that includes the library and uses the function.
So, feel free to use and write C/C++ code as needed for your Arduino projects!