#include #include #include Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(); #define NOTE_C4 262 #define NOTE_G3 196 #define NOTE_A3 220 #define NOTE_B3 247 #define NOTE_C4 262 int melody[] = { NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 }; int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; const int buttonPin = 7; const int led1Pin = 8; const int led2Pin = 9; const int led3Pin = 10; const int led4Pin = 11; const int led5Pin = 12; const int buzzer = 13; String command; int buttonState = 0; int count_value =0; int prestate =0; void setup(void) { pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT); pinMode(led3Pin, OUTPUT); pinMode(led4Pin, OUTPUT); pinMode(led5Pin, OUTPUT); pinMode(buzzer, OUTPUT); Serial.begin(9600); if(!accel.begin()) { Serial.println("No valid sensor found"); while(1); } } void loop(void) { sensors_event_t event; accel.getEvent(&event); if (Serial.available()) { command = Serial.readString(); } if(command.equals("reset")) { count_value = 0; digitalWrite(led1Pin,LOW); digitalWrite(led2Pin,LOW); digitalWrite(led3Pin,LOW); digitalWrite(led4Pin,LOW); digitalWrite(led5Pin,LOW); } if((event.acceleration.z > 10.25) && prestate == 0 && count_value < command.toInt()) { count_value++; delay(1000); Serial.print(count_value - 1); Serial.print("/"); Serial.println(command); if(count_value-1 == command.toInt()/5) { digitalWrite(led1Pin,HIGH); } if(count_value-1 == command.toInt()/5 * 2) { digitalWrite(led2Pin,HIGH); } if(count_value-1 == command.toInt()/5 * 3) { digitalWrite(led3Pin,HIGH); } if(count_value-1 == command.toInt()/5 * 4) { digitalWrite(led4Pin,HIGH); } tone(buzzer, 256, 100); delay(100); noTone(buzzer); prestate = 1; } else if(event.acceleration.z < 10.25) { prestate = 0; if(count_value == command.toInt() && count_value != 0) { digitalWrite(led5Pin, HIGH); for (int thisNote = 0; thisNote < 8; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; tone(buzzer, melody[thisNote], noteDuration); int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); noTone(buzzer); } Serial.print(count_value); count_value = 0; Serial.print("/"); Serial.print(command); Serial.println("!"); Serial.println("Congrats! You are the push-up master!"); } } }