Book Image

Arduino Development Cookbook

By : Cornel M Amariei
Book Image

Arduino Development Cookbook

By: Cornel M Amariei

Overview of this book

Table of Contents (16 chapters)
Arduino Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Button to serial


If we want to easily track how a button acts, serial communication is the best and simplest way. All we need to do is to read the status of the button and print it to the serial connection.

Testing whether a button is working can be solved by using an LED. However, if we need to check two buttons or better understand what's happening when the button is pressed, serial communication is much safer and may even be simpler.

Getting ready

The following are the ingredients required to execute this recipe:

  • An Arduino board connected to a computer via USB

  • A button

How to do it…

This recipe uses the Button with no resistor recipe's hardware implementation. Please implement the same schematic as in that recipe. We will have different code here, which will output the values on the serial connection.

Code

The following code will print the button status on the serial connection:

int buttonPin = 2;

void setup() {
  // Define pin #2 as input
  pinMode(buttonPin, INPUT_PULLUP);

  // Establish the...