//// Add names to pins const int PotPin = A0; const int PunchButton = 2; //// Add variables long Val = 0; float SpeedOfPunch = 0; unsigned long Timer = 0; int TimesPunched = 0; float AVGPunchingSpeed = 0; float AVGPunchingPower = 0; float PowerOfPunch = 0; //// Make the punch button work boolean ButtonState = LOW; boolean DebouncePunchButton(boolean State) { boolean NewState = digitalRead(PunchButton); if (State != NewState) { delay(10); NewState = digitalRead(PunchButton); } return NewState; } void setup() { Serial.begin(9600); pinMode(PunchButton, INPUT); delay(500); } void loop() { if (TimesPunched == 0) { if(DebouncePunchButton(ButtonState) == HIGH && ButtonState == LOW) // the first punch will start the timer { TimesPunched++; // add 1 punch Val = analogRead(PotPin); SpeedOfPunch = map(Val, 0, 1023, 5, 10) + SpeedOfPunch; // boxers generaly punch between 5 and 10 m/s PowerOfPunch = map(Val, 0, 1023, 440, 1100) + PowerOfPunch; // olympic boxers punch between 440 an 1100 pounds ButtonState = HIGH; Timer = millis(); } else if (DebouncePunchButton(ButtonState) == LOW && ButtonState == HIGH) { ButtonState = LOW; } } else if (TimesPunched >= 1) { if(DebouncePunchButton(ButtonState) == HIGH && ButtonState == LOW) // when the button is pushed { TimesPunched++; // add 1 punch Val = analogRead(PotPin); SpeedOfPunch = map(Val, 0, 1023, 5, 10) + SpeedOfPunch; // boxers generaly punch between 5 and 10 m/s PowerOfPunch = map(Val, 0, 1023, 440, 1100) + PowerOfPunch; // olympic boxers punch between 440 an 1100 pounds ButtonState = HIGH; } else if (DebouncePunchButton(ButtonState) == LOW && ButtonState == HIGH) { ButtonState = LOW; } else if ((millis() - Timer) >= 20000) // check if 20 seconds has past to simulate the training is done and print the information { AVGPunchingSpeed = SpeedOfPunch/TimesPunched; AVGPunchingPower = PowerOfPunch/TimesPunched; Serial.print(TimesPunched); Serial.print("|"); Serial.print(AVGPunchingSpeed, 2); Serial.print("|"); Serial.print(AVGPunchingPower, 2); Serial.println(); TimesPunched = 0; SpeedOfPunch = 0; AVGPunchingPower = 0; //delay(5000); } } }