SD check for life

Good morning,

It’s a good day to learn new stuff. I am currently working on an SD card that is used as datalogger. The idea is as follows. You have a button, 2 leds, a switch and of course an SD card reader.

If the button is pressed, a counter will keep the number of pressures. The time is also saved when the button is pushed. If the data is written, a led will burn.
There is a condition for writing down the data. That is, the switch is ON. The idea behind this is that if the SD card is ready to write and the LED is out, you can switch the switch and safely take the SD card.
This section works completely.

My problem / question is the following. The idea is that you can stop the SD card without having to reset the system. This is not a problem now. I really want to write a part what checked if the switch is ON or the SD card is present and works.

if (digitalRead(SDswich) == 1 && SDtest == 0) {
    SD.begin(53);
    if (!SD.begin(53)) {
      Serial.println("initialization failed!");
      return;
    }
    else {
      Serial.println("initialization completed!");
      SDtest = 1;
    }
  }

This last part doesn’t work. When you print it, it will print "initialization failed!".

This is the total code:

/*
  Connect the 5V pin to the 5V pin on the Arduino
  Connect the GND pin to the GND pin on the Arduino
  Connect CLK to pin 52 (SCK)
  Connect DO to pin 50 (MISO)
  Connect DI to pin 51 (MOSI)
  Connect CS to pin 53 (/SS)
*/
#include <SPI.h>
#include <SD.h>
#include <Controllino.h>

File myFile;

#define SDswich CONTROLLINO_A8
#define SDled CONTROLLINO_D7
#define button CONTROLLINO_A7
#define SDcheck CONTROLLINO_D9

int SDstage = 0;      //to controle the SD
int SDtest = 0;

int val = 0;
int currentState = 0;
int previousState = 0;
long counter = 0;

int dag;
int maand;
int jaar;
int uur;
int minuut;
int seconde;

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

  pinMode(SDswich, INPUT);
  pinMode(SDled, OUTPUT);
  pinMode(button, INPUT);

  Controllino_RTC_init(0);
  //Controllino_SetTimeDate(26, 3, 4, 2017, 10, 4, 25); //set a new start values to RTC chip, when you run the program it needs to bij '//'

  pinMode(53, OUTPUT);
  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    return;
  }
}

void opslaan() {
  //SD.begin(53);
  digitalWrite(SDled, HIGH);
  myFile = SD.open("datalog.txt", O_CREAT | O_WRITE);
  if (myFile) {
    myFile.print("counter: ");
    myFile.print(counter);
    myFile.print("\t");
    myFile.print(dag);
    myFile.print("/");
    myFile.print(maand);
    myFile.print("/");
    myFile.print(jaar);
    myFile.print("\t");
    myFile.print(uur);
    myFile.print(":");
    myFile.print(minuut);
    myFile.print(":");
    myFile.println(seconde);
    myFile.close();
  }
}

void loop() {
  dag = Controllino_GetDay();
  maand = Controllino_GetMonth();
  jaar = Controllino_GetYear();
  uur = Controllino_GetHour();
  minuut = Controllino_GetMinute();
  seconde = Controllino_GetSecond();

  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);
  }

  if (digitalRead(SDswich) == 1 && SDtest == 0) {
    SD.begin(53);
    if (!SD.begin(53)) {
      Serial.println("initialization failed!");
      return;
    }
    else {
      Serial.println("initialization completed!");
      SDtest = 1;
    }
  }

  if (SDstage == 1 && SDtest == 1) {
    opslaan();
    SDstage = 0;
  }
  else {
    digitalWrite(SDled, LOW);
    SDtest = 0;
  }
}

You have a button, 2 leds, a switch and of course an SD card reader.

Make up your mind. You have switches or you have shirts with buttons sewn on. Which is it?

PaulS:
Make up your mind. You have switches or you have shirts with buttons sewn on. Which is it?

i put a photo in the attachment. I mean a push button.

FeelGoodGirl:

    SD.begin(53);

if (!SD.begin(53)) {

I don't have any experience with the SD library but that code seems wrong to me. The line:

    if (!SD.begin(53)) {

calls SD.begin() so you are calling it twice in a row. I think you should only call it once. You might find the code easier to understand if you reverse the logic:

    if (SD.begin(53)) {
      Serial.println("initialization completed!");
      SDtest = 1;
    }
    else {
      Serial.println("initialization failed!");
      return;
    }

thanks @pert for your feedback.

When i was testing the code (without the check) I found out that when the card was removed en pushed back in you need "SD.begin(53)" again. That why i did this also in that part.

I looked for programs with this same idea, but I coudnt find any... :frowning:

I tryed some new think, without succes....
I changed the code so that different values should be correct before it's going to see if the SD card is good. But if I still do not get it, I can not control the SD card in the program, so outside of the setup.

can someone help me please?

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

File myFile;

#define SDswich CONTROLLINO_A8
#define SDled CONTROLLINO_D7
#define button CONTROLLINO_A7
#define SDcheck CONTROLLINO_D9

int SDstage = 0;      //to controle the SD
int SDtest = 0;

int val = 0;
int currentState = 0;
int previousState = 0;
long counter = 0;

int switchstate;

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

  pinMode(SDswich, INPUT);
  pinMode(SDled, OUTPUT);
  pinMode(button, INPUT);

  pinMode(53, OUTPUT);
  if (!SD.begin(53)) {
    Serial.println("initialization failed! setup");
    return;
  }
  else {
    Serial.println("initialization completed! setup");
    SDtest = 1;
  }
}

void SDsave() {
  digitalWrite(SDled, HIGH);
  myFile = SD.open("datalog.txt", O_CREAT | O_WRITE);
  if (myFile) {
    myFile.print("counter: ");
    myFile.print(counter);
  }
}

void test() {
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    SDtest = 0;
  }
  else {
    Serial.println("initialization completed!");
    SDtest = 1;
  }
}

void loop() {
  val = digitalRead(button);
  if (val == HIGH) {
    currentState = 1;
  }
  else {
    currentState = 0;
  }
  if (currentState != previousState) {
    if (currentState == 1) {
      counter = counter + 1;
      Serial.print("Counter: ");
      Serial.println(counter);
      SDstage = 1;
    }
    previousState = currentState;
    delay(50);
  }

  if (SDstage == 1 && SDtest == 1) {
    SDsave();
    SDstage = 0;
  }
  
  switchstate = digitalRead(SDswich);
  if (switchstate == 1 && SDtest == 0) {
    test();
  }

  if (switchstate == 0){
    SDtest = 0;
  }
}