How I get 100 hz frequency in arduino mega 2560

That may be the maximum writing speed of the SD file with continuous open/close commands.

Please re-format your code for proper indentation.

WHY are you writing the same data 101 times?!?

because, I want 100 sample so I am using this loop. Is this not correct way? If no so please tell me correct way.

U r correct sir, open and closing it is taking time. And I formatted my sd card. What will do.

Can you do one hundred samples then write those results to the SD card?

I guess what Johnwasser is suggesting is make things like dataString arrays so it would be dataString[I] you are writing to the file. Serial.print is a known time user so I bet dataFile is also.

Here is my best guess of the correct way. I don't have the libraries you use and certainly not the hardware so this sketch is UNTESTED. It may not compile due to typographical errors. It is up to you to fix any errors you find.

#include "Protocentral_MAX30100.h"  //for PPG MAX30100 PulseSensor library.
#include <Wire.h>                   //for I2C Comm library.
#include "RTClib.h"                 //for RTC library
#include <SPI.h>                    //for SPI comm. library.
#include <SD.h>                     //for SD Card library.
#include <Q2HX711.h>                //for pressure PulseSensor library HX710.

RTC_DS1307 RTC;                     // RTC_DS1307 class object declaration
const int chipSelect = 53;          //chip select pin for the SD card

int GSRValue = 0;         // store the gsr value
int ECGValue = 0;         // store the ecg value

//-----------------declaration for MAX30100 PPG Sensor----------------
MAX30100 PulseSensor;
long PulseIRValue, PulseRedValue;

//-----------------declaration for pressur HX710 Sensor----------------
const byte PressureSensor_data_pin  = 3;
const byte PressureSensor_clock_pin = 2;
Q2HX711 PressureSensor(PressureSensor_data_pin, PressureSensor_clock_pin);

int state = 0; //Create empty string called incomingData
const int air_pump = 5; //Pin 7 for connecting Mini Air Pump via ULN2803A driver
const int solenoid = 4; //Pin 6 for connecting solenoid Air valve via ULN2803A driver

File OutputFile;
int SampleCount = 0;
const unsigned long SampleInterval = 10;  // Milliseconds

void setup()
{
  Serial.begin(115200);
  while (!Serial) {}

  RTC.begin();
  if (RTC.isrunning())
    Serial.println("RTC is  running!");
  else
  {
    Serial.println("Start RTC and set to compile time.");
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

  Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect))
  {
    Serial.println("Card failed, or not present");
    while (1) {}
  }

  Serial.println("Card initialized.");

  Wire.begin();
  PulseSensor.begin(pw1600, i50, sr100 );
  PulseSensor.printRegisters();

  pinMode(8, INPUT); // Setup for leads off detection LO +
  pinMode(9, INPUT); // Setup for leads off detection LO -
  if ((digitalRead(8) == HIGH) || (digitalRead(9) == HIGH)) //check if leads are removed
  {
    Serial.println("ECG leads off!");
    while (1) {}
  }

  pinMode(air_pump, OUTPUT);
  pinMode(solenoid, OUTPUT);
  digitalWrite(air_pump, HIGH); //Inflates pressure cuff
  digitalWrite(solenoid, LOW);

  // Open the output file
  OutputFile = SD.open("iitd.txt", FILE_WRITE);
  if (!OutputFile)
  {
    Serial.println("Failed to open OutputFile.");
    while (1) {}
  }
}

void loop()
{
  // Check for input commands.
  if (Serial.available())
  {
    state = Serial.parseInt();
    if (state == 1)
    {
      //Serial.println("Activated");
      digitalWrite(air_pump, LOW); //Deflates pressure cuff
      digitalWrite(solenoid, HIGH);
    }
    if (state == 2)
    {
      //Serial.println("deactivated");
      digitalWrite(air_pump, HIGH); //Inflates pressure cuff
      digitalWrite(solenoid, LOW);
    }
  }

  // Record 100 samples
  if (SampleCount >= 100)
  {
    Serial.println("Finished. We have all of our samples.");
    OutputFile.close();
    while (1) {}
  }

  static unsigned long lastSampleTime = millis();
  if (millis() - lastSampleTime < SampleInterval)
    return;  // Not time to sample yet

  lastSampleTime += SampleInterval;

  SampleCount++;

  DateTime now = RTC.now();

  //-------------PulseSensor Signal Reading-------------------------------
  PulseSensor.readSensor();
  PulseIRValue = PulseSensor.IR;
  PulseRedValue = PulseSensor.RED;

  //-------------GSR Sensor Signal Reading-------------------------------
  GSRValue = analogRead(GSRPin);

  //-------------Pressure Sensor Signal Reading--------------------------
  int avg_val = (PressureSensor.read() / 100);
  float pres_volt = (avg_val * 0.0048828125) - 0.5;
  float pres_PSI = pres_volt / 13.33;
  float pressure = pres_PSI - 0.4;

  //-------------ECG Sensor Signal Reading--------------------------
  ECGValue = analogRead(ECGPin);

  // Write this sample into a line of the file
  OutputFile.print(now.day(), DEC);
  OutputFile.print('/');
  OutputFile.print(now.month(), DEC);
  OutputFile.print('/');
  OutputFile.print(now.year(), DEC);
  OutputFile.print(' ');
  OutputFile.print(now.hour(), DEC);
  OutputFile.print(':');
  OutputFile.print(now.minute(), DEC);
  OutputFile.print(':');
  OutputFile.print(now.second(), DEC);
  OutputFile.print(',');

  OutputFile.print(PulseRedValue );
  OutputFile.print(',');
  OutputFile.print(PulseIRValue);
  OutputFile.print(',');
  OutputFile.print(GSRValue);
  OutputFile.print(',');
  OutputFile.print(pressure);
  OutputFile.print(',');
  OutputFile.println(ECGValue);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.