Go Down

Topic: MIDI Footswitch (Read 2020 times) previous topic - next topic

cesarferrolho

Hi! I'm new to this forum.
I started trying to build a midi footswitch to use with guitar software on the PC.
A Double Throw foostswitch connects to the Arduino, using 2 digital inputs. one switch position goes on input 2 and the other in input 3, the middle connection goes to the 5v of the Arduino. It's all connected with the pushbutton method on the examples of the arduino. i'm using a 1k resistor on each input (to GND).
Then i use Digital port 1 for sending serial data to a 5 pin MIDI connector which connects to a MIDI->USB interface.
The software detects the midi device and uses it to toggle an effect on/off.

It's all working, BUT.. sometimes, the arduino doesn´t detect/send the midi command when i use the footswitch. It's a random thing, every X clicks, it fails.
What could be wrong? (the footswitch is in order, i tested it)

Using Arduino ver 0022

johnwasser

Could be an error in your sketch.  Perhaps if you showed your code someone might notice a programming error that you missed.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

cesarferrolho

#2
Aug 14, 2012, 09:15 am Last Edit: Aug 14, 2012, 09:20 am by cesarferrolho Reason: 1
Right, here it is, it's an adaptation of one i saw on the web:

Code: [Select]
#define SWITCH1 2
#define SWITCH2 3

#define BOUNCEDELAY 1

int switches[2] = { SWITCH1, SWITCH2 };
int switchState[2] = { HIGH, HIGH };

int currentSwitch = 0;
int currentProgram = 22;   // current program - sent to the output
int bypassState = LOW;     // state of bypass pedal


void setup() {
 //  Set MIDI baud rate:
 Serial.begin(31250);

 // Setup Switches and activation LEDs
 for( currentSwitch = 0; currentSwitch < 2 ; currentSwitch++ ) {
   pinMode( switches[currentSwitch], INPUT );          // Set pin for switch
   digitalWrite( switches[currentSwitch], HIGH );      // Turn on internal pullup
 }
}
void loop() {
 for( currentSwitch = 0; currentSwitch < 2; currentSwitch++ ) {
   if((digitalRead(switches[currentSwitch]) != switchState[currentSwitch] )&&(switchState[currentSwitch] == HIGH)){
     switch( currentSwitch ) {
       case 0:
         //Bypass
         if( bypassState == LOW ) {
           bypassState = HIGH;
           midiSend( 0xB0, 0x5B, 0x00 ); // bypass off
           
         }
         else {
           bypassState = LOW;
           midiSend( 0xB0, 0x5B, 0x7F ); // bypass on
           
         }
         break;
}
     delay( BOUNCEDELAY );
   }
   switchState[currentSwitch] = digitalRead( switches[currentSwitch] );
 }  
}

//  Send a three byte midi message  
void midiSend(char status, char data1, char data2) {
 Serial.print(status, BYTE);
 Serial.print(data1, BYTE);
 Serial.print(data2, BYTE);
}


It might look rather strange using case, and there is an unused variable there. But as i said it's an adaptation.
The problem i'm talking about also happened in various other sketches with different programing. I'm going to try different value resistors anyway...

cesarferrolho

Update:
I replaced the 1K resistors with 10K resistors.. same thing..
On the Arduino, i used a voltmeter in AC mode to read the voltage between the 5V pin and GND and it gave 10,2 V AC !!! Is this normal? (in DC mode it reads 5 V)

I'm powering the Arduino with a 9V battery (between Vin and GND)

johnwasser

> A Double Throw foostswitch connects to the Arduino, using 2 digital inputs.
> one switch position goes on input 2 and the other in input 3, the middle connection goes to the 5v of the Arduino.
Code: [Select]

  for( currentSwitch = 0; currentSwitch < 2 ; currentSwitch++ ) {
    pinMode( switches[currentSwitch], INPUT );          // Set pin for switch
    digitalWrite( switches[currentSwitch], HIGH );      // Turn on internal pullup
  }


If you are switching the inputs to +5V you need a pull-DOWN resistor on each.  Enabling the internal pull-UP resistors will be counter-productive and if you didn't implement the external pull-DOWN resistors would cause both pins to read HIGH all the time: Switch open = pull-up reads HIGH; Switch closed = +5V reads HIGH.

If you don't have pull-down resistors, leave the internal pull-up resistors enabled and connect the center terminal of the switch to GROUND (instead of +5).  The switches will then read as HIGH when open and LOW when closed.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

cesarferrolho

Hi! Thanks for answering  :)
Hmm, well, i will show you my circuit:



Given this circuit, how would you advise me to change the code?

Regards.

cesarferrolho

Update:
I tried turning that internal pullup in the program to LOW, and seems the same. It seems no matter what i do, it always ends up the same.
It's really frustrating  =(
It keeps randomly failing to read the input i give through the footswitch. The footswitch works fine, i tested with a multimeter.
The serial comunication seems in order too, that's not where it's failing either.

Is there a simple program/circuit i can use to check if the inputs of the arduino are 100% ok?

cesarferrolho

PROBLEM SOLVED !  :smiley-sweat:

Someone sent me a very good program and very simple also, that not only made everything work, it also discards the need for 2 inputs, one is enough!!
I also used a 10K resistor instead of 1K and a 100 ohm resistor before the 5V that go into the switch middle pin.

The code used:
Code: [Select]
#define pin2 2
byte vsw1;

void setup()
{
}
void loop()
{ if (vsw1 != digitalRead(pin2))
  { if (vsw1 == 0) {midiSend( 0xB0, 0x5B, 0x00 );
                    vsw1 = 1;
                  delay(1);}
              else {midiSend( 0xB0, 0x5B, 0x7F );
                    vsw1 = 0;
                  delay(1);}
  }
}
void midiSend(char cc, char data1, char data2) {
  Serial.begin(31250);
  Serial.print(cc, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
  Serial.end();
}


Thank you very much to all who helped :)

Go Up