Pages: 1 [2]   Go Down
Author Topic: hold last sensor value  (Read 1796 times)
0 Members and 1 Guest are viewing this topic.
berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 154
fubbi.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't really understand what you are doing.

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

Quote
Code:
    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 smiley-sad
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

berlin
Offline Offline
Full Member
***
Karma: 0
Posts: 154
fubbi.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
#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);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33419
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2]   Go Up
Jump to: