Manually Assigning Other Chip Select Pins

Hi everyone,

The code below is for my Arduino Due to communicate with LabVIEW to write values to my AD5235 25kOhm digital potentiometer. When I have csPin=10, the code works, and I am able to assign whatever values in LabVIEW to then have the resistance on the digipot change. When searching online, I saw that pins 4, 10, and 52 are built-in chip selects for this Arduino. However, I saw that you should be able to manually assign other digital pins as chip selects. I've tried a few tips from other forums, but I cannot get the resistances to change when I have a csPin = any value other than 4, 10, and 52. I would like to be able to use pin 5 as my chip select and will eventually include cases for the other pins I'd like to work with. If anyone has an idea of how to do this, I'd really appreciate the help!

// Importing SPI & String libraries
#include <SPI.h>
#include <string.h>

// Assign pins for each digipot
const int csPin = 5;
// online it is said that 4, 10, and 52 can be used as chip selects automatically
//manually would like to initialize pins 5-10 as chip selects

String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether string is complete

void setup() {

Serial.begin(115200); // initialize serial
inputString.reserve(200); // reserve 200 bytes for the inputString

pinMode(csPin,OUTPUT); // csPin variable carries with it the pin I assigned to it in the beginning and is reassigned once if statements are adjusted
digitalWrite(csPin, HIGH);

SPI.begin(csPin);
SPI.setClockDivider(csPin, 21);
SPI.setBitOrder(MSBFIRST);
Serial.begin(115200);
SPI.setDataMode(csPin, SPI_MODE2);

}

void loop() {
if (stringComplete == true)
{
String myString = inputString;
int commaIndex = myString.indexOf(',');
// Search for the next comma just after the first
int secondCommaIndex = myString.indexOf(',', commaIndex + 1);
int thirdCommaIndex = myString.indexOf(',', secondCommaIndex + 1);

String firstValue = myString.substring(0, commaIndex); 
String secondValue = myString.substring(commaIndex + 1, secondCommaIndex); 
String thirdValue = myString.substring(secondCommaIndex + 1, thirdCommaIndex); 

int x = secondValue.toInt();
int y = thirdValue.toInt();

if (firstValue == "5") // will expand this case for each pin (5-10) needed as chip selects
{     
      digitalPotWrite(x,y); 
 }
    
  
// clear the string
inputString = "";
stringComplete = false;

}

}

void serialEvent()
{
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString
inputString += inChar; // add new byte to the inputString
// if the incoming character is a new line
//set a flag so the main loop can do something about it:
if (inChar == '\n')
{
stringComplete = true;
}
}
}

void digitalPotWrite(int address, int value) {
digitalWrite(csPin, LOW); //select AD5235
byte command = 0xB0; //0xB0 = 10110000
command += address;
SPI.transfer(csPin, command);
byte byte1 = (value >> 8);
byte byte0 = (value & 0xFF); //0xFF = B11111111
SPI.transfer(csPin, byte1);
SPI.transfer(csPin, byte0);
delay(20);
digitalWrite(csPin, HIGH); //de-select slave
//Serial.println("test digiwrite");
delay(1000);
}

And

Should not contain the csPin in them.

The code only started working when I included "csPin." Unfortunately, when I take it out, the resistance will not change, even when I use the automatic chip selects built in to the Due

Odd because that is not what happens with this example from this forum.

https://docs.arduino.cc/tutorials/communication/DigitalPotControl

I believe it is due to a difference between the Arduino Uno pictured in that forum and the Arduino Due that I am using. The code without csPin in those commands worked when I had everything hooked up to an Uno, but I had to switch to a Due.

Hi everyone. Still looking for some help on this if anyone has any more ideas!