digital input troubles

Hey guys, I'm working on a project and I'm having trouble with the digital I/O pins. I'm moving a door and have just been moving it for a set amount of time, but I recently I added limit switches to stop the up/down movements, but my digital input pins weren't responding, so I wrote a simple sketch to test them:

int A = 2;
int B = 3;

void setup() {

pinMode (A, INPUT_PULLUP);
pinMode (B, INPUT_PULLUP);
Serial.begin (9600);

}

void loop () {

digitalRead (A);
Serial.println (A);
delay (1500);
digitalRead (B);
Serial.println (A);
delay (1500);

}

So, here's what's happening, with or without switches connected (I've tried several different kinds trying to troubleshoot), it serial prints whatever pin# I've assigned. I've tried A&B = each pin 1-8. So for the code above, my serial print would look like:

2
3
2
3
2
3
2
3
2
etc...

And if I change to any other pins, same thing.

What am I missing?

Swithes are normally open, connected to the digital input pin and to ground.

The digitialRead() function returns a boolean value representing the input state. You need a place (variable) to put the state value.

// name the pins
const byte Apin = 2;
const byte Bpin = 3;

// name the state variables
boolean ApinState;
boolean BpinState;


void setup()
{
   pinMode (Apin, INPUT_PULLUP);
   pinMode (Bpin, INPUT_PULLUP);
   Serial.begin (9600);
}

void loop ()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      ApinState = digitalRead (Apin);
      Serial.println (ApinState);

      BpinState = digitalRead (Bpin);
      Serial.println (BpinState);
   }
}

I gave the variables more descriptive names. Single letter variable names are not good.
Note that I made the pin variables byte data type to save memory and constant to put them in program memory to save SRAM. Also used the blink without delay method of timing.
Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

In C/C++ by default the arguments to functions are passed by value - input only, nothing comes back to them,
only the result of the call comes back.

Arguments go in, result comes back.

You're telling fibs. The code you posted never prints 3. It prints all 2s.

But really you just needed need to print the result of the digitalReads....NOT the pin numbers.

Steve

And you should expect to be missing a lot of signals with

delay (1500);
delay (1500);

your program sleeping for 3 seconds. Imagine how fast the other 2 lines of code are getting executed,

If the two reads and serial prints takes 1mS the ratio of detecting the states vs time spent not doing anything or, as per this example a ratio of 3 seconds sleeping to .001 seconds working, one should expect to miss out on some signals.

This may help Demonstration code for several things at the same time - Project Guidance - Arduino Forum.

I can't tell if slipstick is joking or I did that bad of a job expalining my situation,

slipstick:
You're telling fibs. The code you posted never prints 3. It prints all 2s.

But really you just needed need to print the result of the digitalReads....NOT the pin numbers.

Steve

but you are exactly right, what I'd like to do is to print the digitalReads...NOT the pin numbers. That is my question. Why is it printing the pin numbers at all? How do I stop it?

Idahowalker:
And you should expect to be missing a lot of signals with

delay (1500);

delay (1500);




your program sleeping for 3 seconds. Imagine how fast the other 2 lines of code are getting executed,

If the two reads and serial prints takes 1mS the ratio of detecting the states vs time spent not doing anything or, as per this example a ratio of 3 seconds sleeping to .001 seconds working, one should expect to miss out on some signals.

This may help https://forum.arduino.cc/index.php?topic=223286.0.

The delay makes no difference at all. If it is reading the pin number and not the digitalReads, who cares how fast it reads them? I'm trying to understand why it is printing the pin numbers at all. I deleted the delays and now it just reads the pin numbers really super fast.

Why is it reading the pin numbrs instead of the digital input?

This is what you might be looking for.

Serial.println( digitalRead (A) );
delay (1500);
Serial.println (digitalRead (B) );
delay (1500);

Your code is working just fine. You wrote it to print the pin numbers and not the return of a digitialRead(x). digitialRead() is a method that takes an argument and returns a value. The argument A contains a number known as 2. Your code asks that a 2 be printed and not the return of the method, which requires the argument 2 in order to print the value of the pin state.

Perhaps this lesson by W3C can help you understand C++ Functions - Return

Here is the Arduino API for digitalRead() digitalRead() - Arduino Reference

Reads the value from a specified digital pin, either HIGH or LOW.

Syntax
digitalRead(pin)

Parameters
pin: the Arduino pin number you want to read

Returns
HIGH or LOW

Understand?

And slipstick was very clear. He/she wrote the same thing I just did.

Thank you. I see what I was missing now. I really appreciate the explanations (and the links too) :slight_smile:

You are welcome.