Home Projects Exercise counter or timer part 1

Exercise counter or timer part 1

by shedboy71
[lebox id="1"]

One of the fun things about Arduino's is coming up with projects to use Arduino's and various sensors or modules. In this project I was thinking about an example that would detect the presence of a person.

In the world of fitness and exercise this could be used in 2 ways, you could have a counter when a person moves in and out of a certain range, this would be a basic counter so you could for example count press-ups, the other idea is to detect how long a person holds the plank position – in this case it would be more of a timer example.

We will use the hc-sr04 ultrasonic sensor and connect to our Arduino

HC-SR04 Ultrasonic Sensor

HC-SR04 Ultrasonic Sensor

 

Code

A basic example which will toggle the on board LED when an object is detected within a 5 centimetres range

[codesyntax lang=”cpp”]

//HCSR-04 pins
const int trigPin = 13;
int echoPin = 11;

//anything within 5 cm triggers on board LED
int detected = 5;
 
//LED pin numbers
int testLED = 13;
 
void setup() 
{
 // initialize serial communication
 Serial.begin(9600);
}
 
void loop()
{

 long duration, cm;
 
 //initializing the pin states
 pinMode(trigPin, OUTPUT);
 pinMode(testLED, OUTPUT);
 
 //sending the signal, starting with LOW for a clean signal
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(trigPin, LOW);
 
 //setting up the input pin, and receiving the duration in microseconds for the sound to bounce off the object
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 
 // convert the time into a distance
 cm = microsecondsToCentimeters(duration);
 
 //printing the current readings to ther serial display
 Serial.print(cm);
 Serial.print("cm");
 Serial.println();
 
 //valid press up detected
 if (cm > detected)
 {
 digitalWrite(testLED, HIGH);
 }
 else
 {
 digitalWrite(testLED, LOW);
 }
 
 delay(100);
}
 
long microsecondsToCentimeters(long microseconds)
{
 // The speed of sound is 340 m/s or 29 microseconds per centimeter.
 return microseconds / 29 / 2;
}

[/codesyntax]

open the serial monitor and move an object closer to the HC-SR04, you should see something similar to the following

30cm
26cm
25cm
24cm
22cm
21cm
23cm
22cm
19cm
16cm
16cm

Share
[lebox id="2"]

You may also like