![](/media/cache/group/mbed-logo-square-512_3.png.50x50_q85.png)
DigitalIn Hello World
Fork of DigitalIn_HelloWorld_Mbed by
Use
The DigitalIn API is used to read a digital pin. The value will be either 0 or 1. Buttons and switches are some of the more common digital inputs.
API
API reference.
Import librarymbed
main.cpp@10:9377dcb1cef3, 2017-06-27 (annotated)
- Committer:
- sarahmarshy
- Date:
- Tue Jun 27 11:14:19 2017 -0500
- Revision:
- 10:9377dcb1cef3
- Parent:
- 9:954ac88dda04
"Update mbed-os"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 4:9b20ef58e04c | 1 | /* mbed Example Program |
mbedAustin | 4:9b20ef58e04c | 2 | * Copyright (c) 2006-2014 ARM Limited |
mbedAustin | 4:9b20ef58e04c | 3 | * |
mbedAustin | 4:9b20ef58e04c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbedAustin | 4:9b20ef58e04c | 5 | * you may not use this file except in compliance with the License. |
mbedAustin | 4:9b20ef58e04c | 6 | * You may obtain a copy of the License at |
mbedAustin | 4:9b20ef58e04c | 7 | * |
mbedAustin | 4:9b20ef58e04c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 4:9b20ef58e04c | 9 | * |
mbedAustin | 4:9b20ef58e04c | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 4:9b20ef58e04c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbedAustin | 4:9b20ef58e04c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 4:9b20ef58e04c | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 4:9b20ef58e04c | 14 | * limitations under the License. |
mbedAustin | 4:9b20ef58e04c | 15 | */ |
mbed_official | 0:aaf5a9d465fd | 16 | #include "mbed.h" |
mbedAustin | 5:fa65447e171b | 17 | |
mab5449 | 9:954ac88dda04 | 18 | DigitalIn mypin(SW2); // change this to the button on your board |
mbedAustin | 5:fa65447e171b | 19 | DigitalOut myled(LED1); |
mbedAustin | 5:fa65447e171b | 20 | |
mbedAustin | 5:fa65447e171b | 21 | int main() |
mbedAustin | 5:fa65447e171b | 22 | { |
mbedAustin | 8:ef3558374977 | 23 | // check mypin object is initialized and connected to a pin |
mbedAustin | 7:64e67ec4441e | 24 | if(mypin.is_connected()) { |
mbedAustin | 5:fa65447e171b | 25 | printf("mypin is connected and initialized! \n\r"); |
mbedAustin | 5:fa65447e171b | 26 | } |
mbedAustin | 5:fa65447e171b | 27 | |
mbedAustin | 7:64e67ec4441e | 28 | // Optional: set mode as PullUp/PullDown/PullNone/OpenDrain |
mbedAustin | 8:ef3558374977 | 29 | mypin.mode(PullNone); |
mbedAustin | 5:fa65447e171b | 30 | |
mbedAustin | 5:fa65447e171b | 31 | // press the button and see the console / led change |
mbed_official | 0:aaf5a9d465fd | 32 | while(1) { |
mbedAustin | 5:fa65447e171b | 33 | printf("mypin has value : %d \n\r", mypin.read()); |
mbedAustin | 5:fa65447e171b | 34 | myled = mypin; // toggle led based on value of button |
mbed_official | 0:aaf5a9d465fd | 35 | wait(0.25); |
mbed_official | 0:aaf5a9d465fd | 36 | } |
mbedAustin | 5:fa65447e171b | 37 | } |