PIN read (already tried everything)

Dear community,

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.

void setup() {
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, INPUT);
}

int switchState = LOW;


void loop() {

  switchState = digitalRead(3);
 
  digitalWrite (1, HIGH);

  if (switchState = HIGH) {

      digitalWrite(1, LOW);
      digitalWrite(2, HIGH);
      delay(1000);
  }

  digitalRead(3);

}

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:

  1. Why does pin 3 always read HIGH? It does not seem to be floating and looks proper to me?

Thank you very much,

Kindest regards,

Boris.

  if (switchState = HIGH) {

should be

  if (switchState == HIGH) {

This is a mistake often made by beginners.

Other errors:

Where is the line to switch the green led off?

There is a line of code near the end of the sketch with digitalRead() that does nothing and can be deleted.

Dear Paul,

Thank you very much. I think I owe you a beer, problem is solved!

Thank you also for the other comments on code.

You the man!!!

Kindest regards,

Boris.

Just, a "deeper" question, about the code;

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?

Thank you,

Boris.

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.

...R

Thank you, Robin.

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.
Nothing will happen at all for xy mS.

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.

cerko88:
the pin will be read for xy ms?

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.

...R

Thanks a lot for all your replies.

If I am not pushing, I'd like some further knowledge (questions base on my wiring above);

  1. With the wiring above, where do the electrons go, after they reach PIN 2?
  2. If I don't use a pull down resistor on PIN 2 (or other components, which Paul mentioned), does it read HIGH, because it "floats"?

Thank you very much,

Kindest regards,

Boris.

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.

They go into the input gate of the input buffer inside the processor.

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.

No. The real concern is current consumption

As you yourself point out this is not the case.

Not real concern on power hungry Arduino UNO

Which is what we have here. Please try and be a bit more intelligent about your replies.

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.

You are missing the point.

Even complete noob should understand it is pointless

Should. But don't. Noobs come to the forum with this problem almost every day, this thread is a case in point.

To be fair to the OP, they knew about floating inputs and knew they were to be avoided, but thought that they would always read HIGH.