Day 5 - Section 4 - Playing with buttons

Site: ΕΛ/ΛΑΚ Moodle
Course: Robotics - 3D Printing - Internet of Things
Book: Day 5 - Section 4 - Playing with buttons
Printed by: Guest user
Date: Monday, 29 April 2024, 5:04 AM

Description

After the completion of this section the students will be able to:

  • describe and use push buttons in circuits
  • create a circuit to turn an LED on when we push a button
  • create the code for the above circuit
  • describe the use of comments inside an Arduino IDE program
  • describe the binary system and match binary to decimal numbers and vice versa
  • combine many LEDs and pushbuttons to create binary sequences

Push the button...

NodeMCU is not just an output device. It can accept triggers from its environment and act respectively, according to the program that it has on its memory.

In this example we 'll try to light on an LED every time we push a pushbutton. It is called so, because it connects its legs when someone push it for a moment and removes the connection when the button is not pressed anymore.

http://thumbs3.ebaystatic.com/d/l225/m/mdqVKBu1On7rLNet7EIPtHw.jpg

Our components will be arranged on the breadboard like below:

We notice here that we use D0 (pin 16) as an input pin to accept commands from the button and D1 (pin 5) as an output pin to send the signal to the LED for the ON-OFF operation.

In our case, when the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 3.3 volts, so that we read a HIGH.

As for the code for the above, it looks like that:

int ledPin = 5; // choose the pin for the LED
int inPin = 16;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == LOW) {         // check if the input is LOW (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

If you are wondering what are // characters, they are used as comments starting characters, so everything that follows in the same line is not interpreted. We can give notices to the other programmers about our ideas and explain our thoughts in a few words.

An other new structure that we notice in our code is the IF structure. With this, we can decide what actions we choose based on a codition.
So in our example, first we read the state of pin16 and if it is LOW (unpressed) then we turn the LED off (pin 5). If not, we turn the LED on.

So, copy the code in bold and paste it to Arduino IDE. Then upload your code to NodeMCU and.......play with the button!




There are 10 kinds of people....

"There are 10 kinds of people, those who understand binary system and those who don't."

This is an old joke for programmers. Maybe you are wondering why 10 and not 2? The answer is that number 10 in binary system (the one that a computer can handle) really represents number 2 of decimal system.

Watch the video to understand how it works:

The following table shows how the numbers match:


Now, let's create a NodeMCU game with three LEDs and three buttons. Connect the components based on the following diagram:

ft

Everytime we press a button, the corresponding LED will be lighted. So, we can create any combination of activated and not activated LEDs (ON and OFF).

The code for the above setup is the following:

int led1Pin = 14;
int led2Pin = 2;
int led3Pin = 5;
int in1Pin = 10;
int in2Pin = 16;
int in3Pin = 4; 
int val = 0;  

void setup() {
  pinMode(led1Pin, OUTPUT); 
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(in1Pin, INPUT);
  pinMode(in2Pin, INPUT);
  pinMode(in3Pin, INPUT);   
}

void loop(){
  val = digitalRead(in1Pin); 
  if (val == LOW) {        
    digitalWrite(led1Pin, LOW); 
  } else {
    digitalWrite(led1Pin, HIGH); 
  }

  val = digitalRead(in2Pin); 
  if (val == LOW) {        
    digitalWrite(led2Pin, LOW); 
  } else {
    digitalWrite(led2Pin, HIGH); 
  }

  val = digitalRead(in3Pin); 
  if (val == LOW) {        
    digitalWrite(led3Pin, LOW); 
  } else {
    digitalWrite(led3Pin, HIGH); 
  }

}


Copy the above code, paste it on Arduino IDE and upload it to the NodeMCU. Then try the following: Create binary numbers from 0 to 7 using the three push buttons. You can get help from the table above. For example 5 is represented by 1st LED ON, 2nd LED OFF and 3rd LED ON.

Try it yourself. Then ask from the other students to guess the numbers you create and don't forget: Have fun with coding!