A programming challenge involving piezo sensors, LEDs, and relays

I’m so so happy you’re making progress. I also didn’t know how to read the sensors. If you’ve increased the delay time it might be too slow to recognise the movement. Otherwise you can play with the two variables a near the top of the file that set the sensitivity of the sensors. You see, the value you read from them is a range so I set threshold for each so that you can fiddle those values to correctly recognise a hit without it being so sensitive that you read false hits.

See lines 84 through 87:

// Target values can be from 0 through 1023. To avoid false "hit" detection, modify the threshold that the value must exceed to be recognised as a hit.
// These are likely to be the same, but the reality is that one target might be more sensitive than another so we use two values for flexibility.
int target1Threshold = 512;
int target2Threshold = 512;

I hope you and your friend have many hours of fun with it. I’d love to see it finished and doing it’s thing.

1 Like

Ok so I fixed the values for now until I run the actual test. Here’s a video:https://www.dropbox.com/s/nxqiauk5l2b866a/IMG_3882.MOV?dl=0
so the only two things to tweak at this point is the time values to seconds with decimal points after (ex. player 1 hits in 2 and a quarter seconds display: Player 1 2.25)and the light to flash if they win and to turn of if they lose. We are so close I’m super excited!!! Thanks so much for your help so far!

1 Like

Okay, I’m unsure as to how I would go about accomplishing the things specified in the previous comment. Is there any way I could get some help with that? In the mean time I’ve done a ton of work on the physical components like enclosures and the targets themselves.

Sure you can. Could you start with a brief summary of what’s working and what’s not working as you’d like it to?

Well everything is working but I would like the light on the target of the winner to blink and the light of the losing target to turn off. Then I would like the display to show the time in seconds rather than in thousands of a second. (Ex. It currently shows 3240 but I would like it to show 3.24)

Thanks. I’ll try find some time to address that tomorrow (it’s 11pm for me now).

1 Like

I’ve just updated the INO file to include code that turns off the loser’s LED and blinks the winner. (Untested of course, I’m flying blind here.) :grimacing:

Edit:
And now, in theory, the time should be presented in “n.nnn” format. Would love to see this if it works. :laughing:

1 Like

Haha as soon as I get home and test it I’ll send results! Thanks so much!

1 Like

Ok so I gave it a couple run throughs and this is what happened.


It works almost flawlessly to begin with but each time ends up non-resetable.

1 Like

Hmmm. That’s either an issue with the 50ms delay at the end of the loop (sleeps so long that it doesn’t recognise the button press) or something else is in a loop.

It did point out a scenario I have not planned for - of both players hot the target before the targets are enabled.

Edit:
The target sensors look very sensitive. You might want to alter the 512 value on lines 86 and 87 to something higher like 900. NB. The maximum value is 1024 - anything higher will not recognise a target hit.

int target1Threshold = 512;
int target2Threshold = 512;

Otherwise it’s looking pretty good so far. Wish I had the hardware to test alongside you but really grateful for the videos you’ve posted - they really help me see what’s going on.

Edit 2:
I noticed that the time for player 1 incremented 10 seconds into the movie. The cause is that it’s not ignoring target movement after the initial movement. I’ve just made a fix for that.

1 Like

Another update will show !!! when a player hits the target before the targets are enabled, and ... when waiting for a player to hit the target.

Edit:
I’ve just made my last change for the night to display better results when the game is over. It now caters for cases when both players false start, or where one player wins due to a false start.

1 Like

Okay so as far as sensitivity goes I’m ah the max and will probably replace the resistor in the near future. the thing still doesn’t seem to want to reset. https://drive.google.com/file/d/1jOyJ_W0cQPCpDyvlTNe_4Bb05I4ktdL6/view?usp=sharing

I don’t think you have the latest code on there. I’ve removed the “-----” displays. The possible displays for when at least one target is hit are either “…” when waiting on a player to hit their target, or “!!!” if they hit their target before the target is activated.

1 Like

ok you were totally right about the code. now the code I just downloaded won’t sense the touching of the sensors.https://drive.google.com/file/d/17w3vE7t20JvQosPlJlxEvw3c6MfCMNgX/view?usp=sharing

Oh dear, that’s no good at all. I hate to ask, but could you check they’re still connected to A3 and A1 pins on the Arduino? (The video movement is too fast for me to see.)

By the way, what is the chart on the screen showing?

1 Like

okay so the screen is displaying a value whenever the button or sensors are triggered and it just wasn’t showing having sensed the sensors. And yes they are plugged into A1 and A3.


also i want to again thank you for the help you have provided!

1 Like

I couldn’t have gotten so far on my own. Here’s a look at what is currently done overall of the project.


This is the light enclosure and the arduino enclosures I designed.

1 Like

So where do we go from here?

It might be a good idea to either change the program to provide a diagnostic/test mode. In my fish feeder project, it behaves differently for testing if the button is held down when the system is powered up.

I’ve got work in 45 minutes. I’ll “design” the test mode now and hope to implement it at lunch time.

1 Like

Good news, it didn’t take that long to do. Please get the latest version and try that. The change you should see is as follows:

On startup, the word “Startup…” should appear on the LCD for almost a second (800ms). If at the end of that the button is “down” (being pressed) then it’ll enter test mode. The simplest is just to hold it down when powering up until you see the message “TEST MODE” on line one and “Release button” on line 2. Releasing the button will start the tests:

During test mode, line one will show “TEST MODE”. Line 2 will show the test in progress in this order:

  1. Relay 1 ON (for 2 seconds)
  2. Relay 2 ON (for 2 seconds)
  3. Relay 1 OFF (for 2 seconds)
  4. Relay 2 OFF (for 2 seconds)
  5. Show the value for piezo vibration sensor 1 as “PV1: 0000” (for 5 seconds)
  6. Show the value for piezo vibration sensor 2 as “PV2: 0000” (for 5 seconds)

After this test process, the system will start normally and you’ll see “Ready” on line one and “Press the button” on the second line.

If you could share a video of your test we’ll know if it’s a problem with the sensors or the code. (Probably the code, but I think this test mode will be valuable regardless.)

1 Like