Pages: 1 [2] 3   Go Down
Author Topic: Please assist me in optimizing my (beginner) code  (Read 2391 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PaulS: i should change it to this?

Code:

int pushbutton_state[] = {};



Imahilus: thanks, i will get back to tomorrow smiley

Another question about this part:

Code:
}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?
Logged

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

Quote
i should change it to this?

Code:

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

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

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, now i'm confused though.
Can you tell in english what this means:

Code:
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...  :-?
« Last Edit: December 11, 2010, 01:03:21 pm by stereomuffin » Logged

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

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see! Got it smiley
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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};
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
« Last Edit: December 12, 2010, 06:07:59 am by stereomuffin » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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? :-[
« Last Edit: December 12, 2010, 06:51:31 am by stereomuffin » Logged

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

There are three parts to using a flag to make sure to send only one off note
Code:
boolean midiOffSent = false;

Code:
// Send a midi note
midiOffSent = false;

Code:
// Need to send midi off note?
if(!midiOffSent)
{
   // Send midi off note
   midiOffSent = true;
}
« Last Edit: December 12, 2010, 07:00:06 am by PaulS » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for your patience PaulS  smiley
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:
#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;
            
          }
    }
}
« Last Edit: December 12, 2010, 08:19:17 am by stereomuffin » Logged

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

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed it but i have the same problem..  :-/

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

          }
    }
}
« Last Edit: December 12, 2010, 09:04:49 am by stereomuffin » Logged

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

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 15
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: 1 [2] 3   Go Up
Jump to: