Book Image

Arduino Essentials

Book Image

Arduino Essentials

Overview of this book

Table of Contents (17 chapters)
Arduino Essentials
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

The complete example code


There is nothing really new in the code that we will use for this example. We will, just like in the previous one, simply read the digital input corresponding to the phototransistor and only if it is a HIGH value, which means it is not receiving any light and thus not conducting, will we make the output LED blink.

Once again, just for the sake of simplicity, and given that now we know that we don't really need to store the value read with digitalRead() and we can simply call the functions inside the if condition, I have opted this time to save up this variable and simplify even a little more of the code:

/*
 Chapter 05 - Sensing the real world through digital inputs
 Optical coin detector
 By Francis Perea for Packt Publishing
*/

// Global variables we will use
int led = 13;
int phototransistor = 8;

// Configuration of the board: one output and one input
void setup() {
  pinMode(led, OUTPUT);
  pinMode(phototransistor, INPUT);
}

// Sketch execution loop
void loop...