I kindly ask for your help on the following matter. It is really a n00b thing, in my defense, I have tried and read everything on this topic, but couldn't find a solution.
Proj. description, code; Check value on pin 3 (if switch is pressed), Red LED is on (pin 1), if there is HIGH value on pin 3, turn on green LED (pin 2), turn off red LED.
Wiring description; (attached picture); LEDs connected to the relative pins, grounded via 220 ohm resistors; switch grounded with "pull down" 10kOhm resistor, blue LED for test if it works.
PROBLEM:
The green LED is always on, so pin 3 always reads HIGH. Pressing the button lights my blue LED, but does not change the wiring behaviour.
QUESTIONS:
Why does pin 3 always read HIGH? It does not seem to be floating and looks proper to me?
Does method digitalRead() get to work when it gets read in the loop (only at the beginning of the loop) , or is it like a ("multithread") "process", which always listens to PIN input, no matter where in the loop the execution is?
It only reads the pin when the code gets to the line with digitalRead() on it. Same with every other line of code.
And it is best to leave pins 0 and 1 unconnected and unused in your program as they are used when uploading programs and by Serial when you send messages to the PC over the USB cable.
The way code works is that one line is done at a time. The processor only does one instruction at any instant of time. You can make it appear to do more than one thing at once only by use of clever code that swaps its attention between two or mor tasks. This is not automatic you have to write the code that does this.
If you want to look at a pin for a specific length of time put the read instruction in a loop that repeatedly does the digitalRead for how ever long you want. But you have to think what you want to look for because a single digital read returns just one value and if you are in a loop that reads it a lot at the end of the loop you only get the value it last read.
What do you want it to do? You can write something like
unsigned int lastTime=millis();
while (millis()<lastTime+xy) {
switchState=digitalRead(3);
}
but you will only get the last value read anyway.
You may want to wait until the value is changed like
while (digitalRead(3)==LOW) {} //wait while the pin is LOW
//Do domething here since pin 3 is HIGH
while (digitalRead(3)==HIGH) {} //wait while the pin is HIGH
//Here it is LOW, do something
Little tip for you: you don't need the 10K pulldown resistor on the switch, because the blue led + series resistor will pull down the Arduino pin anway.
cerko88:
So, a bit further; if we put delay(xy) right after (in the next line) the digitalRead(), the pin will be read for xy ms?
No. It just performs the instruction on one line of code and then immediately moves on to the next line. delay(xx) is just an instruction to spend xx milliseconds doing nothing.
Have a look at the demo Several Things at a Time. Try it out and make changes to the code so you get a good understanding of what is going on. It is pretty much fundamental to all Arduino programming.
Grumpy_Mike:
When it floats it can read any value high or low you just don't know. That is why you have to avoid floating inputs.
No. The real concern is current consumption. If you have pin connected to nothing you probably don't care what voltage is applied to it. But any voltage between (GND-diode drop) and (Vcc+diode drop) may be present. If the voltage is near threshold between HIGH and LOW current consumption may increase. Not real concern on power hungry Arduino UNO but may cause trouble in low power stand-alone battery powered application.
This is problem not only of floating pins but analog input also. When voltage close to the threshold is applied on the pin digital buffering eats additional current - unless disabled.
Smajdalf:
No. The real concern is current consumption.
We are talking to a beginner here, someone who does not know what "floating" means. Strictly speaking, Mike should have said "That is why you have to avoid reading floating inputs", but that's the important concept here. Marginal differences in current consumption that only affect low power battery circuits is a subject for a much more advanced forum user.
Floating pin = not connected to anything. There is no reason to read floating pin. It is like reading blank paper. Even complete noob should understand it is pointless (unless you want to use it as some form of RNG).
Grumpy_Mike:
When it floats it can read any value high or low you just don't know. That is why you have to avoid floating inputs.
I believe floating inputs are fine. I never activate/connect pull-ups on unused pins. They are simply floating. Why not? Unless you want low power consumption ofc. You tell a beginner to avoid floating inputs. I think it is bad advice unless he tries to make low power application.