#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);
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
pot1:GND
pot1:SIG
pot1:VCC
pot2:VCC
pot2:SIG
pot2:GND
lcd1:VSS
lcd1:VDD
lcd1:V0
lcd1:RS
lcd1:RW
lcd1:E
lcd1:D0
lcd1:D1
lcd1:D2
lcd1:D3
lcd1:D4
lcd1:D5
lcd1:D6
lcd1:D7
lcd1:A
lcd1:K
r1:1
r1:2
sd1:CD
sd1:DO
sd1:GND
sd1:SCK
sd1:VCC
sd1:DI
sd1:CS