SD make the program slow

good morning,

I have a problem. For an assignment I have created a program with an SD card as data logger. However, I noticed that the stepper motor started to get slower. After a bit of searching, I realized that if I remove the SD section, the loop time goes down. Without the SD the loop time is 1/3 relative to the loop time with the SD. However, I can not figure out why the SD costs so much time.

I made a new program to make it more clear. Here, however, I left a number of sections. Only the SD, stepper motor, switch button, push button and led is used only. Here too, the running time varied. Without the SD the time halved.

In the program I've made, the steppenmotor turns every 2 seconds, 3 rounds. Pressing the push button will raise a counter. If the switch button is in the correct position, and thus the LED is on, the value of the counter is written on the SD every time the push button is pushed.

In the program that i made
This is the program that i use know:

#include <SPI.h>
#include <SD.h>

File myFile;                //SD

#define button A3           //COUNTER BUTTON
#define SDswitch A8         //SD
#define pulsePin 5          //MOTOR
#define dirPin 6            //MOTOR
#define enablePin 7         //MOTOR
#define SDled 11            //SD

int timerSet = 2000;        //MOTOR: time between rotations
int rotations = 3;          //MOTOR: number of rotations
float MStep = 1600;         //MOTOR: pulse/rev (adjusteble on driver
int rpm = 80;               //MOTOR: speed
boolean dirSet = HIGH;      //MOTOR: direction

const int chipSelect = 53;  //SD

int val = 0;                //COUNTER
int currentState = 0;       //COUNTER
int previousState = 0;      //COUNTER
long counter = 0;           //COUNTER
long countertest = 0;       //COUNTER

int SDstage = 0;            //SD
int switchState;            //SD

long timePassed = 0;        //MOTER
long pulseInputMotor;       //MOTER
long pulseCountMotor = 0;   //MOTOR
long timeStepsMotor;        //MOTOR

//---------------------------------------------------------------------------------------------------------------------------
void setup () {
  Serial.begin(9600);

  pinMode(button, INPUT);       //COUNTER

  pinMode(pulsePin, OUTPUT);    //MOTOR
  pinMode(dirPin, OUTPUT);      //MOTOR
  pinMode(enablePin, OUTPUT);   //MOTOR
  digitalWrite(dirPin, dirSet); //MOTER

  pinMode(SDswitch, INPUT);     //SD
  pinMode(SDled, OUTPUT);       //SD

  SD.begin(53);
  pinMode(53, OUTPUT);

  pulseInputMotor = MStep * rotations;        //MOTOR: setting the number of rooms
  timeStepsMotor = 60000000 / (rpm * MStep);  //MOTOR: setting speed for rpm
}

//---------------------------------------------------------------------------------------------------------------------------
void loop () {
  unsigned long time = millis();        //LOOP TIME
  for (int i = 0; i < 1000; ++i) {      //LOOP TIME

    //-----------------------------------------------------------------
    //***Counter***
    val = digitalRead(button);
    if (val == HIGH) {
      currentState = 1;
    }
    else {
      currentState = 0;
    }

    if (currentState != previousState) {
      if (currentState == 1) {
        counter = counter + 1;
        Serial.print("real counter: ");
        Serial.println(counter);
        SDstage = 1;
      }
      previousState = currentState;
      delay(50);
    }

    //-----------------------------------------------------------------
    //***Motor***
    if (millis() > timePassed + timerSet) {   //MOTOR
      motor();
    }
    else {
      digitalWrite(enablePin, LOW);
    }
    //-----------------------------------------------------------------
    //***SD***
    switchState = digitalRead(SDswitch);
    if (switchState == 1) {
      digitalWrite(SDled, HIGH);
    }
    else {
      digitalWrite(SDled, LOW);
    }

    if (SDstage == 1 && switchState == 1) {
      SD_save();
      Serial.println("naar SD save");
      SDstage = 0;
    }
  }
  Serial.println(millis() - time);          //LOOP TIME: the time after 1000 loops
}

void motor() {
  //Serial.println(pulseInputMotor);
  pulseInputMotor = MStep * rotations;                    //@@@@MOTOR: setting the number of rooms

  if (pulseCountMotor <= pulseInputMotor)
  {
    digitalWrite(enablePin, HIGH);
    delayMicroseconds(6);                   //see DM422 specifications chapter 10
    digitalWrite(dirPin, dirSet);
    delayMicroseconds(6);                   //see DM422 specifications chapter 10
    digitalWrite(pulsePin, HIGH);
    delayMicroseconds(timeStepsMotor);
    digitalWrite(pulsePin, LOW);
    delayMicroseconds(6);                  //see DM422 specifications chapter 10
    pulseCountMotor++;                           //adding each course
  }
  if (pulseCountMotor > pulseInputMotor) {
    pulseCountMotor = 0;
    digitalWrite(enablePin, LOW);
    timePassed = millis();
  }
}

void SD_save() {
  Serial.println("SD save");
  myFile = SD.open("datalog.txt", O_CREAT | O_WRITE);
  if (myFile) {
    myFile.println(counter);
    myFile.close();
  }
}

The loop time is 0.028ms/rev. When the SD isnt there, then is the loop time 0.015ms/rev. When the motor turns the loop time is the same, 0.539ms/rev.

Does anyone have a idea how i can fix this? I am looking forward to the responds. :smiley:

This is the program that i use know:

Why

did

you

post

the

code

in

three

separate

windows?

Post ALL of your code in ONE piece.

I did not see a problem with 3 parts. But I adjusted it.

Do you also have an idea how i can solve this problem?

Every time you call SD_save(), you open the file, write to it, and close the file. That takes time. You can't expect that to happen in no time.

You also write to the serial port, very slowly. Pick THAT speed up.

You can speed up the save function by only opening the file once, and only closing it once.

You'll need some extra inputs to know when to open the file and when to close it.

Thank you, i will try this!!

But i still have a question. What is the reason that the total program gets slow when i use a SD. I can understand that it gets slow when it use the SD, but it is also when it is not using the SD.....

FeelGoodGirl:
Thank you, i will try this!!

But i still have a question. What is the reason that the total program gets slow when i use a SD. I can understand that it gets slow when it use the SD, but it is also when it is not using the SD.....

The code you posted uses the SD card. I can't comment on other code you haven't posted.