code{ overflow:auto; /* barra de rolagem*/ background: #E8E8E8; border:1px solid #000000; color:#XXXXXX; /* cor da fonte*/ font-size:90%; height:200px; display:block; white-space:pre; text-align:left; word-wrap:break-word; padding:0 10px 5px 20px; }

Wednesday, 25 February 2015

Ligar um LED através de um botão - Arduino Tuturial 4

baseado em http://www.electroschematics.com/

Apresentamos o código necessário para fazer um micro-controlador Arduino ligar um LED a partir do comando de um botão externo. Abstemo-nos de o explicar uma vez que o explicado até ao momento é suficiente para entender o funcionamento.

1. /* sketch 1 
2. turn on a LED when the button is pressed
3. turn it off when the button is not pressed (or released)
4. */
5. int pinButton = 8//the pin where we connect the button
6. int LED = 2//the pin we connect the LED
7. 
8. void setup() {
9. pinMode(pinButton, INPUT); //set the button pin as INPUT
10. pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
11. }
12. 
13. void loop() {
14. int stateButton = digitalRead(pinButton); //read the state of the button
15. if(stateButton == 1) { //if is pressed
16. digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
17. else { //if not pressed
18. digitalWrite(LED, LOW); //write 0 or low to led pin
19. }
20. }





No comments:

Post a Comment