// Private Defintions
#define IR_pin A0
#define distance_1 20
#define distance_2 40
// Gloabal Variables
int adc_val;
int num_of_push_ups;
int state_1 = 0;
float distance = 0.0;
// Arduino Initialization
void setup()
{
pinMode(IR_pin, INPUT);
Serial.begin(9600);
}
// Infinite loop
void loop()
{
/*
Reads ADC value which is in range of 0-1023
When ADC = 1023 -> Distance = 0
When ADC = 0 -> Distance = 50
Construct straight line:
y = -50/1023 x + 50
y is distance
x is ADC value
This function will convert value from (0,1023)
into distance from 50 to 0cm
*/
adc_val = analogRead(IR_pin); //read ADC value
distance = -(50.0/1023.0)*adc_val+50.0; //calculate distance from adc value
/*
Check completion of push-up
When distance is less than 20 cm
State 1 is enabled, so program is waiting for user to reach 40cm
When distance is larger than 40 cm, number of push ups completed is increased
Output is printed in the serial monitor
*/
if (distance <= distance_1)
{
state_1 = 1;
}
if (distance >= distance_2 && state_1)
{
num_of_push_ups++;
state_1 = 0;
Serial.print("Number of push-ups:");
Serial.println(num_of_push_ups);
}
delay(10);
}