Check WHICH pin is high?

Hi!
This could be a very dumb question. But I need your help! :o

I want to check WHICH pin is high on my Arduino Due.

As in, if pin 10 is high, it prints to the Serial monitor "10." Or if pin 47 is high, it prints "47" I know the long way you could to this:

  1. Declare EVERY single pin on the board
  2. Identify EVERY pin as input
  3. In the loop, check EVERY single pin for it's state.
  4. If the pin is high, print it's number.

That seems like a ton of work, considering the number of pins on the Arduino Due. Is there ANY easier way to do this than check each pin, one by one? There MUST be an easier way.

Please save me! :slight_smile: :o :confused:

-SQ

The pins are grouped into hardware "ports", which have labels PORTA, PORTB and so on. which pins belong to which ports varies from board to board.

You'll need to become familiar with the C 'bit fiddling" operators: ~ & ^ | << >> and >>> .

stupid-questions:
That seems like a ton of work, considering the number of pins on the Arduino Due.

Surely a couple of for loops would do the job fine?

PaulMurrayCbr:
The pins are grouped into hardware "ports", which have labels PORTA, PORTB and so on. which pins belong to which ports varies from board to board.

You'll need to become familiar with the C 'bit fiddling" operators: ~ & ^ | << >> and >>> .

How could I use this? I have a little familiarity with Bit Fiddling Operators, but not much. How would I use this here?

saximus:
Surely a couple of for loops would do the job fine?

Could you provide an example? I kinda understand what you mean, but not entirely. Could you explain a little more?

Thanks so much for the help so far! I really appreciate your input!
-SQ

Why you need to check every pin? Are all 54 pins hooked up to sensors that can randomly be high or low? Polling through the pins would be the easy way.

ChrisTenone:
Why you need to check every pin? Are all 54 pins hooked up to sensors that can randomly be high or low? Polling through the pins would be the easy way.

Indeed. I rather suspect tha this is an x/y problem.

OP - what are you actually trying to accomplish?

To speed things up by reading an entire port at a time, you would have to implement tables to map backward from port/bit numbers to "arduino pin" numbers, as well as doing a fair amount of annoying bitwise math. For this to be worthwhile, you really need to be convinced that the speedup is valuable enough to warrant the effort. Reading all 54 pins on a Due using digitalRead() for each one should end up taking less than a millisecond.

Reading all 54 pins on a Due using digitalRead() for each one should end up taking less than a millisecond.

Since OP is using Serial to output results, only 52 pins need testing.

Are you interested in the 16 analogInput/digital pins as well? 54 to 69?

Delta_G:
An example of a for loop?

// check 13 digital inputs on uno

for(int i = 0; i<14; i++){
  if(digitalRead(i)) Serial.println(i);
}

5/10. See me.

Delta_G:
An example of a for loop?

// check 13 digital inputs on uno

for(int i = 0; i<14; i++){
  if(digitalRead(i)) Serial.println(i);
}

Sorry, dumb question! Yes, I think I understand what you mean. Use a for loop to set the pins, and another one to check each one. I will try this, thank you!

Yeah, but you could have matched the comment to the code - think of the noobs!

PaulMurrayCbr:
The pins are grouped into hardware "ports", which have labels PORTA, PORTB and so on. which pins belong to which ports varies from board to board.

You'll need to become familiar with the C 'bit fiddling" operators: ~ & ^ | << >> and >>> .

A Due is an ARM processor, not an AVR. It's not likely to have the same register names.