Questions tagged [c++]

C++ is the standard language used to program the Arduino IDE. However, the Arduino IDE does have lots of libraries built in, so functions such as "main" are not called directly in the sketch code. Most Arduino code online is written in this language.

C++ is the standard language used to program the Arduino IDE. Most Arduino code online is written in this language.

C++ is one of the most popular coding languages available, being highly cross-platform. However, transferring Arduino code to PC code (and vice versa) is very hard, even when ignoring the fact that your computer can't run a motor. For example, in C++ code, the first thing to be called is main. In Arduino C++, main is still called, however, the Arduino libraries are called, not your sketch. Those libraries then call setup and then loop repeatedly after finishing all the initialization tasks they must perform before the Arduino can run the code of your sketch.

It is pronounced "C plus plus."

1071 questions
104
votes
9 answers

C++ vs. The Arduino Language?

What are the advantages of each language when using the Arduino? I'm thinking this is a good general question, but I'll add a bit about why I'm asking if anyone wants to give me a tip. I'm experienced in preprocessed languages like JavaScript, PHP,…
Friend of Kim
  • 1,543
  • 3
  • 16
  • 16
94
votes
22 answers

What are the other IDEs for Arduino?

The basic Arduino IDE lacks a lot of the sophistication present in other IDEs such as code completion, code collapsing, folder organisation, etc. Are there other IDEs that allow programming in C or C++ and improve on these aspects?
DLJ
  • 1,149
  • 1
  • 10
  • 10
67
votes
2 answers

How do you use SPI on an Arduino?

With reference to the Arduino Uno, Mega2560, Leonardo and similar boards: How does SPI work? How fast is SPI? How do I connect between a master and a slave? How do I make an SPI slave? Please note: This is intended as a reference question.
Nick Gammon
  • 38,901
  • 13
  • 69
  • 125
32
votes
4 answers

Is it better to use #define or const int for constants?

Arduino is an odd hybrid, where some C++ functionality is used in the embedded world—traditionally a C environment. Indeed, a lot of Arduino code is very C like though. C has traditionally used #defines for constants. There are a number of reasons…
Cybergibbons
  • 5,420
  • 7
  • 34
  • 51
22
votes
5 answers

What overheads and other considerations are there when using a struct vs a class?

C on embedded systems has traditionally use structs to hold structured data. Arduino brings C++ to the table, so we can use classes instead. Lets say we have two different data structures which could be considered very similar: typedef struct { …
Cybergibbons
  • 5,420
  • 7
  • 34
  • 51
21
votes
1 answer

How does serial communications work on the Arduino?

With reference to the Arduino Uno, Mega2560, Leonardo and similar boards: How does serial communications work? How fast is serial? How do I connect between a sender and receiver? Please note: This is intended as a reference question.
Nick Gammon
  • 38,901
  • 13
  • 69
  • 125
21
votes
3 answers

Classes and objects: how many and which file types do I actually need to use them?

I have no previous experience with C++ or C, but know how to program C# and am learning Arduino. I just want to organize my sketches and am quite comfortable with the Arduino language even with its limitations, but I really would like to have an…
heltonbiker
  • 503
  • 7
  • 21
18
votes
1 answer

How do interrupts work on the Arduino Uno and similar boards?

Please explain how interrupts work on the Arduino Uno and related boards using the ATmega328P processor. Boards such as the: Uno Mini Nano Pro Mini Lilypad In particular please discuss: What to use interrupts for How to write an Interrupt Service…
Nick Gammon
  • 38,901
  • 13
  • 69
  • 125
17
votes
1 answer

How do I convert a float into char*?

Asked this question on Electrical Engineering Stack Exchange and was directed here: Pretty self explanatory; how would I go about converting a float into a char*? I have code in which the float 'resistance' is constantly being updated. I would like…
higgsboson
  • 173
  • 2
  • 2
  • 6
14
votes
1 answer

Why can't my IF statement see a global?

I'm rather new to Arduino programming. I have a problem compiling the following bit of code: const int relay1 = 10; //Power Relay 1 const int relay2 = 11; //Power Relay 2 const int relay3 = 12; //Toggle Relay const int button1 = 3; const int…
MikeB
  • 243
  • 1
  • 5
14
votes
3 answers

Returning an int array from a function

I have a function that needs to return 3 int values. I currently use an array declared in the global scope and I ask the function to write into them. Is there a way to make a function return an array? Here is what I want to do: int function() { int…
Dat Ha
  • 2,943
  • 6
  • 24
  • 46
13
votes
4 answers

Is setup and loop provided for convenience?

Arduino sketches usually feature a setup and loop function. Are these functions only provided for convenience or do they actually have special purposes? (e.g.: are some operations disallowed or allowed in setup and loop) Are these two pieces of code…
Sebazzz
  • 231
  • 2
  • 6
13
votes
2 answers

Will a .ino Arduino Sketch compile directly on GCC-AVR?

Okay, we all have seen those questions all over the web such as Arduino v.s. C++, or other similar questions. And a vast majority of the answers do not even touch compilation differences other than through abstracted information. My question aims to…
RedDogAlpha
  • 190
  • 1
  • 1
  • 9
12
votes
1 answer

Which C++ standard does the Arduino language support?

The "Arduino language" (as they call it) of the Arduino IDE 1 is obviously C++. Which C++ exactly? I tried to figure it out myself (Arduino IDE 1.8.13 for Arduino Uno), but I can't conclude what C++ standard it supports. From these tests, it seems…
Thomas Weller
  • 1,058
  • 1
  • 8
  • 22
12
votes
4 answers

Why is it considered bad practice to use the 'new' keyword in Arduino?

I previously asked this question: Is it required to delete variables before going to sleep? On that question, @Delta_G posted this comment: ... Really on a microcontroller I would create the object in a smaller scope and try to do everything in my…
Tono Nam
  • 966
  • 1
  • 10
  • 22
1
2 3
71 72