Brad Martin
Published © MIT

BenchCounter

Working out is hard. Doing side projects is fun. If you do a side project with 115lbs, you're working out! Watch the video!

BeginnerShowcase (no instructions)1 hour416
BenchCounter

Things used in this project

Hardware components

Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Raspberry Pi 3 Model B Starter Pack RPI 3 Starter Kit
Itead Raspberry Pi 3 Model B Starter Pack RPI 3 Starter Kit
×1
SparkFun 7-Segment Serial Display - Red
SparkFun 7-Segment Serial Display - Red
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

BenchCounterSch

Code

BenchCounterPy

Python
import RPi.GPIO as GPIO
import time, os
import tm1637

GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 23
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)

Display = tm1637.TM1637(5,4,tm1637.BRIGHT_TYPICAL)
Display.SetBrightnes(1)

def distance():
    GPIO.output(GPIO_TRIGGER, True)
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)

    StartTime = time.time()
    StopTime = time.time()

    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()

    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()

    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2

    return distance

def verifyRep(lastRep):
    if (time.time() - lastRep < 1):
        return False
    else:
        return True

def display(counter):
    num = str(counter)
    for i in range(4 - len(num)):
      num = "0" + num
    Display.Show([int(num[0]),int(num[1]),int(num[2]),int(num[3])])

def say(phrase):
    os.system("espeak -s140 -ven+18 -z " + str(phrase))

if __name__ == '__main__':
    
    inBench = True
    firstRep = False
    dir = False # false for down, true for up
    repCounter = 0
    lastRep = time.time()

    try:
        while True:
            dist = distance()
            
            if (dist < 30):
                if (not firstRep):
                    firstRep = True
                    dir = True
                    lastRep = time.time()
                    say("READY!")
                elif (dir and verifyRep(lastRep)):
                    dir = False
                    lastRep = time.time()
                    say("DOWN")
                elif (verifyRep(lastRep)):
                    repCounter += 1;
                    dir = True
                    lastRep = time.time()
                    say(repCounter)
                    display(repCounter)
                else:
                    print("DEBOUNCE")

            time.sleep(0.05)

    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()

Credits

Brad Martin

Brad Martin

4 projects • 3 followers
I like making gizmos

Comments