Keyboard where note is controlled by light sensitive resistor - electronics help request


(Hannah Mackaness) #1

Hi there,
I was hoping someone might be able to please sign me in as a guest for electronics night for some help with a project I’m building. (I’ve only been to an open evening)

I’m trying to build a keyboard where each note is determined by how much light gets into a light sensitive resistor.

The code is quite hacky but it works… well… “works”

  • with one button the note changes cleanly according to how much light gets into the resistor (works for either button)
  • if I push the code for both buttons the notes do change according to how much light gets into the resistor. However, the sound is warped and there seems to be a delay?

I tried poking around on some forums but with no luck - the main suggestion was to add a capacitor to the piezo but all this has done is reduce the volume. Someone else suggested refactoring it to use a PWM output for tone output and button detection by interrupts. I’m going to look into this a bit more before Wednesday.

I’m reluctant to play around with this too much because I’m worried I’ll fry something accidentally. I don’t necessarily need to fix this - I just want to understand what I should research next and why the sound is coming out like this.

If this sounds like something someone could be interested in please let me know!

github: https://github.com/Hanmack101/keyboardThermin/blob/master/chirrup
Setup is two buttons connected via power and a resistor, a piezo and a light sensitive resistor.


(Andrea Campanella) #2

Well.
Welcome to the world of polyphony.

What’s happening to your code is that you are trying to play two notes at the same time with some hardware that can only play one note. This was a common issue in cheap keyboards in the '80s , you basically need a synth, something that can be fed with multiple frequency and combine them together.

have a look at:
https://playground.arduino.cc/Main/ArduinoSynth/


(Hannah Mackaness) #3

hey emuboy,
Thanks for this. I’m not quite sure what you mean though - please can you highlight for me where the code is trying to play two notes at the same time?
I thought that structuring it with the if statement meant that it was either playing C OR G - but not both at the same time. In the setup there are 2 buttons, and each button should play 2 notes (dependent on how much light gets into the LDR) and the buttons don’t need to play notes at the same time.

My issue is that both buttons break (output is bitty and quiet although you can hear the note change) when I upload the code for both at the same time. If I only load the code for one button, it works as expected (clear note which changes smoothly depending on how much light gets into the resistor).

I’m not sure if you use instagram but I have added a story highlight illustrating the issue at bule_makannasi


(electrotech) #4

I know that emuboy is quite busy so I will try to offer a little bit of input.

Firstly: I doubt I can explain any more clearly than what he had already.

Secondly: have you considered using digitalread for buttons instead? This would be preferred usually.

Lastly: I notice that if a particular button isn’t activated your code calls:

noTone(8)

This is interfering with your sound, cutting it off prematurely.

Every time your program loops round it’s checking both input buttons but if one of them isn’t pressed it’s cancelling all tone output regardless. It’s ignoring the fact that actually one button is still pressed and should play sound.
Similarly when both buttons are pressed together it plays each note for a fraction of a second and starting with the first button and moves on to play the second button… then the first… looping forever creating crap sound.

You need to check if you should be playing sound or not and act accordingly in one location of your code. Definitely not in two sequential parts like this.


(Andrea Campanella) #5

I had a look at your story, you need to denounce the buttons :

https://www.arduino.cc/en/tutorial/debounce


(Hannah Mackaness) #6

thanks both - this is really helpful - I will take a look and try to fix it!


(Hannah Mackaness) #7

fixed! Thanks for your help!
I didn’t have a good reason for not using digital read - when I switched to digital read I tested it and didn’t need to denounce the buttons (high low is sufficient).
Once I could just use high/low I could scrap having noTone( ) in my loop - which was making it chirrup in the first place.

It now works as expected! although actually I kinda miss the chirrup…


(electrotech) #8

Yay congrats on fixing it. Happy to help. :smile: