I am using 4 10k pots on A0,A1,A2,A3, an i2c-display and digital pots with spi.
the pots on A0, A1 and A3 work fine. The pot on A2 is interfering with the pot on A1. That means A2 has not an independent value with analogread().
The multimeter gives the right voltage for all four pins, but confusing as it is for me, the printed value for A2 with analogread() is wrong. I tried two different Nanos (same result).
// include the SPI library:
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
/*
R1: B00010001 17
R2: B00010010 18
R1+R2: B00010011 19
*/
// set pin 10 as the slave select for the digital pot:
const int slave_Select_Pin = 10;
int level1 = 0;
int level2 = 0;
int level3 = 0;
int level4 = 0;
int potPin1 = A0;
int potPin2 = A1;
int potPin3 = A2;
int potPin4 = A3;
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0,0);
// set the slaveSelectPin as an output:
pinMode (slave_Select_Pin, OUTPUT);
Serial.begin(9600);
// initialize SPI:
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.begin();
//Set Volume to 0 for all DigiPots
MSP42010PotWrite(slave_Select_Pin, 1, level1);
MSP42010PotWrite(slave_Select_Pin, 2, level2);
MSP42010PotWrite(slave_Select_Pin, 3, level3);
MSP42010PotWrite(slave_Select_Pin, 4, level4);
pinMode(potPin1, INPUT);
pinMode(potPin2, INPUT);
pinMode(potPin3, INPUT);
pinMode(potPin4, INPUT);
}
void loop() {
PotRead(1);
PotRead(2);
PotRead(3);
PotRead(4);
}
void PotRead(int Pot) {
int PotValue;
switch (Pot) {
case 1:
lcd.setCursor(0,0);
lcd.print ("Pot1 ");
PotValue = analogRead(potPin1);
break;
case 2:
lcd.setCursor(0,1);
lcd.print ("Pot2 ");
PotValue = analogRead(potPin2);
break;
case 3:
lcd.setCursor(0,2);
lcd.print ("Pot3 ");
PotValue = analogRead(potPin3);
break;
case 4:
lcd.setCursor(0,3);
lcd.print ("Pot4 ");
PotValue = analogRead(potPin4);
break;
}
lcd.print (PotValue);
MSP42010PotWrite(slave_Select_Pin, Pot, PotValue/4);
}
void MSP42010PotWrite(int slaveSelectPin, int potSelect , int value) {
// take the SS pin low to select the chip:
digitalWrite(slaveSelectPin,LOW);
switch (potSelect) {
case 1:
SPI.transfer(17);
SPI.transfer(value);
break;
case 2:
SPI.transfer(18);
SPI.transfer(value);
break;
case 3:
SPI.transfer(17);
SPI.transfer(value);
SPI.transfer(0); // Pot 2
SPI.transfer(0); // Pot 2
break;
case 4:
SPI.transfer(18);
SPI.transfer(value);
SPI.transfer(0); // Pot 2
SPI.transfer(0); // Pot 2
break;
}
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin,HIGH);
}