#include <SPI.h>
static const int spiClk = 1000000;
byte address = 0x00;
byte address2 = 0x01; // Address for the second digital pot
SPIClass *hspi = NULL;
void setup()
{
pinMode(53, OUTPUT); // Set SS (Slave Select) pin for the first pot to 53 on Mega
pinMode(45, OUTPUT); // Set SS pin for the second pot to 45
hspi = new SPIClass(SPI);
hspi->begin();
}
void loop()
{
int counter = 0;
while (1) {
for (int i = 0; i <= 255; i++)
{
digitalPotWrite(53, address, i); // Control the first pot
if (counter <= 1) {
digitalPotWrite(45, address2, 255 - i); // Control the second pot
}
delay(10);
}
counter++;
delay(500);
for (int i = 255; i >= 0; i--)
{
digitalPotWrite(53, address, i); // Control the first pot
digitalPotWrite(45, address2, 255 - i); // Control the second pot
delay(10);
}
}
counter = 0;
}
void digitalPotWrite(int csPin, byte address, int value)
{
hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(csPin, LOW); // Select the MCP4131
hspi->transfer(address);
hspi->transfer(value);
digitalWrite(csPin, HIGH); // Deselect the MCP4131
hspi->endTransaction();
}