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:
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().
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.
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?
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.