user60029: I'm using an Arduino Nano with a capacitive touch sensor and a potentiometer. I'm trying to get my code to differentiate between three different program states.

One is when my touch sensor is NOT being touched.
Two is when my touch sensor IS being touched.
And three is when my touch sensor is being touched AND a potentiometer is being moved at the same time.

I'm having trouble picking up the second program state, when I'm simply touching the fader but not moving the potentiometer. Program state 1 works great, but program state 3 activates even when there's no data coming in from the potentioeter.
Here's the code. And ideas?
#include <CapacitiveSensor.h>
#define rxPin 4
#define txPin 1

const byte touchSend = 2;
const byte touchReceive = 7;

byte minimumCp = 200; // Raise if the fader is too sensitive (0-16383)

bool touched = false; //Is the fader currently being touched?

CapacitiveSensor touchSensor = CapacitiveSensor(touchReceive, touchSend);

void setup() {
  touchSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode(rxPin, INPUT );
  pinMode(txPin, OUTPUT); 
  Serial.begin(57600);
}
void loop() {
  int faderPos = analogRead(A7);
  int lastfaderValue;
  int totalCp =  touchSensor.capacitiveSensor(30);
  if (totalCp <= minimumCp) {  // Not Touching Fader
      touched = false;
      Serial.println("Not Touching Fader"); 
      delay(15);}
  if ((faderPos == lastfaderValue) && (totalCp > minimumCp)) { // Touching Fader
      touched = true; 
      Serial.println("Touching Fader, NOT Moving"); 
      delay(15);}
  if ((faderPos != lastfaderValue) && (totalCp > minimumCp)) { // Touching Fader and Moving
      touched = true; 
      Serial.println("Touching Fader, Moving"); 
      delay(15);}
  lastfaderValue = faderPos; }


user60029: Figured it out.
I had the lastfaderValue integer in the wrong spot. It works when I put it at the beginning of the code (before the setup).
I also added a tolerance, so that the potentiometer can still fluctuate slightly when I'm not touching it.
Works well now.

60029: <p>I'm using an Arduino Nano with a capacitive touch sensor and a potentiometer. I'm trying to get my code to differentiate between three different program states.</p>
<ul>
<li>One is when my touch sensor is NOT being touched.</li>
<li>Two is when my touch sensor IS being touched.</li>
<li>And three is when my touch sensor is being touched AND a potentiometer is being moved at the same time.</li>
</ul>
<p>I'm having trouble picking up the second program state, when I'm simply touching the fader but not moving the potentiometer. Program state 1 works great, but program state 3 activates even when there's no data coming in from the potentioeter.</p>
<p>Here's the code. And ideas?</p>
<pre><code>#include &lt;CapacitiveSensor.h&gt;
#define rxPin 4
#define txPin 1

const byte touchSend = 2;
const byte touchReceive = 7;

byte minimumCp = 200; // Raise if the fader is too sensitive (0-16383)

bool touched = false; //Is the fader currently being touched?

CapacitiveSensor touchSensor = CapacitiveSensor(touchReceive, touchSend);

void setup() {
  touchSensor.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode(rxPin, INPUT );
  pinMode(txPin, OUTPUT); 
  Serial.begin(57600);
}
void loop() {
  int faderPos = analogRead(A7);
  int lastfaderValue;
  int totalCp =  touchSensor.capacitiveSensor(30);
  if (totalCp &lt;= minimumCp) {  // Not Touching Fader
      touched = false;
      Serial.println(&quot;Not Touching Fader&quot;); 
      delay(15);}
  if ((faderPos == lastfaderValue) &amp;&amp; (totalCp &gt; minimumCp)) { // Touching Fader
      touched = true; 
      Serial.println(&quot;Touching Fader, NOT Moving&quot;); 
      delay(15);}
  if ((faderPos != lastfaderValue) &amp;&amp; (totalCp &gt; minimumCp)) { // Touching Fader and Moving
      touched = true; 
      Serial.println(&quot;Touching Fader, Moving&quot;); 
      delay(15);}
  lastfaderValue = faderPos; }
</code></pre>
 60029: <p>Figured it out.</p>
<p>I had the lastfaderValue integer in the wrong spot. It works when I put it at the beginning of the code (before the setup).</p>
<p>I also added a tolerance, so that the potentiometer can still fluctuate slightly when I'm not touching it.</p>
<p>Works well now.</p>
