code for potentiometer

Hello
I am trying to write a program that allows a sip/puff device to function as a potentiometer.
The idea is that I want to control knobs and parameters in Ableton Live by sipping or puffing.
This should allow people with disabilities to control effect manipulation in music.

I have established the device as a potentiometer on A0, and I can read the values in the serial port.
When I sip or puff I see the values change respectively up and down from neutral 512-513.
The problem, however, is that when I stop sipping or puffing the read signal reverts to neutral.

Figuratively speaking, this is like having a spring attached to a pot that always reverts itself to neutral when released. I need Arduino to understand that a sip or a puff should move the value either up or down, and just hold the current value it is on when the sip or puff stops until it is reactivated.

I have tried with the following if commands but it doesn't work.
If any of you can help me with this it would be much appreciated.

int PotPin = A0;
int readValue;
int threshold1=513;
int threshold2=512;

void setup() {
Serial.begin(9600);
pinMode(PotPin, INPUT);

}

void loop() {

readValue = analogRead(PotPin);
Serial.println=(readValue);

if (readValue == threshold1) {
readValue=0;
}
if (readValue == threshold2) {
readValue=0;
}

}

Serial.println=(readValue);I’m curious about what the compiler has to say about that.

Also, when using analogue values don’t use equality as a test, use a small range.

It should indeed be:
Serial.println(readValue);

But remember that the loop is constantly running as a … loop. So when it ends,it begins again. Then, the next instructions after your ifs is readValue = analogRead(PotPin);

So, in the if blocks you set a value for readValue but immediately after you set it again to the potentiometer reading, without even using it.
I guess your loop should be like this :

void loop() {
 readValue = analogRead(PotPin);

 if (readValue == threshold1) readValue = 0;
 if (readValue == threshold2) readValue = 0;

 Serial.println(readValue);
}

And, as Prince says, use a range of values instead of a single value, like this:

  if (readValue <= threshold1 && readValue >= threshold2) readValue = 0;

(depending on what you want to do, of course)

[quote author=lesept link=msg=4383650 date=1574692095

And, as Prince says, use a range of values instead of a single value, like this:

  if (readValue <= threshold1 && readValue >= threshold2) readValue = 0;

(depending on what you want to do, of course)
[/quote]

Maybe it is the thresholds I get wrong? Sometimes it should be respectively lesser and greater than threshold levels.
The thing is I open the serial monitor and when I dont do anything I just see an endless streams of:
512
512
512
513
(etc.)

When I puff it goes up:
512
645
750
823
(etc)

When I sip it goes down:
512
480
322
289
(etc)

When I let go it just reverts to the stream of 512’s and 513’s

Lets assume that the range is 0-1023. 0 is no volume and 1023 is max volume.
Imagine that you are turning the volume up or down using sips and puffs, then it would be annoying if it always went back to zero when you stopped turning. The fader, or volume controller, should stay in the position you left it and move up and down from that position when you turn it again. This is the thing I want to achieve

What about using a pot with a center tap?
It would be like having 2 pots connected in series, with the common point available as a terminal.
You could read the 2 sections as 2 separate analog inputs, and use the center position for “no effects” and then use one half for sip and the other for puff (even if I have no clue on what sip and puff are :slight_smile:
There are models with a mid-travel detent, that would allow you to bring them back to the center position in a snap (no pun )

IW7EHC:
What about using a pot with a center tap?
It would be like having 2 pots connected in series, with the common point available as a terminal.
You could read the 2 sections as 2 separate analog inputs, and use the center position for “no effects” and then use one half for sip and the other for puff (even if I have no clue on what sip and puff are :slight_smile:
There are models with a mid-travel detent, that would allow you to bring them back to the center position in a snap (no pun )

Thank you!
That might be an interesting idea.
To clarify, a sip and puff is a control method which allows people with limited mobility to operate devices like computers, wheelchairs etc. by sipping(inhaling) or puffing (exhaling) through a tube :slight_smile:

O.k. but I can't work out how you can tell that the user has stopped sipping or puffing. What exactly is the device that you're using? If it has a specific output to tell you nothing is happening that would be easy but without something like that how do you know?

Presumably 512 is a valid value for the user to select (half volume or whatever) so you can't just use the fact that the value has returned to 512.

Steve

slipstick:
O.k. but I can't work out how you can tell that the user has stopped sipping or puffing. What exactly is the device that you're using? If it has a specific output to tell you nothing is happening that would be easy but without something like that how do you know?

Presumably 512 is a valid value for the user to select (half volume or whatever) so you can't just use the fact that the value has returned to 512.

Steve

Hey Steve.
I am using an MPXV7002. It is a pressure sensor to which I have attached two 1mm plastic tubes.
When I call up the serial port it gives me a row of 512 and 513 until I inhale or exhale through the tube.
Please let me know if this is a bad reply, Im new to Arduino

It's not a bad reply but it doesn't answer the basic question. How can you tell if the value is now 512 because the user has stopped blowing and would like you to hold the output at the value before he stopped blowing or the user has actually selected 512 and wants you to ignore any previous values?

Can you do something like hold it at the maximum (or minimum) value achieved since it was last in the middle? That would mean that if you blow too hard so e.g. the volume is too high you'd have to stop blowing and then try again to get to the value you wanted. Is that a practical way of working or have you already thought of something more useful? If you can describe it then it's almost certainly possible to code but the requirement needs to defined more clearly than "act like a potentiometer".

Steve

It seems you want to use the value of the pressure sensor to change the value of another value.

So if sensorValue > (512+threshold), increase that other value by one each second,
and if sensorValue < (512-threshold), decrease that other value by one each second.

Could have more than one threshold, to change that other value faster.

That other value could be 0-100%, or whatever other range you require.

Edit: Maybe better to change time between steps.
Try this (untested) sketch.
Leo…

const byte sensorPin = A0; // pressure sensor pin
unsigned long currentMillis, previousMillis, interval;
unsigned long fastChange = 20; // ms per step
unsigned long slowChange = 500;
int sensorValue;
int sensorOffset = 512; // sensor idle
int threshold = 50; // deadband
byte volumeValue = 20; // some preset

void setup() {
 Serial.begin(9600);
 Serial.println("Volume value is");
 Serial.println(volumeValue);
}

void loop() {
 currentMillis = millis(); // fast update
 if (millis() - previousMillis > interval) { // variable time-out
   sensorValue = analogRead(sensorPin);
   if (sensorValue > sensorOffset + threshold) {
     if (volumeValue < 255) volumeValue ++;
     Serial.println(volumeValue);
     interval = map(sensorValue, sensorOffset + threshold, 1023, slowChange, fastChange);
   }
   if (sensorValue < sensorOffset - threshold) {
     if (volumeValue > 0) volumeValue --;
     Serial.println(volumeValue);
     interval = map(sensorValue, sensorOffset - threshold, 0, slowChange, fastChange);
   }
   previousMillis = currentMillis;
 }
}

Edit: corrected an error.

The sensor is a differential pressure probe.
one tube is exposed to ambient pressure, while the second tube is exposed to the puff/sip:

Puff will create a positive differential (pressure on the puff side > pressure on the other side at ambient pressure), and drive the output to the positive range 512 to 1023.

Sip will create a negative differential (pressure on the puff side < pressure on the other side at ambient pressure), and drive the output to the negative range 0 to 512.

what you see at power up in stand still conditions (512) is the output of the transducer that sits right at the middle of the range (0-1023 counts on a 10 bit ADC), good news is that the transducer is not busted :slight_smile: (yet) :slight_smile:

The only way you have to hold the peak value (as suggested above)either positive-puff or negative-sip, is to manipulate the data, and hold it there, but then how do you reset it?
Fixed time, or wait until the next change in pressure?

Hi
As @Wawa has said, you need to interpret the higher than threshold as INCREASE the potentiometer and lower than threshold DECREASE the potentiometer.

Not as the absolute values of the potentiometer position.

Tom... :slight_smile:

Presumably the output returns to around the middle value when the user stops sipping or puffing.

You can detect when the value becomes greater or lower than a threshold in either direction and cause a response once or detect when the value is greater or lower than a threshold in either direction and continue to respond. Which one you do depends on the requirement.

The code in post#9 does not only use a blow threshold, but anything over that threshold is converted with map() to a speed value that changes the output value slower or faster depending on how hard you blow.
Plenty of room to experiment with the values though, and it might not even work as expected.
Would be nice to hear back from the user.
Leo…

Wawa:
The code in post#9 does not only use a blow threshold, but anything over that threshold is converted with map() to a speed value that changes the output value slower or faster depending on how hard you blow.
Plenty of room to experiment with the values though, and it might not even work as expected.
Would be nice to hear back from the user.
Leo..

I like it.... :slight_smile: :slight_smile: :slight_smile: :slight_smile: