/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
The original sketch
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial
has been modified avoiding the delay() function and using 115200 Bd as
state-of-the-art instead of 9600 Bd.
2023-07-29
*/
// lastMeasurement stores the time in msec when a measurement takes place
unsigned long lastMeasurement = 0; // msec
// delayTime defines the time in msec between the last and the next
// measurement
unsigned long delayTime = 1; // msec
// Count counts the loops performed between two measurements
unsigned long Count = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 115200 bits per second:
Serial.begin(115200);
}
// the loop routine runs over and over again forever:
void loop() {
// Increment Count with every loop
Count++;
// check if the last measurement has been performed (at least) delayTime ago
if (millis()-lastMeasurement >= delayTime){
// if yes: Store the new measurement time
lastMeasurement = millis();
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out how many loops have been performed
// between the last and the recent measurement
Serial.print(Count);
// print a TAB
Serial.print("\t");
// print out the value you read:
Serial.println(sensorValue);
// Reset Count
Count = 0;
}
}