Skip to content

Instantly share code, notes, and snippets.

@CarlosGabaldon
Created December 12, 2023 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosGabaldon/681050554c801a9830dea20166fde17b to your computer and use it in GitHub Desktop.
Save CarlosGabaldon/681050554c801a9830dea20166fde17b to your computer and use it in GitHub Desktop.
Review of Arduino code
#include <SD.h>
#define POTENTIOMETER_PIN A0
int potValue;
int previousPotValue;
bool buttonState = HIGH;
bool previousButtonState = HIGH;
bool recording = false; // Flag to indicate whether data recording is active
unsigned long startTime = 0; // Variable to store the start time
int rateOfChange;
const int buttonPin = 7; // Button input pin
int data[100]; // Array to store data
int dataIndex = 0; // Index of the next element in the array
void setup() {
Serial.begin(9600);
initializeSDCard();
// Set button pin as input with pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Read potentiometer value
potValue = analogRead(POTENTIOMETER_PIN);
// Save current value
previousPotValue = potValue;
// Calculate rate of change
rateOfChange = potValue - previousPotValue;
//Current button pin value
buttonState = digitalRead(buttonPin);
// Save current state
previousButtonState = buttonState;
// Check if the button is pressed and has changed state
if (buttonState == LOW && previousButtonState == HIGH) {
recording = !recording; // Toggle recording state
if (recording) {
startTime = millis(); // Record the start time
}
delay(500); // Debounce delay
}
if (recording) {
// Calculate elapsed time in seconds
unsigned long elapsedTime = (millis() - startTime) / 1000;
printToSerialMonitor(elapsedTime, potValue, rateOfChange);
// Save data to a text file on the SD card
saveDataToFile(elapsedTime, potValue, rateOfChange);
}
// Print potentiometer value to serial monitor for debugging
Serial.print("Potentiometer Value: ");
Serial.println(potValue);
// Store data in array
data[dataIndex++] = potValue;
// Check if array is full
if (dataIndex >= 100) {
dataIndex = 0; // Reset index
}
delay(100); // Delay between readings
}
void initializeSDCard() {
if (SD.begin(10)) { // Change 10 to the chip select pin you're using for the SD card
Serial.println("SD card initialized.");
} else {
Serial.println("Error initializing SD card.");
return;
}
}
void printToSerialMonitor(unsigned long elapsedTime, int potValue, int rateOfChange) {
// Print and save data only when recording is active
Serial.print("(");
Serial.print(elapsedTime);
Serial.print("s, ");
Serial.print(potValue);
Serial.print(", ");
if (rateOfChange > 0) {
Serial.print(rateOfChange);
Serial.print(", ");
} else {
Serial.print("0, ");
}
if (rateOfChange < 0) {
Serial.print(-rateOfChange);
} else {
Serial.print("0");
}
Serial.println(")");
}
void saveDataToFile(unsigned long elapsedTime, int potValue, int rateOfChange) {
// Open the data.txt file on the SD card in append mode
File dataFile = SD.open("data.txt", FILE_WRITE);
// If the file opened successfully, write data
if (dataFile) {
dataFile.print("(");
dataFile.print(elapsedTime);
dataFile.print("s, ");
dataFile.print(potValue);
dataFile.print(", ");
if (rateOfChange > 0) {
dataFile.print(rateOfChange);
dataFile.print(", ");
} else {
dataFile.print("0, ");
}
if (rateOfChange < 0) {
dataFile.print(-rateOfChange);
} else {
dataFile.print("0");
}
dataFile.println(")");
// Close the file
dataFile.close();
} else {
Serial.println("Error opening data.txt");
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment