Monday, May 18, 2015

Using buttons

Light switch

For this project we are going to build a simple circuit to act like a light switch.  The starter and budget packs come with tactile buttons which we will use.  We are going to 'de-bounce' the button to prevent the light flickering when it is pushed.


Software

The code has 3 main parts, declaration of variables, the setup and the loop which has two sub parts.

Variables

We are going to use 3 variables, led for the pin that the LED is on, button for the pin that the button is on and on for whether or not the LED should be turned on.

int led = 13;
boolean on = false;
int button = 2;

I would suggest avoiding pins 0 and 1 since they are used by the computer when uploading code.  If you do have to use them you should probably unplug them before uploading any new code.  We are using two different types of variables, int which is a whole number, positive and negative, and boolean, which is either true of false.

Setup

The setup method is identical to the blink code.  All pins by default are input pins so we do not need to specify that pin 2 is an input.

void setup(){
  pinMode(led,OUTPUT);
}

Loop

The loop does most of the work; it turns the LED on and off and detects if the button is pushed.  The LED is easy to turn on or off.  We create a simple if statement to check if we should be on:

  if(on) digitalWrite(led,HIGH);
  else digitalWrite(led,LOW);

Note that since the statements are on one line we do not need curly brackets {}.  If you wanted to add in more than one statement you would need to change the code to:

  if(on){
    digitalWrite(led,HIGH);
  }
  else{
   digitalWrite(led,LOW);
  }

We now need to detect if the button has been pressed.  To do this we use a method called digitalRead which will return either 1 (true) or 0 (false).  If the button has been pressed then we want to flip on; if it was true we want it to be false and if it was low we want it to be high. 

  if(digialRead(button)){
  on = !on;
}

The only issue with this method is that the LED will flicker a little when the button is pushed.  This is because the arduino goes through the loop quicker than you can release the button!  To fix this problem we add in a small delay of 1ms using delay(1).  This is called de-bouncing the button.  One other issue is that if we hold the button down the LED flashes on and off.  We can solve this by stalling the program until the button is released using a while loop.
   if(digitalRead(button)){
    on = !on;
    delay(1);
    while(digitalRead(button)); // stay here while the button is pressed
  }  

Hardware

Button wiring according to the datasheet
The button completes a circuit when you push it down.  In the picture to the left pins 3 and 4 are only connected to pins 1 and 2 when the button is pushed down.  We will build our circuit to use this feature.  We will be using a 10k pull up resistor for the button, all this means is that there is a 10,000 ohm resistor from ground to the button.  See the circuit diagram for details.  If your arduino board turns off at any point then there is a short circuit.  This means that 5V or 3.3V is directly connected to GND.  This can damage your board so try and avoid doing it.  If the LED doesn't turn on when you push the button check that your LED is in the correct way around.  Also check that your button is wired correctly.  If this doesn't fix it try the blink sketch to check your LED.
Circuit diagram

No comments:

Post a Comment