Reading of Soft potentiometer values from the serial port

Hello everyone!
I am carrying out a project that includes three buttons, which turn on three LEDs and activate 2 potentiometers and 2 soft potentiometers.
I'm having problems reading the soft potentiometer values, I would like the serial port to print the last value I touched on the soft potentiomter, while when I lift my finger the serial port reads the lowest value of the range, such as 1 .
These values of the soft potentiometer will then be read also on Max msp.
I attach the code below. (I only write the code related to the activation button of the led and the soft potentiometer).


bool lastValue3; // white button
bool toggleStatus3 = false;
bool digitalValueConfirmed3 = false;
unsigned longtimePast3 = 0;
const byte whitebutton = 7;
const byte ledBianco = 10;
const byte analogPin3 = A2; // softpot
const byte analogPin4 = A1; // potentiometer

int currentValue;

void setup () {
  pinMode (whitebutton, INPUT);
  pinMode (ledBianco, OUTPUT);


  Serial.begin (9600);

}

void loop () {
  unsigned longtimeActual = millis (); 


  // debounce and toggle white button
  if (digitalRead (whitebutton) == 1) {
    timePast3 = timeActual;
    digitalValueConfirmed3 = 1;
  } else {
    if (timeActual - timePast3> 50) // Has some time passed?
      digitalValueConfirmed3 = 0;
  }
  if ((lastValue3 == 0) && (digitalValueConfirmed3 == 1)) {
    toggleStatus3 =! toggleStatus3;
    // so that I touch, I reverse the toggle
  }
  digitalWrite (ledBianco, toggleStatus3);
  lastValue3 = digitalValueConfirmed3;

  int val4 = analogRead (analogPin4);

  int gran = map (val3, 0, 1023, 1, 100);
  currentValue = map (val3, 0, 1024, 0, 1000);
  int lastVal = map (currentValue, 0, 1000, 0, 50);
  int gran2 = map (val4, 0, 1023, 0, 50);
  
  if ((toggleStatus3 == HIGH) and (currentValue != gran)) {
    Serial.print ("gran1");
    Serial.println (lastVal);
    Serial.print ("gran2");
    Serial.println (gran2);
  }

  }


 }

Would anyone know how to help me?
Thank you all!

Hello @loren00 could you tell me which pot are you using?

Hello! i am using this:
SoftPot Membrane Potentiometer - 50mm, SEN-08680

bool lastValue3; // white button
bool toggleStatus3 = false;
bool digitalValueConfirmed3 = false;
unsigned longtimePast3 = 0;
const byte whitebutton = 7;
const byte ledBianco = 10;
const byte analogPin3 = A2; // softpot
const byte analogPin4 = A1; // potentiometer

int val4=0;
int currentValue;

void setup () {
  pinMode (whitebutton, INPUT);
  pinMode (ledBianco, OUTPUT);


  Serial.begin (9600);

}

void loop () {
  unsigned longtimeActual = millis (); 


  // debounce and toggle white button
  if (digitalRead (whitebutton) == 1) {
    timePast3 = timeActual;
    digitalValueConfirmed3 = 1;
  } else {
    if (timeActual - timePast3> 50) // Has some time passed?
      digitalValueConfirmed3 = 0;
  }
  if ((lastValue3 == 0) && (digitalValueConfirmed3 == 1)) {
    toggleStatus3 =! toggleStatus3;
    // so that I touch, I reverse the toggle
  }
  digitalWrite (ledBianco, toggleStatus3);
  lastValue3 = digitalValueConfirmed3;

  val4 = analogRead (analogPin4);

  int gran = map (val3, 0, 1023, 1, 100);
  currentValue = map (val3, 0, 1024, 0, 1000);
  int lastVal = map (currentValue, 0, 1000, 0, 50);
  int gran2 = map (val4, 0, 1023, 0, 50);
  
  if ((toggleStatus3 == HIGH) and (currentValue != gran)) {
    Serial.print ("gran1");
    Serial.println (lastVal);
    Serial.print ("gran2");
    Serial.println (gran2);
  }

  }


 }

Please try with this code

I did the same reasoning but it doesn't work

So when you release your finger the value changes into 0?
And if you touch change to another value, right?

exact

I see the problem, but first, replace the soft sensor with a normal rotary potentiometer and let me know the behavior.

when using the rotary potentiometer there is no such problem, as it remains in the same position and the serial port always prints the same value.

Yes that is the point, the way that your sensor works always return to 0 always.

exact the rotary potentiometer does not give 0 but the value on which it is stopped

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