Go Down

Topic: Please assist me in optimizing my (beginner) code (Read 3581 times) previous topic - next topic

stereomuffin

PaulS: i should change it to this?

Code: [Select]


int pushbutton_state[] = {};



Imahilus: thanks, i will get back to tomorrow :)

Another question about this part:

Code: [Select]

}else{
         pushbutton_pressed = false;
         BlinkM_setRGB(leds[i],0x00,0x00,0x00);
      }



How would i go about making sure that this else part in the loop only runs once? Do i need to do the same as above and use some kind of counter? I only want it run once because i want to implement midi note off there and its enough if it sends a noteoff once.

Same goes with the BlinkM_setRGB(leds,0x00,0x00,0x00);
statement, it turns my leds off (black) - but really i don't need it to keep on running forever do i?

PaulS

Quote
i should change it to this?

Code:

int pushbutton_state[] = {};

No.
Code: [Select]
int pushbutton_state[number_of_pushpins];
Also, this:
Code: [Select]
int number_of_pushpins = 16;
should be this:
Code: [Select]
[glow]const[/glow] int number_of_pushpins = 16;
This prevents the sketch from altering the value in number_of_pushpins.

PaulS

Quote
Do i need to do the same as above and use some kind of counter? I only want it run once because i want to implement midi note off there and its enough if it sends a noteoff once.

Either a counter or a simple flag, midiOffSent, initialized to false, and set false again whenever a MIDI note is sent. Then, test midiOffSent before sending a MIDI off note. Only send the off note if one has not been sent.

stereomuffin

#18
Dec 11, 2010, 07:01 pm Last Edit: Dec 11, 2010, 07:03 pm by stereomuffin Reason: 1
Thanks, now i'm confused though.
Can you tell in english what this means:

Code: [Select]

int pushbutton_state[number_of_pushpins];


something like: define an array of type integer that holds the value of the number_of_pushpins (=16) ? i dont get the logic behind it...  :-?

PaulS

The value in the [] is the number of elements in the array. When no value is present, the initializer list is used to determine the number of elements.

When there is no initializer list, the number of elements in the array must be explicitly defined.


lloyddean

Unless I missed something pins can't be used simultaneously for both input and output as indicated with the values in these arrays:

Quote

byte leds[] = {9, 10, 11, 12, 13, 14, 15, 16 ,17, 18, 19, 20, 21, 22, 23, 24};
int pushbutton_pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 22};


stereomuffin

#22
Dec 12, 2010, 10:51 am Last Edit: Dec 12, 2010, 12:07 pm by stereomuffin Reason: 1
Hi lloyddean, sorry i will add comments to the code soon - then you would have read that the byte leds array is an array of I2C network addresses and not pin numbers :)

stereomuffin

#23
Dec 12, 2010, 12:50 pm Last Edit: Dec 12, 2010, 12:51 pm by stereomuffin Reason: 1
Quote

Either a counter or a simple flag, midiOffSent, initialized to false, and set false again whenever a MIDI note is sent. Then, test midiOffSent before sending a MIDI off note. Only send the off note if one has not been sent.


I am lost with this, really no idea how its done..any example somewhere which i can study? :-[

PaulS

#24
Dec 12, 2010, 12:59 pm Last Edit: Dec 12, 2010, 01:00 pm by PaulS Reason: 1
There are three parts to using a flag to make sure to send only one off note
Code: [Select]
boolean midiOffSent = false;

Code: [Select]
// Send a midi note
midiOffSent = false;


Code: [Select]
// Need to send midi off note?
if(!midiOffSent)
{
  // Send midi off note
  midiOffSent = true;
}

stereomuffin

#25
Dec 12, 2010, 02:13 pm Last Edit: Dec 12, 2010, 02:19 pm by stereomuffin Reason: 1
Thank you for your patience PaulS  :)
I have problem with the syntax or i just implement it the wrong way (or probably both).

This is what i got now (but the problem is that it sends a note and immediately after a note off)

Code: [Select]

#include "Wire.h"
#include "BlinkM_funcs.h"
#include <MIDI.h>

byte blinkm_all = 0x00;
byte incomingByte;

boolean pushbutton_pressed = false;
boolean midiOffSent = false;

const int number_of_pushpins = 16;
int pushbutton_state[number_of_pushpins];

byte leds[] = {9, 10, 11, 12, 13, 14, 15, 16 ,17, 18, 19, 20, 21, 22, 23, 24};  // array for the i2c addresses of my blinkm's
int pushbutton_pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 22};


void setup() {
 
  MIDI.begin(1);
  Serial.begin(57600); // temporary so i can use spikenzielabs.com serial to midi app
 
   BlinkM_begin();
   
   for(int i=0; i< number_of_pushpins; i++){
      pinMode(pushbutton_pins[i], INPUT);
   }

}

void loop() {
 
 for(int i=0; i< number_of_pushpins; i++){
      pushbutton_state[i] = digitalRead(pushbutton_pins[i]);
   }


 for(int i=0; i< number_of_pushpins; i++){
      if( pushbutton_state[i] == HIGH) {
        if(pushbutton_pressed == false){
          BlinkM_setRGB(leds[i],0x00,0x00,0xff);
            MIDI.sendNoteOn(70,127,1);
            midiOffSent = false;
          pushbutton_pressed = true;        

         
        }
      }else if (!midiOffSent) {
         
         
           MIDI.sendNoteOff(70,127,1);
           midiOffSent = true;

      }
         else{
           BlinkM_setRGB(leds[i],0x00,0x00,0x00);
           pushbutton_pressed = false;
           
         }
   }
}

PaulS

Quote
This is what i got now (but the problem is that it sends a note and immediately after a note off)

Well, of course it does. This code:
Code: [Select]
      }else if (!midiOffSent) {
         
         
           MIDI.sendNoteOff(70,127,1);
           midiOffSent = true;

      }

(minus the else) needs to go in the block that turns the BlinkM thing off.

stereomuffin

#27
Dec 12, 2010, 03:03 pm Last Edit: Dec 12, 2010, 03:04 pm by stereomuffin Reason: 1
I changed it but i have the same problem..  :-/

Code: [Select]

#include "Wire.h"
#include "BlinkM_funcs.h"
#include <MIDI.h>

byte blinkm_all = 0x00;
byte incomingByte;

boolean pushbutton_pressed = false;
boolean midiOffSent = false;

const int number_of_pushpins = 16;
int pushbutton_state[number_of_pushpins];

byte leds[] = {9, 10, 11, 12, 13, 14, 15, 16 ,17, 18, 19, 20, 21, 22, 23, 24};  // array for the i2c addresses of my blinkm's
int pushbutton_pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 17, 18, 19, 22};


void setup() {
 
  MIDI.begin(1);
  Serial.begin(57600); // temporary so i can use spikenzielabs.com serial to midi app
 
   BlinkM_begin();
   
   for(int i=0; i< number_of_pushpins; i++){
      pinMode(pushbutton_pins[i], INPUT);
   }

}

void loop() {
 
 for(int i=0; i< number_of_pushpins; i++){
      pushbutton_state[i] = digitalRead(pushbutton_pins[i]);
   }


 for(int i=0; i< number_of_pushpins; i++){
      if( pushbutton_state[i] == HIGH) {
        if(pushbutton_pressed == false){
          BlinkM_setRGB(leds[i],0x00,0x00,0xff);
            MIDI.sendNoteOn(70,127,1);
            midiOffSent = false;
          pushbutton_pressed = true;        

         
        }
      }
         else{
                if (!midiOffSent) {
         
         
           MIDI.sendNoteOff(70,0,1);
           midiOffSent = true;

      }
           BlinkM_setRGB(leds[i],0x00,0x00,0x00);
           pushbutton_pressed = false;

         }
   }
}

PaulS

It appears that you want to send the midi off note only when pushbutton_pressed is false, not every time button is not pressed.

Spending a little time putting the { on a new line, properly lined up with the }, and with everything in between properly indented would make your code a lot easier to read.

stereomuffin

Ofcourse.. thanks for the tip!
I will clean up the code as well before reposting..

Go Up