CCCPaste

"Sensor Recorder" rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Sensor Recorder"
	- Source Code compiled for: Arduino Nano
	- Source Code created on: 2023-12-17 18:08:11

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* record sensor data and save in sd card */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/**** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t SENSOR_PIN = A0;
const uint8_t BUTTON_PIN = 2;
const uint8_t SD_CS_PIN = 10;

/****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
SdFat sd;
File dataFile;
EasyButton button(BUTTON_PIN);

void setup(void)
{
  // Initialize Serial communication
  Serial.begin(9600);

  // Initialize the button
  button.begin();

  // Initialize the SD card
  if (!sd.begin(SD_CS_PIN, SPI_FULL_SPEED))
  {
    Serial.println("SD initialization failed!");
    while (1);
  }

  // Open the data file for writing
  dataFile = sd.open("data.txt", FILE_WRITE);
  if (!dataFile)
  {
    Serial.println("Error opening data file!");
    while (1);
  }
}

void loop(void)
{
  // Check if the button is pressed
  if (button.read())
  {
    // Record sensor data and save to SD card
    int sensorData = analogRead(SENSOR_PIN);
    dataFile.println(sensorData);
    Serial.println("Data recorded and saved to SD card.");
  }
}