Skip to content

Arduino code and IDE

Arduino code#

The Arduino programming language is C like language. You can program in a style that is functionally equivalent to C or you can lean more toward Object Oriented (OO) software methods.

The main difference between C and Arduino is the structure of the code and that Arduino is designed for embedded hardware solutions rather than a general purpose application language.

In Arduino there is the concept of the setup() function which runs only once at the start of the sketch. Then there is the loop() function which is similar but not the same as the main() function in C or C++.

An Arduino programming language example#

// IN Arduino, the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Compare this with the C version of the same thing#

!!! Note that while the example below is C-like, as there are no includes of libraries or macros that would serve to make the code work for pinMode(), delay(), or digitalWrite(). So, the following example serves to show the maincore differences between the C language and the Arduino Language.

void setup() {
// initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}
/* the main function does NOT run over and over again forever, so we need another mechanism here. This is seen in the while(1){} construct below.
*/
void main() {
  setup();              // Here we must call the setup from within the main() function. 

  while(1){             // This emulates the loop() idea in Arduino
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  }
}

The Arduino IDE#

Attribution: Cedar101

Limitations to the IDE#

The IDE will accept C code and C++ code. However, any attempt to use main() in place of loop() will cause an error!

There are also overheads to using the Arduino language, its IDE. There is unavoidable bloatware which is placed in your object code after compilation. Additionally, some of the built-in functions such as delay(), use AVR clocks and this may impact your use of the AVR when you try to write code that uses the same clocks and interrupts. Having said this, all of this stuff is well documented and there is not much impediment to producing fairly complex programs (sketches) in Arduino.

In addition to the above the IDE is not considered professional. It lacks many of the functionality of even a simple IDE such as Geany

Comments