code correction

Anyone know why this code will not work on all analog inputs? I have a teensy 3.6 that has 22 analog inputs most have piezos connected to trigger midi notes. pins 14-22 won't work, I tested the pins on another program and they all work showing the voltage jump on the serial monitor when taped same as the others. Is there something in the code?

[code]

/*
   MIDIUSB_test.ino

   Created: 4/6/2015 10:47:08 AM
   Author: gurbrinder grewal
   Modified by Arduino LLC (2015) & Grumpy Mike
*/

#include "MIDIUSB.h"

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void setup() {
  Serial.begin(115200);
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void loop() {
  int val;
  val = analogRead(0);
  if (val > 600) { // if it is greater than the threshold
    noteOn(0, 50, 127); // send a note on message the 68 is the pitch of the note
    MidiUSB.flush(); // send the MIDI message
    while (analogRead(0) > 600) { } // wait here until the signal has dropped
    noteOff(0, 50, 127); // send the note off message
  }
  MidiUSB.flush(); // send the MIDI message

  {
    int val;
    val = analogRead(1);
    if (val > 600) { 
      noteOn(0, 51, 127); 
      MidiUSB.flush(); 
      while (analogRead(1) > 600) { }
      noteOff(0, 51, 127);
    } 
    MidiUSB.flush(); 
  }
  
  {
    int val;
    val = analogRead(2);
    if (val > 600) { 
      noteOn(0, 52, 127); 
      MidiUSB.flush(); 
      while (analogRead(2) > 600) { } 
      noteOff(0, 52, 127);
    } 
    MidiUSB.flush();
  }

  {
    int val;
    val = analogRead(3);
    if (val > 600) { 
      noteOn(0, 53, 127); 
      MidiUSB.flush(); 
      while (analogRead(3) > 600) { } 
      noteOff(0, 53, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(4);
    if (val > 600) { 
      noteOn(0, 54, 127); 
      MidiUSB.flush(); 
      while (analogRead(4) > 600) { } 
      noteOff(0, 54, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(5);
    if (val > 600) {
      noteOn(0, 55, 127); 
      MidiUSB.flush(); 
      while (analogRead(5) > 600) { } 
      noteOff(0, 55, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(6);
    if (val > 600) { 
      noteOn(0, 56, 127); 
      MidiUSB.flush(); 
      while (analogRead(6) > 600) { } 
      noteOff(0, 56, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(7);
    if (val > 600) { 
      noteOn(0, 57, 127); 
      MidiUSB.flush(); 
      while (analogRead(7) > 600) { } 
      noteOff(0, 57, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(8);
    if (val > 600) { 
      noteOn(0, 58, 127); 
      MidiUSB.flush(); 
      while (analogRead(8) > 600) { } 
      noteOff(0, 58, 127);
    } 
    MidiUSB.flush();
  }

  {
    int val;
    val = analogRead(9);
    if (val > 600) { 
      noteOn(0, 59, 127); 
      MidiUSB.flush(); 
      while (analogRead(9) > 600) { } 
      noteOff(0, 59, 127);
    } 
    MidiUSB.flush(); 
  }

   {
    int val;
    val = analogRead(10);
    if (val > 600) { 
      noteOn(0, 60, 127);
      MidiUSB.flush(); 
      while (analogRead(10) > 600) { } 
      noteOff(0, 60, 127);
    } 
    MidiUSB.flush();
  }

  {
    int val;
    val = analogRead(11);
    if (val > 600) { 
      noteOn(0, 61, 127); 
      MidiUSB.flush(); 
      while (analogRead(11) > 600) { } 
      noteOff(0, 61, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(12);
    if (val > 600) { 
      noteOn(0, 62, 127); 
      MidiUSB.flush(); 
      while (analogRead(12) > 600) { } 
      noteOff(0, 62, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(13);
    if (val > 600) { 
      noteOn(0, 63, 127); 
      MidiUSB.flush(); 
      while (analogRead(13) > 600) { } 
      noteOff(0, 63, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(14);
    if (val > 600) { 
      noteOn(0, 64, 127); 
      MidiUSB.flush(); 
      while (analogRead(14) > 600) { } 
      noteOff(0, 64, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(15);
    if (val > 600) { 
      noteOn(0, 65, 127); 
      MidiUSB.flush(); 
      while (analogRead(15) > 600) { } 
      noteOff(0, 65, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(16);
    if (val > 600) { 
      noteOn(0, 66, 127); 
      MidiUSB.flush(); 
      while (analogRead(16) > 600) { } 
      noteOff(0, 66, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(17);
    if (val > 600) { 
      noteOn(0, 67, 127); 
      MidiUSB.flush(); 
      while (analogRead(17) > 600) { } 
      noteOff(0, 67, 127);
    } 
    MidiUSB.flush();
  }

  {
    int val;
    val = analogRead(18);
    if (val > 600) { 
      noteOn(0, 68, 127); 
      MidiUSB.flush(); 
      while (analogRead(18) > 600) { }
      noteOff(0, 68, 127);
    } 
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(19);
    if (val > 600) { 
      noteOn(0, 69, 127); 
      MidiUSB.flush(); 
      while (analogRead(19) > 600) { } 
      noteOff(0, 69, 127);
    } 9
    MidiUSB.flush(); 
  }

  {
    int val;
    val = analogRead(20);
    if (val > 600) { 
      noteOn(0, 70, 127); 
      MidiUSB.flush(); 
      while (analogRead(20) > 600) { } 
      noteOff(0, 70, 127);
    } 
    MidiUSB.flush();
  }

 {
    int val;
    val = analogRead(21);
    if (val > 600) { 
      noteOn(0, 71, 127); 
      MidiUSB.flush(); 
      while (analogRead(21) > 600) { } 
      noteOff(0, 71, 127);
    } 
    MidiUSB.flush();
  }
  
  {
    int val;
    val = analogRead(22);
    if (val > 600) { 
      noteOn(0, 72, 127); 
      MidiUSB.flush(); 
      while (analogRead(22) > 600) { } 
      noteOff(0, 72, 127);
    } 
    MidiUSB.flush(); 
  }


}

[/code]

You'd be better off asking about a Teensy 3.6 on the Teensy forums.

Pete

It seems strange to copy/paste a block of code that reads from an analog pin, make no changes to the pasted code, and then complain that the pasted code won't read from another pin.

It is pointless to copy/paste code and bloat loop(), when a function could have been used, instead.

I only see one read from analog pin 0 and a whole bunch from analog pin 1.

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);

Looks like you edited the code in the original post. Though pin 19 reads 19 then waits on pin 1 (you'll probbly want to fix that).

What does it do or not do now?

I fixed 19 on the post, the version I used does not have those mistakes. Pins 0-13 work fine, 14-22 do nothing they test fine on the serial monitor with a different sketch which is why I wonder if there's something in this code.

I wonder if it is getting confused because 14 through 22 are the digital pin numbers for A0 through A8.

Perhaps you should try the names A0 through A22?

Genius!!! it works! Thank you. I’m struggling to learn enough to complete this project I have one other part, the hi hat pedal. This is what I’m trying to do but not sure how to go about it.

The pedal itself triggers 2 different midi notes.
pedal tapped down triggers note #58 (hh chick)
tapped down but quickly released triggers note #59 (hh splash)

The pedal also controls which one of 3 notes is played on pad #4
pedal up note #60 (open hh)
half way note #61 (half open hh)
pedal down NOTE #62 (closed hh)

any suggestions or if I’m on the right track to write this code

if value of (A0) goes below threshold and stays for ? milliseconds play note #58
if value of (A0) goes below threshold and stays for ? milliseconds play note #59

if value of (A0)> 400 (A1) plays note #60
if value of (A0)> 200 but < 300 (A1) plays note #61
if value of (A0)< 200 (A1) plays note #62

edrummer:
any suggestions or if I’m on the right track to write this code

if value of (A0) goes below threshold and stays for ? milliseconds play note #58
if value of (A0) goes below threshold and stays for ? milliseconds play note #59

if value of (A0)> 400 (A1) plays note #60
if value of (A0)> 200 but < 300 (A1) plays note #61
if value of (A0)< 200 (A1) plays note #62

I would suggest you draw out a flowchart or the steps of what should happen from the start with the pedal not being pressed to it being pressed. It will help you determine if what you have programmed or I will suggest is correct (we could both be right or wrong).

You have the if statements almost generated. The only thing missing is to add the timer check.

You can call millis() to get the time. At the top of the statements you currently have, add timenow=millis(). (you definitely don’t want to or need to call it for each if statement). The proper way to check if the time has elapsed is (timenow - starttime)>interval. That should take care of checking the time to be more than the interval.

This is the part that will be questionable and is going to get long.

When an interval is first met, store starttime (starttime=timenow); but only when the threshold is first met. When the threshold is not met, this status will have to be reset or it won’t detect again. Then if (timenow - starttime)>interval is true then play the note.

I’m thinking something like this:

if value of (A0) goes below threshold and stays for interval1 milliseconds play note #58
if value of (A0) goes below threshold and stays for interval2 milliseconds play note #59

***interval1 must be less than interval2

if (A0<x)
{
if (A0started)
{
if ((timenow-starttimeAO)>interval2) && (A0started)
{ play note #59 }
else if ((timenow-starttimeAO)>interval1) && (A0started)
{ play note #58 }
}
else
{
starttimeA0=timenow;
A0started=TRUE;
}
}
else
{ A0started=FALSE; }

I’m not saying this is correct or will work, but a pseudo-code example of what is in my head.

The other suggestion will be to start modularizing the program so you don’t have to type or fix the same error 23 times.

I appreciate your suggestion, I was able to trigger 1 note with the FSR just by modifying the code to read < 10 instead of >600. I’ll experiment with your’s tomorrow.

  {
    int val;
    val = analogRead(A0);
    if (val < 10) { 
      noteOn(0, 51, 127); 
      MidiUSB.flush(); 
      while (analogRead(A0) < 10) { }
      noteOff(0, 51, 127);
      MidiUSB.flush(); 
    } 
    
  } 



  }

I’m not sure how to do this ( change which note pin A1 plays by changing the value of pin 0) feel to correct.

{
    int val;
    val = analogRead(A1);
    if (val > 600 and val of pin 0 is > 400) { 
      noteOn(0, 60, 127); 
      MidiUSB.flush(); 
      while (analogRead(A1) > 600 and val of pin 0 is > 400) { } 
      noteOff(0, 60, 127);
      MidiUSB.flush();
    } 
    
    int val;
    val = analogRead(A1);
    if (val > 600 and val of pin 0 is < 400 but > 200) { 
      noteOn(0, 61, 127); 
      MidiUSB.flush(); 
      while (analogRead(A1) > 600 and val of pin 0 is < 400 but > 200 ) { } 
      noteOff(0, 61, 127);
      MidiUSB.flush();
    }
 
    {
    int val;
    val = analogRead(A1);
    if (val > 600 and val of pin 0 is < 200) { 
      noteOn(0, 62, 127); 
      MidiUSB.flush(); 
      while (analogRead(A1) > 600 and val of pin 0 is < 200) { } 
      noteOff(0, 62, 127);
      MidiUSB.flush();
    } 
    
]

I'm not sure how to do this ( change which note pin A1 plays by changing the value of pin 0) feel to correct.

Well, first you have to read the value of pin 0...

Next, you have to have some idea what note to play when the pin 0 value is 0, when it is 1, when it is 2, etc.

Third, you write the code to map the pin 0 value to a note.

Finally, you replace the hard-coded note number in that snippet with the variable note number.

    val = analogRead(A1);
    if (val > 600 and val of pin 0 is > 400) { 
      noteOn(0, 60, 127); 
      MidiUSB.flush(); 
      while (analogRead(A1) > 600 and val of pin 0 is > 400) { } 
      noteOff(0, 60, 127);
      MidiUSB.flush();
    } 
    
    int val;
    val = analogRead(A1);
    if (val > 600 and val of pin 0 is < 400 but > 200) { 
      noteOn(0, 61, 127); 
      MidiUSB.flush(); 
      while (analogRead(A1) > 600 and val of pin 0 is < 400 but > 200 ) { } 
      noteOff(0, 61, 127);
      MidiUSB.flush();
    }

The direct answer is to make another variable for pin 0 for use in the if and while statements. Then make sure the conditions of the two blocks of code are mutually exclusive.

There are several architecture issues starting to develop in your program.
These two blocks of code read analog pin 1 twice. That’s going to slow things down. Instead of constantly reading the pins through out the code, I suggest reading all the pins at the top and assigning the values into an array val[0…22] then using val throughout the code.

Based on what I just told you to do, you are likely going to add val0=analogRead(A0) directly before or after val = analogRead(A1). As there is only one ADC and the inputs are multiplexed, when switching from pin to pin there is a high chance the reading of the second pin is partially based on the input voltage of the first pin. When switching between pins, the new pin should be read and discarded, then read again and the value retained. For example:

junk = analogRead(A0);
val[0] = analogRead(A0);
junk = analogRead(A1);
val[1] = analogRead(A1);

Later we can talk about using the ADC interrupt to get the reading to allow the note programming to execute while the ADC values are updated, as each analogRead take 0.1ms and you will now have a series of 44 reads (or the program will spend 4.4ms waiting and reading ADC pins).

When you say reading all the pins at the top you mean #define? I was planning to get the pedal working while the code is in blocks so I can focus on pin A0 which is the pedal, and get the second note to trigger. Then move on to the pedal value changing the 3 notes on A1. After I get those 2 things working then I can focus on refining the code.

When you say reading all the pins at the top you mean #define?

No. He/she means "at the top of loop()".