Go Down

Topic: hold last sensor value (Read 2322 times) previous topic - next topic

fubbi

Quote

I don't really understand what you are doing.


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

Quote

Code: [Select]
    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?

Quote
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 :(

PaulS

Quote
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.

Quote
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
Code: [Select]
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.

fubbi

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


Code: [Select]
#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);
  }
}

PaulS

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?

Grumpy_Mike

Quote
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.

Go Up