The final result of this project will be a device that counts the distance I swim in the pool. The initial version calls out the distance on a speaker using a speech synth library. I was going to use an Arduino sound shield that I had from a previous project, but I couldn't' get it to work again with a nano. 

I also looked into building an underwater speaker, and I may do that at some point, but there's nothing commercially available that's remotely cheap enough. 

The current version uses the speaker but also shows the distance on a LED display.

I'm also thinking about buying a cheapo smart watch off of Alibaba and writing a android app that counts and displays the app. I don't know what sensors are available or if I can even load an app on a cheapo smart watch. That's a project for another day. 

  1. Test ultrasonic sensor on breadboard. Done. Works
  2. Write code to detect swimmer coming and going and count laps. Assume 45-1:30 times for 50 yards. Count each lap as 50 yds. Write output as serial

Psuedocode - real code and fritzing diagram is in github: https://github.com/PeterQuinn925/Lap-Counter

Psuedocode
0) Wait until momentary pushbutton starts (look into interrupts)
     Start_time = millis()
     Last_lap_time = millis()
   1) When Distance <100 cm for more than .25 sec and milis() - last_lap_time > 40 sec. 
      2) Lap completed.
          Increment lap_count
          Yards = lap_count * 50
     While distance <100 cm for more than 0.25 sec
        Announce yards
        Serial print yards
        Serial print elapsed time
        Serial print average time for 100 yds
    While end
    Last_lap_time = millis()

Note: This code isn't exactly what I wrote, but it's what I thought up at the time.

3. Add Amp and battery. https://www.sparkfun.com/tutorials/392.

4. Added a reset switch and an on/off switch. It all works great on the bench on USB power. 

5. Built a simple enclosure out of a repurposed takeout container. I cut a hole in it for the ultrasonic sensor "eyes" and for the speaker. I cut holes in the top for the switches. This is the part of the project that I suck at. I can solder and program, but all I do is make a big mess with hot glue.

Let's take it to the pool and test for real. 

5. The good news is the sensor works fine and it counts laps, at least for a while. The bad news is that it's not nearly loud enough. Also, it failed somewhere between 10 and 20 minutes in after running for hours with no problems on USB power.

6. Added battery Booster. Seems to help. https://www.adafruit.com/product/4654 

7. Added Adafruit 7 segment display w/backpack https://learn.adafruit.com/adafruit-led-backpack/0-dot-56-seven-segment-backpack-arduino-setup

7/26

Tried it in the pool. In the bright sunshine, it's hard to read the LED but not impossible. Most of the time I swim at or before dawn so it will be no problem. The biggest problem is that it stopped recording laps after 350 yards (7 laps). I think it might be because a water droplet got on the ultrasonic detector.

To minimize that kind of thing happening again, I cut out a piece of plastic and hot glued it above the "eyes" and now it looks like a person wearing a baseball cap. Will test again tomorrow.

7/27

It basically works, but I have a bug in my logic somewhere. Three or four times during my swim it appeared to freeze but several laps later it caught back up. It should never be able to count laps and not update the display so there's obviously a bug. Shocking, I know. 

I looked at the code after work and I think I found the problem. I should be using a Do-While loop that guarantees one pass through the loop rather than a While that does not. I changed it. I'll do some more testing and see if that's it.

8/8/2020

It worked great for several workouts but last time out, it didn't. It worked for 300 yds and then stopped. I reset it shortly afterwards and it started up but never counted any laps. If there was a short, I feared it would overheat and let the magic smoke out, so I turned it off. When I got back home, it seemed to work ok. I suspect a loose wire or a short.

I've been wanting to take out the amp and disconnect the speaker since I wasn't using it. I took it apart, took the amp out, disconnected the speaker and checked all the other wires. I think one of the ground wires was loose or possibly disconnected. I hooked it all up and it was working again on the bench. 

I still have the speaker hot glued to the container and it's the biggest hole in the container and the most likely place for water to get it. At some point I may build a new container now that I no longer need the speaker. It can be a lot smaller.

Since I took out the speaker, I also removed all of the voice and amp control code. I left it in the original Lap_counter.ino code in case I want it in the future, but from now on, I'm using Lap_counter_novoice.ino.

While I was there, I also changed the display to alternate the elapsed time with the lap count.

We'll see how well it works at the pool when I swim on Sunday morning. 

8/10

It worked fine for about 1900 yds on Sunday and then then stopped. I'm puzzled. I don't know if there is a logic issue, a short, or something else. I plugged it in to my laptop and ran it with the serial monitor immediately after getting home and it ran fine. This doesn't rule out a loose connection, but I'm not sure. 

I changed the software again, this time so the clock updated the display correctly but otherwise didn't change anything else. I tested it in the house numerous times and it never failed. 

This morning (monday), I used it in the pool and it ran flawlessly.