Programming Problem

Discuss tips, tricks, and the creative process of music creation. Post HELP threads here

Programming Problem

Postby Pimps_McGee » 28 Feb 2013 23:21

I've been creating a mock MIDIfighter controller and I've seemed to have gotten stuck on programming.

Background:
The controller uses the Arduino microcontroller with the HIDUINO firmware flash so it can communicate directly over the USB rather than through MIDI ports. I'm using the MIDILibrary set which allows actual MIDI I/O communications.

The Problem:
What's happening is that when I push a button the program should send one simple MIDI NoteOn out to the computer and when I release it should send one MIDI NoteOff message to the computer. What is actually happening is that for the duration I have the button pressed it loops and sends the NoteOn message repeatedly and when I release it, it sends the NoteOff message repeatedly. I can't seem to think of the logic statement I need in order to send only one message at a time.
Here is code I've been working with:
#include <MIDI.h> //include the MidiLibrary

//Set constant pin number to note correlations
const int C3 = 2;

//Set button state to note correlations
int C3s = 0;

void setup(){
MIDI.begin(MIDI_CHANNEL_OMNI);
//To begin debug uncomment next line
//Serial.begin(9600);

//Set all pin modes to input
pinMode(C3, INPUT);
}


void loop(){
C3s = digitalRead(C3);

if(C3s == HIGH){
for(int x=0; x<1; x++){
MIDI.sendNoteOn(48, 100, 1);
//Serial.write("hueheuhue");
}
}
else{
for(int x=0; x<1; x++){
//Serial.write("nonono");
MIDI.sendNoteOff(48, 100, 1);
}
}
}

I have a feeling the answer is extremely simple but for some reason I can't think of it.
User avatar
Pimps_McGee
 
Posts: 47
Joined: 11 Mar 2012 14:20

Re: Programming Problem

Postby vaceslav » 01 Mar 2013 06:41

#include <MIDI.h> //include the MidiLibrary

//Set constant pin number to note correlations
const int C3 = 2;

//Set button state to note correlations
int C3s = 0;
//Set state checker to off
int check = 0;

void setup(){
MIDI.begin(MIDI_CHANNEL_OMNI);
//To begin debug uncomment next line
//Serial.begin(9600);

//Set all pin modes to input
pinMode(C3, INPUT);
}


void loop(){
C3s = digitalRead(C3);

if(C3s == HIGH){
//check in note was off in previous loop
if(check == 0){
//set state checker to on
check = 1;
for(int x=0; x<1; x++){
MIDI.sendNoteOn(48, 100, 1);
//Serial.write("hueheuhue");
}
}
}
else{
//check in note was on in previous loop
if(check == 1){
//set state checker to off
check = 0;
for(int x=0; x<1; x++){
//Serial.write("nonono");
MIDI.sendNoteOff(48, 100, 1);
}
}
}



It's been a long time since I've written any code so my syntax may be a little off, but something like this might work.

Or maybe not.

Edit: Just added some extra comments where I added code.
Youtube
Soundcloud
Skype: fvaceslav
User avatar
vaceslav
 
Posts: 78
Joined: 28 Aug 2012 20:56
Location: Brisbane
OS: Windows
Primary: Ableton
Cutie Mark: Safety Pin

Re: Programming Problem

Postby Pimps_McGee » 02 Mar 2013 14:27

Thanks Man! It's working perfectly... Well at least with one button. I think the problem is that the check variable is global and needs to localized into each if-else statement. I'll post back after I try this.
User avatar
Pimps_McGee
 
Posts: 47
Joined: 11 Mar 2012 14:20


Return to Technique



Who is online

Users browsing this forum: No registered users and 0 guests