hold last sensor value

I need it to stay on the last value it read.

That's exactly what it is doing. The last value that it read was a 0. You need to be a little clearer in your requirements.

PaulS:

I need it to stay on the last value it read.

That's exactly what it is doing. The last value that it read was a 0. You need to be a little clearer in your requirements.

Sorry if I was unclear,

Its a 50 cm position sensor and if I press it in the middle and get a value of perhaps 60, I would like the MIDI.sendControlChange to keep sending 60. Until I press again somewhere else and update the "last pressed value".

Thanks

So, you want it to detect the highest value read within some period of time? Remember that the 0 values that it reads are perfectly valid values. Remember also that it will read values from zero up to some high value and then all the way back down to zero.

PaulS:
So, you want it to detect the highest value read within some period of time? Remember that the 0 values that it reads are perfectly valid values. Remember also that it will read values from zero up to some high value and then all the way back down to zero.

If I press 0 that would indeed be the right value for that case. However, I would like it to continously send the last value I pressed circumventing the automatic drop back to 0.

I am building a midi instrument and I am using the position sensor to update filter parameters. Kinda dull if it always snaps back to zero. It should just stay where I last lifted my finger.

cheers

If I press 0 that would indeed be the right value for that case. However, I would like it to continously send the last value I pressed circumventing the automatic drop back to 0.

I don't think what you want to do can be done, then. You can't "press a 0". If you are not touching the sensor, the value that it returns should be 0. When you do touch it, the value read ramps up to some value and then back down to zero when you release it. The Arduino reads a number of values along that curve.

If you want to track the highest value in some time frame, and use that value for some time, that could be done.

But, the Arduino can not tell whether a reading was of some particular meaning to you, and should be maintained until it no longer means anything to you. It does not have an ESP sensor built in.

I hear ya.

thanks!

Hey, since I have a reliable zero (pull-down R) when not pressed. It must be possible to "hold" any last value that wasn't a zero, no?

Yes do you mean something like this:-

tempVal = analogRead(posPin);
if(tempVal !=0) posVal = tempVal;

Grumpy_Mike:
Yes do you mean something like this:-

tempVal = analogRead(posPin);

if(tempVal !=0) posVal = tempVal;

yey! this works:

void loop(){
    delay(30);
    readPos();
    MIDI.sendControlChange(22, posVal, 16);
}

void readPos(){
  tempVal = analogRead(posPin);
  if(tempVal !=0) {
  posVal = tempVal;
  posVal = map(posVal, 1023, 1, 127, 0);
  posVal = constrain(posVal, 0, 127);
}
}
    delay(30);

So, you DID mean periodically? You swore up and down you didn't.

PaulS:

    delay(30);

So, you DID mean periodically? You swore up and down you didn't.

the delay was just in there to slow things down temporarily

:.

Then, the value read from the FSR is going to ramp up and back down. Holding the last non-zero value is not the same as holding the highest value that was reached. But, of course, that might be just fine, if the fall off ramp is very steep (without the delay()).

I was wondering about that. How fast a hardware pulldown occurs VS how fast the arduino loops. But it behaves ok. So if it isn't broken don't fix it, right?

Now that that works I need to figure out how to release it so that I can slide my finger over the ribbon triggering notes form a scale.

I am currently blocking the polling of the sensor by how I wrote my code so the value does not update and the note won't change. Can you point me in the right direction please?

#include <MIDI.h>

int posPin = A0;
int posVal = 0;
int note = 0;
int tempVal = 0;
int led = 13;
int scale[] = {
  60,62,64,65,67,69,71};

#define LED 13

void setup() {
  pinMode(LED, OUTPUT);
  MIDI.begin();            	// Launch MIDI with default options
}

void loop(){
  readPos();
}

void readPos(){
  tempVal = analogRead(posPin);

  if(tempVal == 0) {
    MIDI.sendControlChange(123,0,1); // OMNI NOTE OFF MSG
    digitalWrite(led, LOW);
  }

  if(tempVal !=0) {
    posVal = tempVal;
    posVal = map(posVal, 1023, 1, 127, 0); // make midi notes
    posVal = constrain(posVal, 0, 127); // constrain it
    note = map(tempVal, 1023, 1, 6, 0); // make it 7 for the note array
    note = constrain(note, 0, 6); // constrain it
    note = scale[note]; // pick a note
    MIDI.sendNoteOn(note,127,1); // send the note (note, velocity, channel)
    digitalWrite(led, HIGH);
  }
}

I don't really understand what you are doing.

  if(tempVal == 0) {
    MIDI.sendControlChange(123,0,1); // OMNI NOTE OFF MSG
    digitalWrite(led, LOW);
  }

  if(tempVal !=0) {

If tempVal is not 0, how can it be anything other than not 0? The 2nd if statement should be an else.

    posVal = tempVal;
    posVal = map(posVal, 1023, 1, 127, 0); // make midi notes
    posVal = constrain(posVal, 0, 127); // constrain it

Why? You never use posVal anywhere else.

Now that that works I need to figure out how to release it so that I can slide my finger over the ribbon triggering notes form a scale.

Pressing somewhere else/some other amount should generate another non-zero value.

I don't really understand what you are doing.

Me neither, hence my posting in the forum "programming questions" :slight_smile:

    posVal = tempVal;

posVal = map(posVal, 1023, 1, 127, 0); // make midi notes
    posVal = constrain(posVal, 0, 127); // constrain it



Why? You never use posVal anywhere else.

Its a remain from previous iterations. Does it matter?

If tempVal is not 0, how can it be anything other than not 0? The 2nd if statement should be an else.

This what always confuses me. the logic of if's and else's. I cant figure it out :frowning:

Its a remain from previous iterations. Does it matter?

If it doesn't do anything useful, delete it. It doesn't hurt anything, but it takes time to compile every time, it takes time to upload every time, and it takes up space every time.

This what always confuses me. the logic of if's and else's. I cant figure it out

Else is used when there is something to do if the if statement is false.

If the switch pin is HIGH, turn the LED on. Else, turn it off. The pin can only be HIGH or LOW. There is no possibility of HALFWAYHIGH or NOTQUITELOW.

In the other hand, the values from an analog pin might fall into ranges that you want to treat as LOW, NOTQUITELOW, HALFWAYHIGH, and HIGH

int val = analogRead(somePin);
if(val <= 250)
{
}
else if(val <= 500)
{
}
else if(val <- 750)
{
}
else
{
}

Here, once an if (or else if) evaluates to true, and a block is executed, the rest of the else ifs are skipped, as is the else. If none of the if (or else if) statements are true, the else block will be executed.

I am trying the else I can still not dynamically update the value without releasing.
exactly the same as before

#include <MIDI.h>
#define LED 13

int sensorPin = A0;
int tempVal = 0;
int led = 13;
int note = 0;
int scale[] = {
  60,62,64,65,67,69,71};


void setup() {
  pinMode(LED, OUTPUT);
  MIDI.begin();
}

void loop(){
  tempVal = analogRead(sensorPin);

  if(tempVal == 0) {
    MIDI.sendControlChange(123,0,1); // OMNI NOTE OFF MSG
    digitalWrite(led, LOW);
  }

  else if (tempVal > 1) {
    note = map(tempVal, 1023, 1, 6, 0);
    note = constrain(note, 0, 6);
    MIDI.sendNoteOn(scale[note],127,1);
    digitalWrite(led, HIGH);
  }
}

You need a Serial.begin(115200); in setup() and some Serial.print() statements in loop. What value are you reading from the sensor each time? It is possible that you are not getting the clean 0 that you think you are.

What value is ending up in note?

I can still not dynamically update the value without releasing.

I suspect that you are flooding the MIDI device with lots of note on messages and only on release you have a note off.

You need to see if the value has changed even though it is not gone down to zero. Then turn the old note off and the new note on.