// Define the pins for the potentiometer and pushbutton:
const int potentiometerPin = A0;
const int pushButtonPin = 2; // Change this to the pin you're using for the pushbutton
// Variables to store sensor readings:
int sensorValue = 0; // Current reading from the potentiometer
int storedValues[3] = {0}; // Array to store readings from the potentiometer
int storedIndex = 0; // Index for storing readings sequentially
void setup() {
// Initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// Set the pushbutton pin as input with internal pull-up resistor:
pinMode(pushButtonPin, INPUT_PULLUP);
}
void loop() {
// Read the input on analog pin A0 (the connection to the potentiometer's wiper):
sensorValue = analogRead(potentiometerPin);
// Check if the pushbutton is pressed (since using pull-up, it's active LOW):
if (digitalRead(pushButtonPin) == LOW) {
// Store the current potentiometer reading if the pushbutton is pressed:
storedValues[storedIndex] = sensorValue;
delay(500); // Debouncing delay
storedIndex = (storedIndex + 1) % 3; // Update the index, loop back to 0 if reaches 3
}
// Print out the current potentiometer reading:
Serial.print("Current Reading: ");
Serial.println(sensorValue);
// Print out the stored values:
Serial.print("Stored Values: ");
for (int i = 0; i < 3; ++i) {
Serial.print(storedValues[i]);
Serial.print(" ");
}
Serial.println();
// Wait for a bit to avoid spamming the serial monitor:
delay(100);
}