Writing code to react to a press

Before we go into the details of the programming of such a circuit, we'll have to decide what behavior we will unchain in our project once the event of a press is detected. For the sake of simplicity, we will make an LED blink or fade depending on the press of the button.

This way, we can test all that we have already learned in a simple example and this will also lead us to meet another very important control structure of any programming language, called a conditional bifurcation, which some say is the basic structure that makes any program a logical structure and not a simple sequence of instructions.

Once the circuit is assembled, let's take a look at the code we are going to write to detect and react to the button press as a digital input. Here, you have the complete code for this example:

/*
 Chapter 05 - Sensing the real world through digital inputs
 Sensing a switch
 By Francis Perea for Packt Publishing
*/

// Global variables we will use
int led = 9;
int button = 7;
int pressed = 0;
int intensity = 0;
int increment = 10;

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

// Sketch execution loop
void loop(){
  // We read the button pin
  pressed = digitalRead(button);
  // if it is not pressed lets blink digitally
  if (pressed == LOW) {  
    digitalWrite(led, LOW);
    delay(50);
    digitalWrite(led, HIGH);
    delay(50);
  }
  // otherwise lets fade analogly
  else { 
    for(intensity=0; intensity<=255; intensity=intensity+increment){ 
      analogWrite(led, intensity);   
      delay(30);                     
    }
    for(intensity=255; intensity>0; intensity=intensity-increment){ 
      analogWrite(led, intensity);   
      delay(30);                     
    } 
  }
}

Configuring and reading a digital input

From the point of view of programming, there are basically two main new concepts in this example regarding the use of a digital input:

  • pinMode(button,INPUT): In the setup() function, we have to set the pin we are going to sense as an input so that Arduino can read from it. The function is just the same pinMode() function we have been using so far, but instead of using the OUTPUT constant, this time, we use INPUT to set the pin accordingly.
  • digitalRead(button): When we deal with an input whether it is a digital or analog one, we are going to receive the read value instead of setting a value like how we have been using outputs. This means that we only pass a parameter indicating the pin to be read to the reading function, analogRead() in this case, but it also means that this function, as opposed to writing functions, is going to return us a value that we have, in some cases, to store in a variable for later use.

Being realistic, in this example, it isn't really necessary to store the read value in a variable because we are not going to use it anymore in the rest of the code, and we could simply have used the digitalRead() function inside the if parentheses like if(digitalRead(button) == LOW), but for the sake of clarity in this first example, I preferred to use a variable this time.

Taking decisions with conditional bifurcations

Beside the differences between the use of inputs and outputs, there is an even more important concept in this example, and it is the use of the if control structure to decide what has to be done depending on the state of a previous event.

The if control structure is the basis of any programming language, giving them the power to decide and act on consequences.

The simplest syntax of the if sentence is as follows:

if (condition) {
  Block of instructions to be executed
}

Being a condition, any logic expression is one that evaluates as true or false and that can use logical operators such as == (is equal to), > (is bigger than), < (is less than), or != (is different).

In a more complex format, the if control structure can even include a block of instructions to be executed in case the condition evaluates as false, in which case, its syntax is as follows:

if (condition) {
  instructions to be executed in case the condition evaluates as true
}
else {
  instructions to be executed in case the condition evaluates as false
}

As you can see in the code for our example, this format is the one we have used to get two different reactions on the pressing of the button:

  • If the pressed == LOW condition is true, being pressed the value read from the digital input and meaning no press on the button, we execute a simple blink
  • In the case of the condition being false, which means that there has been a press on the button, we execute the block of instructions contained in the else branch of the code

For a deeper reference with respect to the if control structure in the Arduino programming language, you can visit the Arduino website's Reference section, particularly the pages related to if and if … else at http://arduino.cc/en/Reference/If and http://arduino.cc/en/Reference/Else.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset