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.