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:
Declare EVERY single pin on the board
Identify EVERY pin as input
In the loop, check EVERY single pin for it's state.
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.
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.
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.
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!
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.