Using Index addressing to control IO output for A0 to A5

I am currently using the little bit of code below to set all output ports high which will shut off all the leds on the project. Note that in the first section I have a loop to set ports 6 to 12 high. But when it comes to dealing with the Analog ports, which have been set as outputs also, I have found the only way to address them is directly, individually. As this takes space in the program I would like to crush this down into a loop. I have tried several versions of "(A(val), HIGH) but without success. The best would be to create an offset of the variable c and digitalwrite Analog port to HIGH in the same loop as the digital ports.

Any Suggestion on how to clean this up?

ledcontrol(){

for (int c = 7; c < 13; c++){ // set count loop for 6 leds

digitalWrite(c, HIGH); // turns off the IO port LEDs used for second half of display

} // Next

digitalWrite(A0, HIGH); // turns off the Analog port used for display
digitalWrite(A1, HIGH); // turns off the Analog port used for display
digitalWrite(A2, HIGH); // turns off the Analog port used for display
digitalWrite(A3, HIGH); // turns off the Analog port used for display
digitalWrite(A4, HIGH); // turns off the Analog port used for display
digitalWrite(A5, HIGH); // turns off the Analog port used for display

for (byte c = A0; c <= A5: c++)or similar

Or am I missing something?

HI I had not considered that the Analog ports where HEX addresses I had assumed they where labels as in names for the ports in the Arduino language. I'll give that a try and create an hex offset value to index with along with the first loop. Thanks for the insight..

I had not considered that the Analog ports where HEX addresses

They are not HEX addresses. they are #defines. This allows the same names to be used for different boards even though the actual pin numbers may be different.

UKHeliBob:
They are not HEX addresses. they are #defines. This allows the same names to be used for different boards even though the actual pin numbers may be different.

I wonder if that means that using them as suggested in Reply #1 is generally applicable.

I would be inclined to store the values in an array and iterate over the array. Indeed all the led pins could go into a single array which would allow one FOR loop to iterate over them all.

...R

I can't see any advantage in using an array unless you were remapping the pin assignments, and it would use more precious RAM (unless you used PROGMEM)

I wonder if that means that using them as suggested in Reply #1 is generally applicable.

It will be generally applicable as long as the pin numbers are contiguous behind the scenes. There is also the problem that different boards have different numbers of analog pins.

UKHeliBob:
It will be generally applicable as long as the pin numbers are contiguous behind the scenes.

I posed the question because I don't know that.

...R

I posed the question because I don't know that.

Neither do I, hence how I phrased my reply. What I am sure of is that they are contiguous on a Uno from A0 to A5 (14 to 19). There is, however, a catch because printing A6 on a Uno produces the number 20 but the Uno does not have an A6 pin. Other boards, such as the Micro have many more but the pin numbers are not contiguous

The Micro has a total of 12 analog inputs, pins from A0 to A5 are labelled directly on the pins and the other ones that you can access in code using the constants from A6 trough A11 are shared respectively on digital pins 4, 6, 8, 9, 10, and 12.

So, probably best not to assume anything !

You can put the (symbolic or literal) pin numbers into an array, and iterate over that array.

OK.. So You need to know Groove was right on the money with the index addressing to the A port on the Arduino UNO. I do specify that this has only been tried out on the UNO. Have a look at the code below that I build tonight to test this problem. Maybe not the prettiest of code but it works. The values used to point at A0 to A5 must be byte values. When I changed up the first loop to byte things really started working I missed making the second loop byte value and it did not display. Then I found and fixed the byte value in the second loop. Byte format was the key to making this thing work, and yes the display is working properly on my desk right now. Here is the new code segment, and Thank You all for your comments and suggestions on this problem.

void ledcontrol(){
byte rp = 0;
byte rt = 0;
byte rz = 0;
byte ry = 0;
for (byte c = 0; c < 6; c++){ // set count loop for 6 leds in two groups
rp = c + 6; // create offset to address display leds
rt = c + A0; // create offset to address A ports
digitalWrite(rp, HIGH); // turns off the IO port LEDs used for second half of display
digitalWrite(rt, HIGH); // turns off the IO port LEDs used for second half of display

} // Next

for(byte c = 0; c < 6; c++){ // set count loop for 12 leds two groups of 6
rp = c + 6; // create offset to address display leds
rt = c + 7; // offset to pick display value from array
ry = c + 1; // offset to pick display value second half from array
rz = c + A0; // create offset to addres a port display leds

if ( DISO[rt] > 0){ digitalWrite(rp, LOW); } // tests display data array location for 0 or 1 and set led
if ( DISO[ry] > 0){ digitalWrite(rz, LOW); } // if greater than zero turn led port A0 on active low

} //Next

} // end of led control subroutine

Happy Making

sliderule:
I do specify that this has only been tried out on the UNO.

That it works for an Uno was never in doubt.

...R

DrDiettrich:
You can put the (symbolic or literal) pin numbers into an array, and iterate over that array.

+1. The portable way.

aarg:
+1. The portable way.

But see reply #5

outsider:
PORTC |= 0b00111111;

Wouldn't work on a Mega.
See reply #12