Photocells with Keyboard.h

I'm a beginner programmer and I'm working on a project using the Keyboard.h library. Long story short, I'm using a photocell to input keyboard presses when it is blocked from light. My issue is that I want to be able to use the photocell to input only one keyboard press while it is being covered, not having it press the keyboard button numerous times while it is covered (it inputs multiple times because it keeps detecting that the photocell is covered since it is looping). How would I go about programming it to only input once? Hopefully that makes sense :). Lmk if this whole thing needs more clarification, and I can elaborate more on what exactly my project is/involves and why having it only input once is necessary. Thanks.

Look for change in state instead of state alone.
It means that you keep the previous state (aka last read) in a variable to compare each new read to.

BTW, photocells are slow but fingers are slower. Look for photo-transistors. In a bundle/bag, 5 cents ea was good before 2020.
Photo-transistor is a BJT with light as the gate. They output analog.
And the do it extremely fast.

You might also play with DIY touch sensors. A paperclip will hold bare wire to foil (can't solder aluminum). Capacitive Sensing -- scroll down for the article itself.
That is Intro only, there's improvements and apps like wire capacitance used on phone screens that make serve a KB well.

1 Like

Would appreciate if you kindly clarify the above concept with an example.

If the OP wants more, that is time for the OP to think and ask.
The implications may not be lost. The question tells much too.

I might misunderstand your question but the IDE comes with an example for state change detection :wink:

3 Likes

I'm looking at the IDE's example for state change detection and I'm sure this will fix my problem. Thanks guys!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.