I need to use a momentary pushbutton as a toggleswitch. This code works for me but I do not know how to add midi features like chanel, program control etc.
int Button = 0; //This is the default "Button Off" and 1 is "Button On"
int OldButton = 0; //I'll explain later
void setup() {
pinMode(2, INPUT); //Pin 2 is an Input
}
void loop() {
if(digitalRead(2) == HIGH) { //If Pin 2 is high (At 5 Volts)
Button = 1 - Button; //If Button is set as 0 then Button = 1 - 0 (=1) and if Button is 1 then Button=1 - 1 (=0)
}
if(Button == 1 && OldButton == 0) { //If Button is 1 and OldButton is 0 (This means basically means "If the button was just pressed"
//Put what you want to happen when the "Switch" is on here
delay(500); //Delay for half a second
}
if(Button == 0 && OldButton == 1) {
//Put what you want to happen when the "Switch" is off here
delay(500); //Delay for half a second
}
OldButton = Button; //The data is now old
}
Ok, this is my first code and I get stuck on how to do a troggle switch in digital, I can not find the way and I can not find a concrete midi example. In analogue I have three pots and a troggle swhitch working correctly.
Nicolp1976:
I need to use a momentary pushbutton as a toggleswitch. This code works for me but I do not know how to add midi features like chanel, program control etc.
My guess would be that the OP is requesting more than a simple 'ON' or 'OFF' button; the OP wants to increase the channel (looping) and/or increase the program control number (looping).
Delta_G - very intelligent; he'll be back to correct my mistakes.
As for what I suppose you want to do:
int channel =1; //max 4 channels (1 thru 4)
int program =1; //max 8 programs (1 thru 8)
int btn_ch=7; //channel btn = pin7
int btn_pr=8; //program btn = pin8
void setup() {
// put your setup code here, to run once:
}
void loop() {
if(digitalRead(btn_ch) == HIGH) { //If Pin 7 is high (At 5 Volts)
channel++; //increase channel by 1
if(channel == 5){channel = 1;} //loop back to channel 1
Change_Channel(channel);
delay(1000);
}
if(digitalRead(btn_pr) == HIGH) { //If Pin 8 is high (At 5 Volts)
program++; //increase program by 1
if(program == 9){program = 1;} //loop back to program 1
Change_Program(program);
delay(1000);
}
}
void Change_Channel(int int_ch){
// do stuff here referencing int_ch
}
void Change_Program(int int_pr){
// do stuff here referencing int_pr
}
Interesting for this project but what I really want is, and sorry for my ignorance, translate the following code from analog to digital.
int val = 0; //Our initial pot values. We need one for the first value and a second to test if there has been a change made. This needs to be done for all 3 pots.
int lastVal = 0;
void setup()
{
Serial.begin(31250); // Set the speed of the midi port to the same as we will be using in the Hairless Midi software
}
void loop()
{
val = analogRead(0)/8; // Divide by 8 to get range of 0-127 for midi
if (val != lastVal) // If the value does not = the last value the following command is made. This is because the pot has been turned. Otherwise the pot remains the same and no midi message is output.
{
MIDImessage(176,5,val);} // 176 = CC command (channel 1 control change), 1 = Which Control, val = value read from Potentionmeter 1 NOTE THIS SAYS VAL not VA1 (lowercase of course)
lastVal = val;
delay(10); //here we add a short delay to help prevent slight fluctuations, knocks on the pots etc. Adding this helped to prevent my pots from jumpin up or down a value when slightly touched or knocked.
}
void MIDImessage(byte command, byte data1, byte data2) //pass values out through standard Midi Command
{
Serial.write(command);
Serial.write(data1);
Serial.write(data2);
}
Sorry, I cannot help; I'm not into MIDI. Perhaps others can help you. Delta_G was correct - I over-stepped my bounds.
Nicolp1976:
Interesting for this project but what I really want is, and sorry for my ignorance, translate the following code from analog to digital.
I think what he's trying to say is that he wants to use sequences of button-pushes (digitalRead) instead of a potentiometer (analogRead) as the UI for his sketch.
OP: to accomplish this, you need to specify what button-pushes you want to do what. You have to design the user interface.