Four, key control LED light off

Source: Internet
Author: User

Materials:

1.SAGOO UNO 1 Block;

2. Key Module 1 blocks;

3. A number of DuPont lines.

Steps:

1. Follow the Connect button module and UNO;

  Sagoo uno Pin Key Module Pin

3V3 <------------------------------------> V (Power)

GND <------------------------------------> G (Power)

Pin2 <------------------------------------> S (signal)

 2. Launch the Arduino IDE software, open File->examples->digital->button, compile and download to UNO motherboard;

  

 3. The default UNO board LED light is on, press the button led off, release the LED light.

Demo Video: http://v.youku.com/v_show/id_XMTI4NjM2NTg0MA==.html

Schematic diagram:

          

Code:

Constants won ' t change. They ' re used here to

Set PIN numbers:
const int buttonpin = 2; The number of the pushbutton pin
const int ledpin = 13; The number of the LED pin

Variables'll change:
int buttonstate = 0; Variable for reading the pushbutton status

void Setup () {
Initialize the LED pin as an output:
Pinmode (Ledpin, OUTPUT);
Initialize the pushbutton pin as an input:
Pinmode (Buttonpin, INPUT);
}

void Loop () {
Read the state of the pushbutton value:
ButtonState = Digitalread (Buttonpin);

Check if the pushbutton is pressed.
If it is, the buttonstate are high:
if (buttonstate = = high) {
Turn LED on:
Digitalwrite (Ledpin, high);
}
else {
Turn LED off:
Digitalwrite (Ledpin, low);
}
}

Description

  Hardware: Uno motherboard through digital Pin2 read the state of the button module, when the key output is high, lit LED lights, conversely, off LED lights. The key module is output high by default and press the key to output low level.

 Software: 

const int buttonpin = 2; --Defines the constant button as the number 2;

const int ledpin = 13; --Define constant Ledpin as number 13;

int buttonstate = 0;---Defines the default state of a PIN variable to 0;

ButtonState = Digitalread (Buttonpin);--Read the status value of the digital foot 2 to the variable buttonstate;

/*----------------------------------------------------

If...else ... Statement to determine the key state and perform the corresponding action

-----------------------------------------------------*/

if (buttonstate = = high) {
Turn LED on:
Digitalwrite (Ledpin, high);
}
else {
Turn LED off:
Digitalwrite (Ledpin, low);
}

        

Four, key control led light off

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.