// Define the variables
unsigned long previousMillis = 0; // To store the previous time in milliseconds
unsigned long counter = 0; // Counter variable to avoid wrap-around
void setup() {
// Initialize Serial communication
Serial.begin(1000000); // Change the baud rate as needed
while (!Serial) {
// Wait for Serial port to connect
}
}
void loop() {
// Get the current time in milliseconds
unsigned long currentMillis = millis();
// Calculate the elapsed loop time in microseconds
unsigned long elapsedLoopTime = micros() - previousMillis;
// Update the previousMillis for the next iteration
previousMillis = micros();
// Print the data to the Serial console
Serial.print("Time (ms): ");
Serial.print(currentMillis);
Serial.print(" - Counter: ");
Serial.print(counter);
Serial.print(" - Elapsed Loop Time (us): ");
Serial.println(elapsedLoopTime);
// Increment the counter to avoid wrap-around
counter++;
// Add a delay to control the loop rate (optional)
delay(1000); // 1 second delay (adjust as needed)
}