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; }
lastfaderValue
is always equal tofaderPos
.loop
you’re comparing the pot input with an arbitrary value that you think is the last pot value, but since it’s local toloop
, it isn’t. You may have not intended that to be a local variable. I’d add that you’ll probably want a minimum pot delta as well since simultaneous reads may not report the same value even if it’s not being moved.