#include <LiquidCrystal.h>
#include <SD.h>
// Define LCD pins
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// Define potentiometer pins
const int potPin1 = A0;
const int potPin2 = A1;
// Define button pins
const int startandstopButton = 1;
const int zeroButton = 0;
// Define variables for time recording
unsigned long previousMillis = 0;
const long interval = 1000; // Record values every 1 second
bool recording = false;
// Variables to store the initial potentiometer values
int initialPotValue1 = 0;
int initialPotValue2 = 0;
// Button state variables and debounce interval
int lastStartStopButtonState = HIGH;
int lastZeroButtonState = HIGH;
unsigned long debounceDelay = 50;
// Define SD card chip select pin
const int chipSelect = 10;
// Define file on SD card
File dataFile;
void setup() {
// Set up serial communication
Serial.begin(9600);
// Set up the LCD's number of columns and rows
lcd.begin(16, 2);
// Initialize button pins
pinMode(startandstopButton, INPUT_PULLUP);
pinMode(zeroButton, INPUT_PULLUP);
// Initialize the SD card
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
return;
}
Serial.println("SD card initialized successfully.");
// Open file for logging
dataFile = SD.open("data.txt", FILE_WRITE);
if (!dataFile) {
Serial.println("Error opening file!");
return;
}
Serial.println("File opened successfully.");
// Print initial headers to the file
dataFile.println("Time,Angle,Force");
// Print initial message on LCD
lcd.print("Ready");
// Store initial potentiometer values
initialPotValue1 = analogRead(potPin1);
initialPotValue2 = analogRead(potPin2);
}
void loop() {
unsigned long currentMillis = millis();
// Read the state of start/stop button with debouncing
int startStopButtonState = digitalRead(startandstopButton);
if (startStopButtonState != lastStartStopButtonState) {
delay(debounceDelay);
if (startStopButtonState == LOW) {
recording = !recording; // Toggle recording state
previousMillis = currentMillis; // Reset timer
lcd.clear();
lcd.print(recording ? "Recording" : "Paused");
}
}
lastStartStopButtonState = startStopButtonState;
// Read the state of zero button with debouncing
int zeroButtonState = digitalRead(zeroButton);
if (zeroButtonState != lastZeroButtonState) {
delay(debounceDelay);
if (zeroButtonState == LOW) {
// Store the initial potentiometer values
initialPotValue1 = analogRead(potPin1);
initialPotValue2 = analogRead(potPin2);
// Clear the LCD screen
lcd.clear();
lcd.print("Zeroed");
}
}
lastZeroButtonState = zeroButtonState;
// Check if it's time to record and recording is active
if (recording && currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Read values from potentiometers
int value1 = analogRead(potPin1) - initialPotValue1;
int value2 = analogRead(potPin2) - initialPotValue2;
// Map analog values to range
int mappedValue1 = map(value1, 0, 1023, 0, 360);
int mappedValue2 = map(value2, 0, 1023, 0, 120);
// Write data to the file
dataFile.print(currentMillis);
dataFile.print(",");
dataFile.print(mappedValue1);
dataFile.print(",");
dataFile.println(mappedValue2);
// Display data on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Angle (°): ");
lcd.print(mappedValue1);
lcd.setCursor(0, 1);
lcd.print("Force (N): ");
lcd.print(mappedValue2);
}
}