Book Image

Learning C for Arduino

By : Syed Omar Faruk Towaha
Book Image

Learning C for Arduino

By: Syed Omar Faruk Towaha

Overview of this book

This book will start with the fundamentals of C programming and programming topics, such data types, functions, decision making, program loops, pointers, and structures, with the help of an Arduino board. Then you will get acquainted with Arduino interactions with sensors, LEDs, and autonomous systems and setting up the Arduino environment. Moving on you will also learn how to work on the digital and analog I/O, establish serial communications with autonomous systems, and integrate with electronic devices. By the end of the book, you will be able to make basic projects such as LED cube and smart weather system that leverages C.
Table of Contents (17 chapters)
Learning C for Arduino
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Structure and function


Structure and function are two different things in C programming. We can pass the values of a structure to a function to make our program shorter and more efficient. There are two ways to pass a structure to a function: pass by value and pass by reference. First we will look at pass by value.

Pass by value

To pass a structure to a function, first we need to define a structure. Say we will define a structure Box, where we will have three double variables: length, height, and width:

struct Box { //declared Box structure 
  float height; //height of the box 
  float width; //width of the box 
  float length; //length of the box 
}; 

Now, we will build a function that calculates the volume of a box and prints it on the Serial Monitor:

void volume(struct Box box) { //Passed Structure 
  double volume = box.height * box.width * box.length;  //calculation 
  Serial.print("The volume of the box is "); 
  Serial.println(volume); //Printed...