Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: TrovatoMassimo on Mar 12, 2018, 05:34 pm

Title: Problem with Input
Post by: TrovatoMassimo on Mar 12, 2018, 05:34 pm
In the following sketch, Arduino uno doesn' t read the button, in fact, regardless of the state of the input, the condition is still false.

int LED = 13;
int BUTTON = 2;

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

}

void loop() {

if(BUTTON == HIGH){
 digitalWrite(LED, HIGH);
 delay(50);
 digitalWrite(LED, LOW);
 delay(50);
}
else{
 (BUTTON == LOW);
digitalWrite(LED, LOW);
delay(1000);
digitalWrite(LED, HIGH);
delay(1000);
}
}





Title: Re: Problem with Input
Post by: slipstick on Mar 12, 2018, 05:41 pm
That's because you haven't asked it to read the button. The variable BUTTON is a pin number and it is equal to 2. Expecting it to change or comparing it to LOW or HIGH doesn't make much sense.

You're using some digitalWrite() commands. Now it's time to have a look at digitalRead().

Steve
Title: Re: Problem with Input
Post by: INTP on Mar 13, 2018, 12:18 am
You made the variable "BUTTON" equal to 2.

It's always 2.

when you say:
if(BUTTON == HIGH){

you are just saying
if(2 == HIGH){

you are NOT saying 'if pin 2 == HIGH'


And you don't need this:  (BUTTON == LOW); even though it's missing parts anyway.