#include <SPI.h>

const int CS_PIN = 10; // CS pin for MCP41010

void setup() {
  SPI.begin();           // Initialize SPI
  pinMode(CS_PIN, OUTPUT);
  digitalWrite(CS_PIN, HIGH); // Deselect chip initially
  Serial.begin(9600);
}

// Set the digital potentiometer (0-255)
void setPot(byte value) {
  digitalWrite(CS_PIN, LOW);   // Select the chip
  SPI.transfer(0x11);          // Command byte for MCP41010
  SPI.transfer(value);          // Wiper position (0-255)
  digitalWrite(CS_PIN, HIGH);  // Deselect the chip
}

void loop() {
  // Sweep the potentiometer from min → max → min
  for (byte i = 0; i <= 255; i++) {
    setPot(i);
    delay(20);
  }
  for (int i = 255; i >= 0; i--) {
    setPot(i);
    delay(20);
  }
}