False Potentiometer Reading

I've written this code to function the following way;

Pot1 - controls led blink rate
Pot2 - controls either blink rate or digital pot
Pot3 - controls digital pot

A 3-position toggle determines whether pot 2 controls the blink rate or the digital pot (left or right), with the centre position (both pins HIGH) allowing them to be controlled by their respective pots with no effect.

The problem I have is when the toggle is in the left position allowing pot 2 to control the blink rate, pot 1 still has some influence causing an unsteady/glitching blink rate.

#include <DigiPotX9Cxxx.h>
DigiPot digiPot(2, 1, 19);
int pot1 = A3;
int pot2 = A0;
int pot3 = A4;
int leftToggle = 15;
int rightToggle = 14;
int led = 24;

unsigned long timeMuteChanged = millis();
unsigned long period;
boolean ledOn = false;

void setup() {
  pinMode(leftToggle, INPUT_PULLUP);
  pinMode(rightToggle, INPUT_PULLUP);
  pinMode(led, OUTPUT);

}

void loop() {

  int LT = digitalRead(leftToggle);
  int RT = digitalRead(rightToggle);
  int onVal1 = analogRead(pot1);
  int offVal1 = analogRead(pot1);
  int onVal2 = analogRead(pot2);
  int offVal2 = analogRead(pot2);
  int onVal3 = analogRead(pot3);
  int offVal3 = analogRead(pot3);

  onVal1 = map(onVal1, 0, 1023, 50, 250);
  offVal1 = map(offVal1, 0, 1023, 50, 250);
  onVal2 = map(onVal2, 0, 1023, 50, 250);
  offVal2 = map(offVal2, 0, 1023, 50, 250);
  onVal3 = map(onVal3, 0, 1023, 50, 250);
  offVal3 = map(offVal3, 0, 1023, 50, 250);

  if (millis() - timeMuteChanged >= period) {
    ledOn = !ledOn;
    timeMuteChanged = millis();
    digitalWrite(led, ledOn);
    if (LT == LOW) {
      period = ledOn ? onVal2 : offVal2;
    }
    if (LT == HIGH) {
      period = ledOn ? onVal1 : offVal1;
    }
  }

  if (RT == LOW) {
    int potVal = analogRead(pot2);
    potVal = map(potVal, 0, 1023, 0, 99);
    digiPot.set(potVal);
  }
  if (RT == HIGH) {
    int potVal = analogRead(pot3);
    potVal = map(potVal, 0, 1023, 0, 99);
    digiPot.set(potVal);
  }

}

How much do you expect the reading to have changed in the last 100 microseconds?

1 Like

Can I not create 2 values from the same pot? I have both of those set up so I can change the mapping if need be.

For example

map(onVal1, 0, 1023, 50, 250)
map(offVal1, 0, 1023, 100, 500)

Hello CHARH

Show a picture of Potentiometer used.

Of course you can, but you should be aware that analogRead is an expensive operation, burning around 1600 instruction cycles per call.

Yes, but what would be point of doing that?

Hi, @anon26912280

Can you please post a schematic?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hope this is adequate. As I understand, there's no reason for the code and the PCB to suggest that the pots would affect eachother



Hi, @anon26912280
Can you please tell us, what is your application?
What are you trying to accomplish?

Why do you want two different pots to do the same job?

Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.