#define echoPin 6 #define trigPin 7 #define buzzerPin 8 #define INT32MAX 0x7fff #define SOUND_SPEED 34320 #define MAX_RANGE 70 #define MIN_RANGE 5 int LOWER = INT32MAX; int HIGER = 0; bool forward, backward; unsigned long period_start; struct deney{ int MAX; int MIN; int period; }; deney arr_period[256]; int arr_counter = 0; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(buzzerPin, OUTPUT); Serial.begin(9600); } int get_distance(){ float duration, distance, distance2; digitalWrite(trigPin,LOW); delayMicroseconds(5); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * SOUND_SPEED/2000000; return (int)(distance); } void print_all_times(){ for (int k = 0; k < arr_counter; k++){ String time_to_press = String(k + 1, DEC) + ". time(ms): "; time_to_press += String(arr_period[k].period, DEC); Serial.println(time_to_press); } } int last_distance = 0; void loop() { delay(200); int distance = get_distance(); if (!distance){return;} if (!last_distance){last_distance = distance;}; if (abs(last_distance - distance) <= 1){return;} if (!period_start && !forward && !backward){ period_start = millis(); } if (!forward){ if (distance < LOWER){ LOWER = distance; } if (last_distance < distance){ forward = true; } } if (!backward){ if (distance > HIGER){ HIGER = distance; } if (last_distance > distance){ backward = true; } } last_distance = distance; if (forward && backward){ int period = millis()- period_start; if (period > 100){ String time_to_press = "min: " + String(LOWER, DEC) + " max: " + String(HIGER, DEC) + " period: "; Serial.print(time_to_press); Serial.println(((float)period / 1000)); } LOWER = INT32MAX; HIGER = 0; forward = false; backward = false; period_start = 0; last_distance = 0; } }