I wanted to create a code where I had to use "if (digitalRead (10)==HIGH/LOW)" my problem is it performs both commands at the same time, so the pin seems to switch the mode. I start to get angry, because nothing in the code changes it. Even if it's not connected to the breadboard (with a pulldown resistor) it does the same thing. It's the same with the other pins... please help!
Let's see a program that shows the problem and a circuit diagram to go with it.
The C compiler is going to see the expression:
if (digitalRead (10)==HIGH/LOW)
as "Divide HIGH by LOW and compare the result to the value returned from digitalRead() on pin 10." The Precedence Rules cause math operations to be performed before relational operations. Therefore, dividing 1 (HIGH) by 0 (LOW) is going to result in a pretty large number and, if the compiler does generate the code, digitalRead() is never going to return a value bigger than infinity.
You're going to have to break the HIGH and LOW tests into two separate statements according to what you're trying to do.
I did that, I just wanted to make the explanation brief.
void loop() {
if (digitalRead (13)==HIGH); {
ShowTime(7);
if (digitalRead (13)==LOW); {
ShowTime(8);
}
}
}
that's my real code. ShowTime is a void programm I created for the Adafruit 7segment display.
if (digitalRead (13)==HIGH);
What is that semicolon doing there ?
Oops. Yeah that was a mistake but removing it doesn't change anything. The display's/the PIN's still changing.
Dang!
Just when I think I can be of assistance and ask if there's a pullup/pulldown resistor, Bob swoops down in his helicopter and shows me just how much more beans I need to munch on.
Yeah, semicolons make both your ShowTimes execute.
Or, maybe it is a floating input after all.
Damn you slow typing!
You should show all of your code. Perhaps you have more mistakes than those two misplaced semicolons.
For future reference, you can use the result of digitalRead() as a boolean (true or false) value:
void loop() {
if (digitalRead(13))
ShowTime(7); // input is HIGH
else
ShowTime(8); // input is LOW
}
There is even an operator ( ? : ) for choosing between two values based on true/false:
void loop() {
ShowTime(digitalRead(13) ? 7 : 8);
}
It's time to add some debugging Serial.prints() to see what value digitalRead(13); is returning in your program and can we please see all of it and a circuit diagram as previously requested.
Well guys, thank you very much. It was the semikolons. I'm such a dumbass... I tried to restart the Arduino and now it works. Thank you so much! You saved my day.
High/low invokes a divide by zero error.