Beginners‎ > ‎

reading a pushbutton


In this example we are going to connect a pushbutton to the pinguino, and based on the value of the button, we are going to switch on and off a Led. The goal is to learn how to read a digital value.


WHAT DO YOU NEED




SCHEMATIC



BOARD


SOFTWARE

// test blink with Pinguino

#define PIC18F4550

// pin where the led is connected
#define LED 0

// pin where the button is connected
#define BUTTON 1


void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(BUTTON,INPUT);
}

void loop()
{
  if(digitalRead(BUTTON)) {    // if a high value is read...
    digitalWrite(LED,HIGH);
  } else {                                // if a low value is read...
    digitalWrite(LED,LOW);
  }
}

As this it's not the first pinguino example, we are going to suppose that you know what means the setup and loop functions, and why appears the #define PIC18F4550 directive.

In this program, we are going to show the use of the digitalRead function.

In setup function we have defined BUTTON pin as an input. Then in the loop routine, we test the value with digitalRead. In this program if the button is pressed (low value), the led is switched off, and if not, the led is switched on.


DO-IT-YOURSELF

- change the behaviour of the button, if you press the button change the status of the led (if it's switched on, switch off the led, and if it's switched off, switch on the led).



Comments