FullyJosh

Okay so I am working on this laser harp project right now. Essential there is a laser that shines on a photocell and when the laser light is blocked by a hand, a note will play on a virtual musical keyboard.

I have it setup right now and it is working however I just need to tweak it so it will be more musical sounding. When the laser is blocked it will repeatability hit the key until my hand is removed from the laser. Is there a way to have it stay down until my hand is removed so that the notes will sound more fluid?

The problem is that the value is constantly changing. When the laser is on it the analogRead value hovers between 830-1000 depending on how much of the laser hits the photocell. Could I get that to work even if the values will be changing constantly?

You need to define a threshold. Above that triggers an action. Below that, nothing happens. Then, the new vs. old is a matter of "on this pass, am I above the threshold but was not last time, or am I below the threshold, but was not last time".

The art of getting good answers lies in asking good questions.

FullyJosh

You need to define a threshold. Above that triggers an action. Below that, nothing happens. Then, the new vs. old is a matter of "on this pass, am I above the threshold but was not last time, or am I below the threshold, but was not last time".

I apologize if I am totally missing where you guys are coming from but I tried doing this and now have it doing almost the exact same thing.

Now, you have a threshold. What you don't have is any record of the previous reading's position relative to the threshold. Re-read what I said. You want to output a value if the previous reading was not above the threshold and the current one is. You MUST keep track of the previous reading.

Now, you have a threshold. What you don't have is any record of the previous reading's position relative to the threshold. Re-read what I said. You want to output a value if the previous reading was not above the threshold and the current one is. You MUST keep track of the previous reading.

Okay so I have been trying to rationalize what you have been saying and I gave it a shot. I am totally missing the boat on something. I tried to explain what I was thinking.

PeterH

I can't believe I made that mistake! Sadly it didn't fix the problem. I just tested it again now.

I think you have fixed that problem, but presumably it's not the only problem in your code. It's not at all unusual to have multiple bugs, and you will need to locate and fix each of them before everything works. To start with, now that you know you've made this mistake in one place, I suggest you carefully review the rest of your code for any other mistakes where you used an assignment instead of a condition, or vice versa.

Your code would be a lot easier to see the structure of if every { was on its own line, and you used Tppls + Auto Format to fix the horrid indenting. Do that, and fix the other mistakes that have been pointed out, and post your code again.

Then read about arrays to find out how to generalise it to multiple strings.

Your code works MUCH better than mine and works the way I want when in the serial monitor however it is still pressing the key too fast and doesn't press the note. Is there any way I could possibly tweek this to work a little bit better?

Edit:Okay so I have been testing out the code dc42 posted and I am noticing that when I am in the serial monitor everything is working as needed, however when I run aac keys I notice that sometimes when I go to cover the laser, that the actual laser cuts out and will blink at random on it's own. I noticed the laser do this with other codes but I figured it might have had something to do with my code being 'wonky'.