Arduinos and Photo-interrupters -help!
Arduinos and Photo-interrupters -help!
In the past I’ve used photo-interrupters as key contacts on both manual keyboards and a pedal board, but on those occasions my go-to MIDI encoder was a Midi Boutique hwce-2x and I didn’t encounter any problems.
Currently I’m building a three-manual “budget” console using photo-interrupters as the key contacts on the manual keyboards and Arduinos as the Midi encoders – and I’ve hit a problem that has me totally baffled.
My original intention was to use an encoder based on the Arduino Due for the keyboards and expression pedals, and a smaller encoder based on the Arduino Pro-Micro for the “pistons”. I chose these particular Arduino boards because they do MIDI over USB, thus obviating the need for (and cost of) a MIDI-to-USB interface.
As I completed the photo-interrupter installation on each keyboard I bench-tested it using the Pro-Micro and everything seemed OK.
It was when I mounted the keyboards into the console and connected them to the Arduino Due that I hit problems :
The Choir keyboard performed flawlessly, delivering clean Note-on/Note-off MIDI messages to the Midi-Ox monitor display. But when the Great and Swell keyboards were connected to the encoder they promptly began delivering a continuous stream of random Note-on/Note-off messages.
This had me baffled because the geometry, the circuits and the components were identical for the three keyboards.
Eventually, after consulting Google, I decided to replace the 4k7 pull-up resistors on the encoder with 2k2 resistors and this made an immediate improvement – so much so that at first I thought I’d solved the problem.
However, as I carried out more thorough testing I found that on both the Great and the Swell manuals a few individual keys were not behaving correctly, either not delivering a message at all, or delivering garbage.
At this point I began to suspect that the problems might have been due to the fact that the Arduino Due operates at 3.3 volts rather than 5 volts like most other boards, so I decided to substitute an Arduino Mega that I’d also programmed as a MIDI encoder and that I knew worked satisfactorily with reed-switch key contacts.
Using the Mega’s built-in pull-up resistors I found that the Choir keyboard worked correctly (as it had with the Due), but the Great and Swell keyboards were totally dead, so I promptly set about installing 2k2 pull-up resistors into the encoder circuitry. This brought the Great and Swell keyboards to life – but all was still not well.
Before I summarise the current state of affairs I should explain that the keyboard wiring is an 8x8 matrix arrangement; the keyboards are connected to the encoder by means of 16-way flat cables with IDC connectors that plug into four 16-way IDC headers that in turn are connected to the Arduino pins; and the Arduino is programmed so that the headers are on MIDI channels 1 through 4 respectively.
So, the current situation:
1. The CHOIR keyboard works perfectly on Channels 1, 2, 3, and 4.
(This leads me to believe there is nothing intrinsically wrong with the Arduino programming or the circuitry.)
2. The GREAT keyboard works perfectly on Channel 1, but spontaneously delivers varying degrees of MIDI garbage (continuous stream of multiple Note-on/Note-off messages) as soon as it is connected to Channels 2, 3, 4, and before pressing any key.
3. The SWELL keyboard works perfectly on Channel 1, but on Channel 2 it delivers a continuous stream of Note-on/Note-off messages (mainly 036-C, 052-E, 092-G i.e. first note in each group of eight) when a key is pressed. On Channel 3 Several Note-on/Note-off messages appear simultaneously when a single key is pressed. On Channel 4, some notes are OK; others generate spurious messages when pressed; some notes are dead
Normally I’m quite good at trouble-shooting, but this one has me beat – I can’t find a common thread! I guess I’m out of my depth and any help would be welcome.
If you have read this far – thank you for your patience!
Graham
Currently I’m building a three-manual “budget” console using photo-interrupters as the key contacts on the manual keyboards and Arduinos as the Midi encoders – and I’ve hit a problem that has me totally baffled.
My original intention was to use an encoder based on the Arduino Due for the keyboards and expression pedals, and a smaller encoder based on the Arduino Pro-Micro for the “pistons”. I chose these particular Arduino boards because they do MIDI over USB, thus obviating the need for (and cost of) a MIDI-to-USB interface.
As I completed the photo-interrupter installation on each keyboard I bench-tested it using the Pro-Micro and everything seemed OK.
It was when I mounted the keyboards into the console and connected them to the Arduino Due that I hit problems :
The Choir keyboard performed flawlessly, delivering clean Note-on/Note-off MIDI messages to the Midi-Ox monitor display. But when the Great and Swell keyboards were connected to the encoder they promptly began delivering a continuous stream of random Note-on/Note-off messages.
This had me baffled because the geometry, the circuits and the components were identical for the three keyboards.
Eventually, after consulting Google, I decided to replace the 4k7 pull-up resistors on the encoder with 2k2 resistors and this made an immediate improvement – so much so that at first I thought I’d solved the problem.
However, as I carried out more thorough testing I found that on both the Great and the Swell manuals a few individual keys were not behaving correctly, either not delivering a message at all, or delivering garbage.
At this point I began to suspect that the problems might have been due to the fact that the Arduino Due operates at 3.3 volts rather than 5 volts like most other boards, so I decided to substitute an Arduino Mega that I’d also programmed as a MIDI encoder and that I knew worked satisfactorily with reed-switch key contacts.
Using the Mega’s built-in pull-up resistors I found that the Choir keyboard worked correctly (as it had with the Due), but the Great and Swell keyboards were totally dead, so I promptly set about installing 2k2 pull-up resistors into the encoder circuitry. This brought the Great and Swell keyboards to life – but all was still not well.
Before I summarise the current state of affairs I should explain that the keyboard wiring is an 8x8 matrix arrangement; the keyboards are connected to the encoder by means of 16-way flat cables with IDC connectors that plug into four 16-way IDC headers that in turn are connected to the Arduino pins; and the Arduino is programmed so that the headers are on MIDI channels 1 through 4 respectively.
So, the current situation:
1. The CHOIR keyboard works perfectly on Channels 1, 2, 3, and 4.
(This leads me to believe there is nothing intrinsically wrong with the Arduino programming or the circuitry.)
2. The GREAT keyboard works perfectly on Channel 1, but spontaneously delivers varying degrees of MIDI garbage (continuous stream of multiple Note-on/Note-off messages) as soon as it is connected to Channels 2, 3, 4, and before pressing any key.
3. The SWELL keyboard works perfectly on Channel 1, but on Channel 2 it delivers a continuous stream of Note-on/Note-off messages (mainly 036-C, 052-E, 092-G i.e. first note in each group of eight) when a key is pressed. On Channel 3 Several Note-on/Note-off messages appear simultaneously when a single key is pressed. On Channel 4, some notes are OK; others generate spurious messages when pressed; some notes are dead
Normally I’m quite good at trouble-shooting, but this one has me beat – I can’t find a common thread! I guess I’m out of my depth and any help would be welcome.
If you have read this far – thank you for your patience!
Graham
Re: Arduinos and Photo-interrupters -help!
Graham, I read your lengthy and interesting post from beginning to end. Unfortunately, I have no helpful comments for the problem you've described.
My reason for reading your post carefully was perhaps to find clues about how to change the default channel on a pedalboad which should arrive from Italy in a few days. I have read elsewhere that it will probably have an Arduino board. My three manual keyboards we're from Classic Midiworks; they came pre-programmed so I did not have to do anything except plug cables in.
My reason for reading your post carefully was perhaps to find clues about how to change the default channel on a pedalboad which should arrive from Italy in a few days. I have read elsewhere that it will probably have an Arduino board. My three manual keyboards we're from Classic Midiworks; they came pre-programmed so I did not have to do anything except plug cables in.
Bruce
Re: Arduinos and Photo-interrupters -help!
We'll need to look at the sketch you uploaded to the arduino. I think at least one of the problems is probably there. Are all the keyboards wired to a single mega? The mega does not have native USB Midi, so you'll need either something like hairless midi or a midi out port and an adapter. It would be helpful to know what photo interrupters you are using, as that will help to clarify the operating parameters. Also, what kind of isolation are you using for the photo interrupters? Extraneous light from the console or other sources can play havoc with them, and they generally need to be well optically isolated, say with black felt or something similar.
Re: Arduinos and Photo-interrupters -help!
Thank you for responding so quickly.
I guess the bottom line of my lengthy post is:
If one keyboard performs satisfactorily on all four channels, and all three keyboards perform satisfactorily on one channel, why don’t all keyboards perform satisfactorily on all channels?
Hopefully if I tweak the right parameter(s) by an appropriate amount, I should get a one-size-fits-all solution?
To answer your questions:
The photo-interrupters are EVERLIGHT ITR9608.
As regards ambient light, I am confident that is not an issue.
You should find the answers to your other questions in this link
https://1drv.ms/f/s!AkOZ2I3AWrKRigPSFr9 ... X?e=a3pDg8
The only differences between my current photo-interrupter and IR diode circuits compared with those in the link are in the value of the IR diode ballast resistors.
For the Choir and Swell keyboards they are 820 ohm, giving a forward voltage of 1.13 V.
For the Great I tried decreasing them to 680 ohm to give VF of 1.2V but it made no difference.
Please don’t deride my Arduino sketches – I’m great at plagiarizing/adapting but a complete non-starter at programming!
Graham
I guess the bottom line of my lengthy post is:
If one keyboard performs satisfactorily on all four channels, and all three keyboards perform satisfactorily on one channel, why don’t all keyboards perform satisfactorily on all channels?
Hopefully if I tweak the right parameter(s) by an appropriate amount, I should get a one-size-fits-all solution?
To answer your questions:
The photo-interrupters are EVERLIGHT ITR9608.
As regards ambient light, I am confident that is not an issue.
You should find the answers to your other questions in this link
https://1drv.ms/f/s!AkOZ2I3AWrKRigPSFr9 ... X?e=a3pDg8
The only differences between my current photo-interrupter and IR diode circuits compared with those in the link are in the value of the IR diode ballast resistors.
For the Choir and Swell keyboards they are 820 ohm, giving a forward voltage of 1.13 V.
For the Great I tried decreasing them to 680 ohm to give VF of 1.2V but it made no difference.
Please don’t deride my Arduino sketches – I’m great at plagiarizing/adapting but a complete non-starter at programming!
Graham
Re: Arduinos and Photo-interrupters -help!
Hello Bruce
Several years ago I encountered a MIDI keyboard encoder which came with instructions for changing the MIDI channel and start-note using the first three or four keys of the keyboard itself. Hopefully your supplier will provide something similar.
Graham
Several years ago I encountered a MIDI keyboard encoder which came with instructions for changing the MIDI channel and start-note using the first three or four keys of the keyboard itself. Hopefully your supplier will provide something similar.
Graham
Re: Arduinos and Photo-interrupters -help!
I find it strange that the keyboards work perfectly when assigned to MIDI channel 1. For another test, try using the Mega for one keyboard and the DUE for another, both set a channel 1. Since HW sees them as 2 different interfaces, they can both have the same channel assignment without issue.
I'm sure you've encountered the issue of having a potentiometer programed on an Arduino Analog port and without valid connections (+ swipe Gnd) a skew of MIDI data get sent out in a constant loop or you get all ports sending data when activating a pot. This is fairly common when you've programmed say 2 pots but only one is connected.
Just some thoughts.
Danny B.
I'm sure you've encountered the issue of having a potentiometer programed on an Arduino Analog port and without valid connections (+ swipe Gnd) a skew of MIDI data get sent out in a constant loop or you get all ports sending data when activating a pot. This is fairly common when you've programmed say 2 pots but only one is connected.
Just some thoughts.
Danny B.
Re: Arduinos and Photo-interrupters -help!
Good suggestion, thanks, Danny.
Unfortunately I can't currently use the Due and the Mega at the same time because I removed the "shield" (shown in the link in my last post ) from the Due and modified it to use with the Mega.
When I made my first Arduino encoder I quickly found that leaving programmed analogue pins unconnected led to MIDI chaos.
While testing this set-up, both expression pedals were connected and - for the record - working satisfactorily.
Graham
Unfortunately I can't currently use the Due and the Mega at the same time because I removed the "shield" (shown in the link in my last post ) from the Due and modified it to use with the Mega.
When I made my first Arduino encoder I quickly found that leaving programmed analogue pins unconnected led to MIDI chaos.
While testing this set-up, both expression pedals were connected and - for the record - working satisfactorily.
Graham
Re: Arduinos and Photo-interrupters -help!
Well, the optical switches you used are designed to operate at 5v, so I would be surprised if they operate correctly at 3.3. That can explain the problems you were having with the Due!
Looks like it is my original code with some modifications! However, since you picked up on it, I made some changes to improve the debounce. That’s probably what the issue is. The new code has adjustable and more robust debounce than the original. The original code worked, but I found it was subject to interference (in my case, someone using the shower), and the interference would be picked up as random notes on and off. Your interference might actually be the other manuals. So I’d incorporate the new changes, and see how it goes. It’s findable on the Arduino Hub page, but that page is pretty slow since they switched servers. I’ll just send you the new version by email, since we communicated at some time in the past!
Looks like it is my original code with some modifications! However, since you picked up on it, I made some changes to improve the debounce. That’s probably what the issue is. The new code has adjustable and more robust debounce than the original. The original code worked, but I found it was subject to interference (in my case, someone using the shower), and the interference would be picked up as random notes on and off. Your interference might actually be the other manuals. So I’d incorporate the new changes, and see how it goes. It’s findable on the Arduino Hub page, but that page is pretty slow since they switched servers. I’ll just send you the new version by email, since we communicated at some time in the past!
Re: Arduinos and Photo-interrupters -help!
Also keep in mind that the voltage drop across a typical diode like a 1N4001 will be at least .7 volts at low current, and about 1.1 volts at 1 amp. Given that you are already starting at a low voltage, this may also be a contributing factor.
- NickNelson
- Member
- Posts: 880
- Joined: Tue Dec 20, 2005 10:31 am
Re: Arduinos and Photo-interrupters -help!
Hi Graham,
Sorry to hear you are having troubles.
I seem to recall that when I developed an optical scanning system along the same lines, I ran into trouble by having the diode IR emitters on all the time and switching the phototransistors on and off, in banks of 8. The difficulty was, I think, that the photo transistors couldn't switch into detect mode fast enough. I ended up leaving the detectors permanently on and switching the diode banks on and off sequentially.
Unfortunately I am having a hip replacement tomorrow so might be out of action for a while. If your problem isn't resolved by the time I'm up and about again I'll take a closer look.
All the best,
Nick
Sorry to hear you are having troubles.
I seem to recall that when I developed an optical scanning system along the same lines, I ran into trouble by having the diode IR emitters on all the time and switching the phototransistors on and off, in banks of 8. The difficulty was, I think, that the photo transistors couldn't switch into detect mode fast enough. I ended up leaving the detectors permanently on and switching the diode banks on and off sequentially.
Unfortunately I am having a hip replacement tomorrow so might be out of action for a while. If your problem isn't resolved by the time I'm up and about again I'll take a closer look.
All the best,
Nick
Re: Arduinos and Photo-interrupters -help!
Hi Nick
Praying that all goes smoothly for you tomorrow!
I suspect my photo-interrupter problem is different from what you experienced.
My keyboards work perfectly with a Midi Boutique hwce2x encoder.
It's the Arduino encoder that won't cooperate
Regards
Graham
Praying that all goes smoothly for you tomorrow!
I suspect my photo-interrupter problem is different from what you experienced.
My keyboards work perfectly with a Midi Boutique hwce2x encoder.
It's the Arduino encoder that won't cooperate
Regards
Graham
Re: Arduinos and Photo-interrupters -help!
Just to bring a degree of closure to this thread:
larason2’s updated code did not fix the problem, but thank you nevertheless, Carlos! At least it demonstrated that contact-bounce was not the culprit.
After installing that I spent a couple more days trying various tweaks, but all to no avail.
Finally, in desperation I decided to “borrow” the Midi Boutique HWce2x encoder from my own console and install it in the console that I have just built – and immediately everything worked:- three keyboards with photo-interrupter key contacts, three piston rails with push-button switches and two expression pedals, all connected and sending the correct MIDI messages. And if I’d connected the pedal board with reed switch contacts, I’m confident that also would have behaved properly.
For completeness I then put the Arduino Mega encoder into my own console which has keyboards with reed switch contacts – and immediately everything worked properly!
So, my conclusion is that there is nothing wrong with my opto-switch contact keyboards; there is nothing wrong with my Arduino encoder (or larason2’s code!): but for some reason they don’t get on with each other.
Perhaps it’s true what they say: “You get what you pay for,” and I was naïve to expect a £20 Arduino to match the performance of a £200+ Midi Boutique encoder. But I’m not convinced (I suspect I need to do something about Vcc, Vout and Gnd in order to get the photo-interrupters and the Arduino engaging with each other civilly), and there may yet be a sequel to this thread.
In the meantime, after considerable heart-searching, I decided to sacrifice my HWce2x to the budget console project (thus effectively doubling the budget!) where it is now properly installed. And once the budget console is shipped, I will focus on accommodating the Arduino kit into my own console.
Graham
larason2’s updated code did not fix the problem, but thank you nevertheless, Carlos! At least it demonstrated that contact-bounce was not the culprit.
After installing that I spent a couple more days trying various tweaks, but all to no avail.
Finally, in desperation I decided to “borrow” the Midi Boutique HWce2x encoder from my own console and install it in the console that I have just built – and immediately everything worked:- three keyboards with photo-interrupter key contacts, three piston rails with push-button switches and two expression pedals, all connected and sending the correct MIDI messages. And if I’d connected the pedal board with reed switch contacts, I’m confident that also would have behaved properly.
For completeness I then put the Arduino Mega encoder into my own console which has keyboards with reed switch contacts – and immediately everything worked properly!
So, my conclusion is that there is nothing wrong with my opto-switch contact keyboards; there is nothing wrong with my Arduino encoder (or larason2’s code!): but for some reason they don’t get on with each other.
Perhaps it’s true what they say: “You get what you pay for,” and I was naïve to expect a £20 Arduino to match the performance of a £200+ Midi Boutique encoder. But I’m not convinced (I suspect I need to do something about Vcc, Vout and Gnd in order to get the photo-interrupters and the Arduino engaging with each other civilly), and there may yet be a sequel to this thread.
In the meantime, after considerable heart-searching, I decided to sacrifice my HWce2x to the budget console project (thus effectively doubling the budget!) where it is now properly installed. And once the budget console is shipped, I will focus on accommodating the Arduino kit into my own console.
Graham
- vpo-organist
- Member
- Posts: 344
- Joined: Wed Apr 29, 2020 6:49 am
Re: Arduinos and Photo-interrupters -help!
Can anyone give me please a link to Larason's arduino code?
I had downloaded the code from 1drive before, but didn't remember the link.
I had downloaded the code from 1drive before, but didn't remember the link.
- vpo-organist
- Member
- Posts: 344
- Joined: Wed Apr 29, 2020 6:49 am
Re: Arduinos and Photo-interrupters -help!
Some days ago I found somewhere the file
MEGA_MIDI__Encoder_8x8x4_B.ino
with the status July 15, 2023.
I was curious how to reduce the code by using loops.
I was able to reduce querying the hardware pins and testing and sending the midi codes to 74(!) lines.
About 4/5 to 5/6 of the code can be removed by using loops.
I adopted the query logic of the pins and my C program for the RaspPi is fewer than 200 lines long.
What I noticed:
- there are no pauses/sleeps in the code. That can be a problem.
- the lastA/B/C/D arrays are not initialized. This can potentially lead to errors.
- the MidiCodes for 5 manuals go from 36-96, but the code queries up to 99.
- The Indicies [5] to [7] of keysA/B/C/D (= 97, 98, 99) are to be questioned:
-- keysA[7][7] = 99 is evaluated but never initialized.
-- keysB[7][7] = 99 is read from pin 36. What is the code for?
-- keysC[7][7] = 99 is read from pin 39. What is the code for?
-- keysD[7][7] = 99 is read from pin A12. What is the code for?
-- etc.
You can make a small clear program out of the code, which is then also easier to debug.
If anyone is interested in a more compact code, I'd be happy to provide it. The essential functions are
void readKeys(int pin, int key, byte kb[8][8], byte pins[8])
void readAllKeys()
void writeKeys(int& note, int noteOn, int noteOff, int key, byte kb[8][8], byte kbLast[8][8])
void writeAllKeys()
And the loop() has become really small
MEGA_MIDI__Encoder_8x8x4_B.ino
with the status July 15, 2023.
I was curious how to reduce the code by using loops.
I was able to reduce querying the hardware pins and testing and sending the midi codes to 74(!) lines.
About 4/5 to 5/6 of the code can be removed by using loops.
I adopted the query logic of the pins and my C program for the RaspPi is fewer than 200 lines long.
What I noticed:
- there are no pauses/sleeps in the code. That can be a problem.
- the lastA/B/C/D arrays are not initialized. This can potentially lead to errors.
- the MidiCodes for 5 manuals go from 36-96, but the code queries up to 99.
- The Indicies [5] to [7] of keysA/B/C/D (= 97, 98, 99) are to be questioned:
-- keysA[7][7] = 99 is evaluated but never initialized.
-- keysB[7][7] = 99 is read from pin 36. What is the code for?
-- keysC[7][7] = 99 is read from pin 39. What is the code for?
-- keysD[7][7] = 99 is read from pin A12. What is the code for?
-- etc.
You can make a small clear program out of the code, which is then also easier to debug.
If anyone is interested in a more compact code, I'd be happy to provide it. The essential functions are
void readKeys(int pin, int key, byte kb[8][8], byte pins[8])
void readAllKeys()
void writeKeys(int& note, int noteOn, int noteOff, int key, byte kb[8][8], byte kbLast[8][8])
void writeAllKeys()
And the loop() has become really small
Code: Select all
void loop()
{
while (true)
{
readAllKeys();
writeAllKeys();
break; // comment out for infinite loop
}
}
Re: Arduinos and Photo-interrupters -help!
Presumably you found this code by going to the link that I included in the 4th post in this thread.
It is my adaptation of larason2’s original code.
He subsequently provide me with an updated version of the code which included de-bouncing.
I adapted that to fit the pin-out of my Arduino MEGA in the same way.
I have added this new version to the One Drive folder, so presumably if you go to the link again you should find it, labelled MEGA_MIDI__Encoder_8x8x4.
The delay added at the end of the loop reduced the MIDI chaos slightly, but did not eliminate it.
The “8x8x84” indicates that it supports four 8x8 switch matrices.
My keyboards have only 60 keys (036-C to 095-B), and the pedal board has 32 pedals, so there are some IDC connector pins (or flat cable tracks) that are not connected to keys or pedals. They can, however be connected to other physical switches or buttons if desired.
I guessed that somebody who knows how to program would be able to reduce this code to a much smaller number of lines, but, as I said in my earlier post, I am good at copying and adapting, but I can’t program from scratch – and I certainly can’t do “nesting” and stuff
I would certainly be interested to see how it looks suitably slimmed-down. But whether I’ll be able to figure out what’s going on and adapt it to my set-up is questionable
It is my adaptation of larason2’s original code.
He subsequently provide me with an updated version of the code which included de-bouncing.
I adapted that to fit the pin-out of my Arduino MEGA in the same way.
I have added this new version to the One Drive folder, so presumably if you go to the link again you should find it, labelled MEGA_MIDI__Encoder_8x8x4.
The delay added at the end of the loop reduced the MIDI chaos slightly, but did not eliminate it.
The “8x8x84” indicates that it supports four 8x8 switch matrices.
My keyboards have only 60 keys (036-C to 095-B), and the pedal board has 32 pedals, so there are some IDC connector pins (or flat cable tracks) that are not connected to keys or pedals. They can, however be connected to other physical switches or buttons if desired.
I guessed that somebody who knows how to program would be able to reduce this code to a much smaller number of lines, but, as I said in my earlier post, I am good at copying and adapting, but I can’t program from scratch – and I certainly can’t do “nesting” and stuff
I would certainly be interested to see how it looks suitably slimmed-down. But whether I’ll be able to figure out what’s going on and adapt it to my set-up is questionable