//set the pin names const int pingPin = 6; const int echoPin = 9; const int buzzer = 10; const int button = 2; int buttonState = 0; // variable for reading the pushbutton status bool Up = 0; int pullUps = 0; int distance = 50; //convert to centimetres long microsecondsToCentimeters(long microseconds) { return microseconds / 29 / 2; } void setup() { Serial.begin(9600); pinMode(pingPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(buzzer, OUTPUT); pinMode(button, INPUT); } void loop() { int state = 0; if (digitalRead(button) == HIGH && state == 0) { state = 1; while (state>0) { long duration, cm; //send and receive ultrasound to detect distance digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); pinMode(echoPin, INPUT); duration = pulseIn(echoPin, HIGH); cm = microsecondsToCentimeters(duration); //check if distance in centimetres is less than 50cm if (cm > distance && Up) { Up = false; } else if(cm <= distance && !Up) { Up = true; pullUps++; digitalWrite(buzzer, HIGH); delay(50); digitalWrite(buzzer, LOW); delay(50); Serial.println(pullUps); } delay(300); if(digitalRead(button)==HIGH){ state = 0; delay(200); } } } }