Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Mixing codes

  1. #1
    Junior Member
    Join Date
    Oct 2016
    Posts
    3

    Mixing codes

    Hey there,

    after observing many posts and many many applications of the Teensy, I tried to go on by myself in this cruel world.

    That was quite successful, thanks to this forum and the great things I learned here.
    Until now...

    I have two codes that work separately that I took here and adapt or not to my needs.
    The thing is now I want to mix them to finally have exactly what I need.

    The one on the left is good, but I need to use the one on the right to "map" perfectly all my 8 analog inputs as it does for only one.
    I quite see how to mix them but hardly tried for two days now, and I must admit that I am not good enough at coding to do that.

    Is there any kind people that may help me?
    The values on the "map" of the right sketch are the ones I want to use for all the 8 analog inputs. That for sending MIDI(each analog input on its own CC), as it is done on the two sketches.


    I know this doesn't look natural to do that as I assume you may understand why I am using the Teensy. But, trust me, this is the final solution...

    In advance, thank you very very much, you'll save my week!

    #include <Bounce.h>

    // define how many pots are active up to number of available analog inputs
    #define analogInputs 8
    // make arrays for input values and lagged input values
    int inputAnalog[analogInputs];
    int iAlag[analogInputs];
    // make array of cc values
    int ccValue[analogInputs];
    // index variable for loop
    int i;



    Bounce button0 = Bounce(0, 3);
    Bounce button1 = Bounce(1, 3);
    Bounce button2 = Bounce(2, 3);
    Bounce button3 = Bounce(3, 3);
    Bounce button4 = Bounce(4, 3);
    Bounce button5 = Bounce(5, 3);
    Bounce button6 = Bounce(6, 3);
    Bounce button7 = Bounce(7, 3);
    Bounce button8 = Bounce(8, 3);
    Bounce button9 = Bounce(9, 3);
    Bounce button10 = Bounce(10, 3);







    void setup() {
    // MIDI rate
    Serial.begin(31250);
    pinMode(0, INPUT_PULLUP);
    pinMode(1, INPUT_PULLUP);
    pinMode(2, INPUT_PULLUP);
    pinMode(3, INPUT_PULLUP);
    pinMode(4, INPUT_PULLUP);
    pinMode(5, INPUT_PULLUP);
    pinMode(6, INPUT_PULLUP);
    pinMode(7, INPUT_PULLUP);
    pinMode(8, INPUT_PULLUP);
    pinMode(9, INPUT_PULLUP);
    pinMode(10, INPUT_PULLUP);

    }

    void loop() {
    // loop trough active inputs for knobs
    for (i=0;i<analogInputs;i++){
    // read current value at i-th input
    inputAnalog[i] = analogRead(i);
    // if magnitude of difference is 8 or more...
    if (abs(inputAnalog[i] - iAlag[i]) > 7){
    // calc the CC value based on the raw value
    ccValue[i] = inputAnalog[i]/8;
    // send the MIDI
    usbMIDI.sendControlChange(i, ccValue[i], 3);
    // set raw reading to lagged array for next comparison
    iAlag[i] = inputAnalog[i];
    }
    delay(5); // limits MIDI messages to reasonable number
    }

    // Push Button code
    button0.update();
    button1.update();
    button2.update();
    button3.update();
    button4.update();
    button5.update();
    button6.update();
    button7.update();
    button8.update();
    button9.update();
    button10.update();


    if (button0.fallingEdge())
    {
    usbMIDI.sendNoteOn(50, 90, 10);
    }
    if (button1.fallingEdge())
    {
    usbMIDI.sendNoteOn(51, 90, 10);
    }
    if (button2.fallingEdge())
    {
    usbMIDI.sendNoteOn(52, 90, 10);
    }
    if (button3.fallingEdge())
    {
    usbMIDI.sendNoteOn(53, 90, 10);
    }
    if (button4.fallingEdge())
    {
    usbMIDI.sendNoteOn(54, 90, 10);
    }
    if (button5.fallingEdge())
    {
    usbMIDI.sendNoteOn(55, 90, 10);
    }
    if (button6.fallingEdge())
    {
    usbMIDI.sendNoteOn(56, 90, 10);
    }
    if (button7.fallingEdge())
    {
    usbMIDI.sendNoteOn(57, 90, 10);
    }
    if (button8.fallingEdge())
    {
    usbMIDI.sendNoteOn(58, 90, 10);
    }
    if (button9.fallingEdge())
    {
    usbMIDI.sendNoteOn(59, 90, 10);
    }
    if (button10.fallingEdge())
    {
    usbMIDI.sendNoteOn(60, 90, 10);
    }

    }
    #define THRESHOLD 14
    int pot = 1;
    int potbefore = 0;

    void setup()
    {

    }


    int update_current( int raw )
    {

    static int current = THRESHOLD;
    int delta = raw - current;

    if ( delta <= -THRESHOLD )
    current = raw + THRESHOLD;

    if ( delta >= THRESHOLD )
    current = raw - THRESHOLD;

    return current;


    }


    void loop() {
    int raw = analogRead(pot);
    int current = update_current( raw );
    int scaled = map( current, 45, 990, 0, 127);




    /* Serial.print ("current outside of if:"); Serial.println (current, DEC); Serial.print ("potbef outside of if:"); Serial.println (potbef, DEC); */

    if (scaled != potbefore) {
    usbMIDI.sendControlChange(1, scaled, 1);

    potbefore = scaled;


    }

  2. #2
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    329
    So what you need is to have one "current" value for each analog input, current is the value on the right that is changed when the difference reaches the threshhold.

    On the left you have iAlag[i], that is the current value of channel i, the inputAnalog[i] is the 'raw' value for channel i.

    You also need

    On the left you have
    if (abs(inputAnalog[i] - iAlag[i]) > 7){
    This corresponds to testing with THRESHOLD equals to 7-1 = 6 on the right.

    So the only missing part is the map that should be applied to the raw value if the threshold is reached,
    on the left that would be applying map to inputAnalog[i] after updating iAlag[i], and then sending the scaled value as
    MIDI cc value.

  3. #3
    Junior Member
    Join Date
    Oct 2016
    Posts
    3
    Thank you very much for clearing everything with this perfect lesson!

    I think I understand all you say but I have a lack of comprehension of the coding order.
    I inserted the map in the "ccValue(i) =" and deleted the /8 as it is now mapped, still under the name of ccValue(i).

    #include <Bounce.h>

    // define how many pots are active up to number of available analog inputs
    #define analogInputs 8
    // make arrays for input values and lagged input values
    int inputAnalog[analogInputs];
    int iAlag[analogInputs];
    // make array of cc values
    int ccValue[analogInputs];
    // index variable for loop
    int i;



    Bounce button0 = Bounce(0, 3);
    Bounce button1 = Bounce(1, 3);
    Bounce button2 = Bounce(2, 3);
    Bounce button3 = Bounce(3, 3);
    Bounce button4 = Bounce(4, 3);
    Bounce button5 = Bounce(5, 3);
    Bounce button6 = Bounce(6, 3);
    Bounce button7 = Bounce(7, 3);
    Bounce button8 = Bounce(8, 3);
    Bounce button9 = Bounce(9, 3);
    Bounce button10 = Bounce(10, 3);







    void setup() {
    // MIDI rate
    Serial.begin(31250);
    pinMode(0, INPUT_PULLUP);
    pinMode(1, INPUT_PULLUP);
    pinMode(2, INPUT_PULLUP);
    pinMode(3, INPUT_PULLUP);
    pinMode(4, INPUT_PULLUP);
    pinMode(5, INPUT_PULLUP);
    pinMode(6, INPUT_PULLUP);
    pinMode(7, INPUT_PULLUP);
    pinMode(8, INPUT_PULLUP);
    pinMode(9, INPUT_PULLUP);
    pinMode(10, INPUT_PULLUP);

    }

    void loop() {

    // loop trough active inputs for knobs
    for (i=0;i<analogInputs;i++){
    // read current value at i-th input
    inputAnalog[i] = analogRead(i);
    // if magnitude of difference is 8 or more...
    if (abs(inputAnalog[i] - iAlag[i]) > 7){
    // calc the CC value based on the raw value
    ccValue[i] = map inputAnalog[i] (iAlag[i], 45, 990, 0, 127);
    // send the MIDI
    usbMIDI.sendControlChange(i, ccValue[i], 3);
    // set raw reading to lagged array for next comparison
    iAlag[i] = inputAnalog[i];

    }
    delay(5); // limits MIDI messages to reasonable number
    }

    // Push Button code
    button0.update();
    button1.update();
    button2.update();
    button3.update();
    button4.update();
    button5.update();
    button6.update();
    button7.update();
    button8.update();
    button9.update();
    button10.update();


    if (button0.fallingEdge())
    {
    usbMIDI.sendNoteOn(50, 90, 10);
    }
    if (button1.fallingEdge())
    {
    usbMIDI.sendNoteOn(51, 90, 10);
    }
    if (button2.fallingEdge())
    {
    usbMIDI.sendNoteOn(52, 90, 10);
    }
    if (button3.fallingEdge())
    {
    usbMIDI.sendNoteOn(53, 90, 10);
    }
    if (button4.fallingEdge())
    {
    usbMIDI.sendNoteOn(54, 90, 10);
    }
    if (button5.fallingEdge())
    {
    usbMIDI.sendNoteOn(55, 90, 10);
    }
    if (button6.fallingEdge())
    {
    usbMIDI.sendNoteOn(56, 90, 10);
    }
    if (button7.fallingEdge())
    {
    usbMIDI.sendNoteOn(57, 90, 10);
    }
    if (button8.fallingEdge())
    {
    usbMIDI.sendNoteOn(58, 90, 10);
    }
    if (button9.fallingEdge())
    {
    usbMIDI.sendNoteOn(59, 90, 10);
    }
    if (button10.fallingEdge())
    {
    usbMIDI.sendNoteOn(60, 90, 10);
    }

    }


    This looks good for me, but I got an error of "invalid consersion"
    I know this must be basic organisation, but regarding from the tutorials, I think I went a bit high for my first try on the Teensy...
    TEENSY_PERSO: In function 'void loop()':
    TEENSY_PERSO:60: error: invalid conversion from 'long int (*)(long int, long int, long int, long int, long int)' to 'int' [-fpermissive]
    ccValue[i] = map inputAnalog[i] (iAlag[i], 45, 990, 0, 127);
    ^



    Thanks again for helping, I see the end of the tunnel!

  4. #4
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    329
    Try
    Code:
    ccValue[i] = map(inputAnalog[i], 45, 990, 0, 127);

  5. #5
    Junior Member
    Join Date
    Oct 2016
    Posts
    3
    Yes! Thank you very much mlu, that works!

    While searching the right values for the map, one question came to me :
    is that possible to have multiple mapping for those inputAnalog(i)? Let's say having a certain mapping from 45 to 300 for 0 to 30 then another one from 301 to 990 for 31 to 127, for exemple.

    I took a look at the multimap function and all the forum solutions with floating values,... but was totally lossed in the sea of codes.
    I tried to use a suite of map function but this did not worked, as they already say everywhere...

    If this solution exist, I am planning to use 3 or 4 mappings between 0 to 127.


    Don't bother if it looks to hard for this time.
    mlu, thank you very very much for your explanations!
    Last edited by Patalaii; 10-07-2016 at 08:47 PM.

  6. #6
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    329
    Good to hear it works

    You can surely make a more complicated map function if that is needed. Probably the best way is to define your function , lets say its called myMap.
    (( This is NOT tested final code, but simply an idea of what you should work with, to learn how to handle things like this. And the details can be done more efficiently, using the inbuilt map function or doing your own math ))

    Code:
    int myMap(int input) {
    /* in private mappings the input and output intervals are defined in the actual code */
    /* Split the input interval into smaller pieces, and specify the ouput for each input interval */
        if (input <31 ) return map(input, ... ... ... ) ;
        else if (input < 90) return (input-90)*3+42;
        else if (input < 172) return (input*input)/84;
        else return 127;
    }
    This example gives four sub-mappings that needs to be carefully adjusted to create a continous map of your prefered shape. I havnt studied multimap, but its probably doing things along these lines.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •