Need help with IF and FOR STATEMENT-have working program but can't modify it

I have a circuit with 6 MCP4162-103/P digital 10k pots.
I started with one and expanded it to 6.
The object is a HW/SW training exercise.
I have a program that works repeatedly and flawlessly cycling the resistance of the pots one at a time.
It starts with them all off and one by one reduces the resistance so the voltage at the wiper will drive a led.
My plan for the next phase was to add analog input. I tested all the analog inputs using an SD Datalogger
example program I found that uses a potentiometer to vary the input voltage and saves it to a datalog.txt
file on the SD card. One of my objectives was to build (breadboard) a data acquisition system. So there’s
no problem with that. It works fine. The problem is when I tried to combine the analog input and the
six digital pot program to read the changing voltages on the wipers and display them on the serial port.
The serial output is no problem but somehow I just don’t get the “IF” statement usage right. When I take
the working six pot program and add IF statements based on the SSelectPin (the SPI CS for each of the
pots) the program doesn’t cycle any more. Only pot 4 cycles.
Here’s the wiring:
DigPot-1 CS = Arduino UNO dig-10-----------=>Analog Input A5
DigPot-2 CS = Arduino UNO dig-9-----------=>Analog Input A4
DigPot-3 CS = Arduino UNO dig-8-----------=>Analog Input A3
DigPot-4 CS = Arduino UNO dig-7-----------=>Analog Input A2
DigPot-5 CS = Arduino UNO dig-8-----------=>Analog Input A1
DigPot-6 CS = Arduino UNO dig-6-----------=>Analog Input A0

I wired it this way thinking I could take advantage of the relationship between the ascending CS and the descending
dig Output pin numbers using "Analog Input#= (SSelectPin-5) but realized that the designation for the analog inputs
start with an “A” (A0, A1 etc…) and I couldn’t figure out how to make it work so in desperation I made some IF
statements.
The attached files are:
SIX_DIGITAL_POTS_6.ino (perfectly functioning pot cycling , led illuminating program)
Data_Acquisition_14 (A completely nonfunctional program )
It got changed that many times because I was too embarassed to ask for help but now I would
rather byte the bullet if I can get it working. I still have add the thermocouple /LCD display (that works standalone)
and the I2C LCD (also works standalone) and the I2C 6-Digit-7-Segment LCD which works (cycles through a test
routine) but I haven’t learned to interpret the figure out the HT1621B datasheet pdf to program it .
All the display stuff will have to wait because if I can’t acquire the data there is nothing to display if you know what
I mean…
I’m sure I did something really dumb in the IF statement department but I had to try anyway.

SIX_DIGITAL_POTS_FINAL_6.ino (5.17 KB)

Data_Acquisition_14.ino (14.9 KB)

I'm sure I did something really dumb in the IF statement department but I had to try anyway.

Yeah, when I wrote the IF statement I forgot to put the analog pin numbers in DESCENDING order instead ASCENDING order. I'm changing it now and then I'll test it again.

Well that didn't help. Only pot-6 is cycling.

Initial first glance impressions:

  1. You only ever need to configure the SPI once, and once only. You don't need SPI.begin() and SPI.setBitOrder() in your loops, only in setup().

  2. You seem to be writing blocking code. Don't. You can't make it do multiple things at once if it has to wait for one thing to finish before it starts the next. Take a look at the BlinkWithoutDelay example in the IDE to see how to use millis() to tell when to do things.

  3. Consider functions instead of repeating the same block of code with different values many times. It'll make the whole program more understandable.

Got it. I'll read it and start changing everything so that it only does something when necessary. Thanks for your input.

I went back and took another look at everything and found a wiring error.
After eliminating that problem I did a little optimizing in the program.
I know how to do arrays now and case so maybe I’ll redo it with one of those methods.

FIVE_MCP4162_P_Digital_Pots_dly_10_ino.ino (2.54 KB)

Five Digital pots delay 10.txt (2.62 KB)