Pages: 1 [2]   Go Down
 Author Topic: hold last sensor value  (Read 620 times) 0 Members and 1 Guest are viewing this topic.
berlin
Offline
Full Member
Karma: 0
Posts: 150
fubbi.com
 « Reply #15 on: December 13, 2012, 10:22:52 am » Bigger Smaller Reset

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

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

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
 Logged

Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35584
Seattle, WA USA
 « Reply #16 on: December 13, 2012, 10:34:18 am » Bigger Smaller 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:
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
Full Member
Karma: 0
Posts: 150
fubbi.com
 « Reply #17 on: December 13, 2012, 11:05:14 am » Bigger Smaller 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(){

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
Online
Brattain Member
Karma: 316
Posts: 35584
Seattle, WA USA
 « Reply #18 on: December 13, 2012, 11:58:47 am » Bigger Smaller 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
Brattain Member
Karma: 277
Posts: 25566
Solder is electric glue
 « Reply #19 on: December 13, 2012, 11:59:56 am » Bigger Smaller 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