// Include NewPing Library
#include "NewPing.h"
// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10
#define TRIGGER_PIN 3
#define ECHO_PIN 2
#define ANALOG_PIN A4
// Maximum distance we want to ping for (in centimeters).
#define MAX_DISTANCE 400
float val = 0;
unsigned long time_start;
unsigned long time_current;
int duration = 5000;
unsigned long current_millis;
unsigned long loop_initiate_time = 2000;
unsigned long loop_interval = 3;
// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// Define arrays to store time and voltage
#define ARRAY_SIZE 1000 // Adjust the size as needed
unsigned long time_values[ARRAY_SIZE];
float voltage_values[ARRAY_SIZE];
int index = 0; // Array index
void setup() {
Serial.begin(230400);
time_current = 0;
}
void loop() {
current_millis = millis();
time_current = 0;
if (current_millis >= loop_initiate_time) {
time_current = millis() - loop_initiate_time;
Serial.print(time_current); // the x axis of signal graph eg snapshots at different time
Serial.print(" ");
val = analogRead(ANALOG_PIN);
float voltage = (val / 4096) * 33000; // Converting the digital value to voltage
Serial.print(voltage, 5);
Serial.print(" ");
Serial.println(sonar.ping_cm());
// Store values in arrays
time_values[index] = time_current;
voltage_values[index] = voltage;
index++;
if (time_current - time_start >= duration) { // Check if 5 seconds passed
while (true) {
// You can add any other tasks to perform before the Arduino stops
}
};
// delay(loop_interval); // Wait for the specified interval before taking the next reading
}
}