Code basics – Arduino C

The Arduino uses a slightly reduced C/C++ programming language. In this recipe, we will remember a few basics of C/C++.

Getting ready

Ensure that you have the Arduino IDE running on a computer.

How to do it…

Here is a simple example of basic Arduino C/C++ manipulating two variables:

// Global Variables
int var1 = 10;
int var2 = 20;

void setup() {
  // Only execute once when the Arduino boots
  var2 = 5; // var2 becomes 5 once the Arduino boots
}


void loop(){
  // Code executes top-down and repeats continuously
  if (var1 > var2){ // If var1 is greater than var2
    var2++; // Increment var2 by 1
  } else { // If var1 is NOT greater than var2
    var2 = 0; // var2 becomes 0
  }
}

How it works…

The code plays with two integer variables. Here ...

Get Arduino Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.