#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Create an LCD object (assuming I2C address of the LCD is 0x27)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Button pin
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
// Variables to store data
#define DATA_SIZE 100
int dataBuffer[DATA_SIZE];
int dataIndex = 0;
// Variables for replay
bool replayMode = false;
int replayIndex = 0;
// IO signal pin
const int ioPin = A0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Configure button as input with pull-up
lcd.begin();
lcd.backlight();
// Initialize LCD
lcd.setCursor(0, 0);
lcd.print("Data Monitor");
delay(1000);
lcd.clear();
}
void loop() {
// Read the IO signal (replace with actual frequency/data calculation)
int ioData = analogRead(ioPin);
// Store the data into the buffer
if (dataIndex < DATA_SIZE) {
dataBuffer[dataIndex++] = ioData;
}
// Display the data on the LCD (recent data)
if (!replayMode) {
lcd.setCursor(0, 0);
lcd.print("IO Signal: ");
lcd.print(ioData);
lcd.setCursor(0, 1);
lcd.print("Buffer Pos: ");
lcd.print(dataIndex);
}
// Read the button state
buttonState = digitalRead(buttonPin);
// If the button is pressed, switch to replay mode
if (buttonState == LOW && lastButtonState == HIGH) {
replayMode = !replayMode;
replayIndex = 0; // Reset replay index
}
lastButtonState = buttonState;
// Replay the data on the LCD when in replay mode
if (replayMode) {
lcd.setCursor(0, 2);
lcd.print("Replay Data: ");
lcd.print(dataBuffer[replayIndex]);
// Cycle through stored data
replayIndex = (replayIndex + 1) % dataIndex;
delay(500); // Slow down the replay speed
}
delay(200); // Adjust this delay as needed
}