Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Arduino Development Cookbook

You're reading from   Arduino Development Cookbook Over 50 hands-on recipes to quickly build and understand Arduino projects, from the simplest to the most extraordinary

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781783982943
Length 246 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Cornel M Amariei Cornel M Amariei
Author Profile Icon Cornel M Amariei
Cornel M Amariei
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Power on – Arduino Basics FREE CHAPTER 2. Blinking LEDs 3. Working with Buttons 4. Sensors 5. Motor Control 6. More Output Devices 7. Digital Communication with Arduino 8. Hacking A. Electronics – the Basics Index

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 we have a code breakdown to better explain each step.

First, we declared two global variables—var1 and var2—and we set them to the values of 10 and 20 respectively.

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

When the Arduino boots, it first allocates the global variables into memory. In the setup() function, we change the value of var2 to 5:

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

After the Arduino allocates the global variables, it executes the code inside the setup() function once. Following this, the loop() function will execute repeatedly. Inside, we have an if condition that will play with the values of var2. If var1 is greater than var2, we increase var2 by one. Eventually, var1 will not be greater than var2, and then we set var2 to 0. This will result in an infinite adding and equaling of var2.

This is one example on how the Arduino executes the code in its two main functions.

See also

Continue the Arduino code basics with the following recipe, Code basics – Arduino pins.

You have been reading a chapter from
Arduino Development Cookbook
Published in: Apr 2015
Publisher:
ISBN-13: 9781783982943
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime