Boa noite
eu estou tendo dificuldades com o pushbutton, não consigo fazer uma leitura correta, eu fiz a montagem no circuito seguindo o esquema abaixo e utilizando o código disponível no exemplo da ide, o led só desliga quando pressiono o botão, ou seja, a leitura é sempre 1. Será que isso é uma falha na montagem,é erro no código?
const int buttonPin = 12; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will 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 is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Tags:
Natã, obrigado pela resposta.
Agora que você comentou, não sabia que existia essa diferença, achei que todos eram normalmente abertos, o que é o caso do meu, ou seja, ele só passa corrente quando é apertado.
o que você testou é assim também?
Acontece que como está ligado ele está mandando o sinal alto para a porta, quando você aperta o botão ele liga com o terra, então a o sinal na porta passa a ser baixo. Mude na programação essa parte:
...
if (buttonState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} ...
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por