Setup not doing all tasks

So I have a small sketch to I took from a bigger one and for some reason its only working the first part.

I run the sketch in an arduino uno with SD card shield v4.1 by seeed studio.

The sketch checks if the sd card is able to be used. If it is not able to use the sd card it shows a message. It it is able to use the card it shows a different message and I want it to make led 13 blink.

All works but led 13 blink is not working and I can see the messages in the serial monitor perfectly but I cant understand why after showing the message it does not make the led turn on and off.

#include <SD.h>

void setup()
{
  int chipSelect = 4;
  int ledpin = 13;
  pinMode(ledpin, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);


  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
    
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  
  delay(1000);
  digitalWrite(ledpin, HIGH);	
  delay(5000);		        
  digitalWrite(ledpin, LOW);	
  delay(1000);
  digitalWrite(ledpin, HIGH);	
  delay(5000);		        
  digitalWrite(ledpin, LOW);	
  delay(1000);
}

void loop()
{}

Thanks

My bet is that the SD card code is not working correctly. I had all kinds of problems with the SD code until I got a different SD card. Personal preference, but I’d move the definition of the LED pin outside of any function and make it a global:

// Use either this...
#define LEDPIN  13

// ...or this, not both.
const int LEDPIN = 13;

To diagnose your problem comment out the if statement block:

/*
if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
*/

and see if the LED behaves as expected. If so, the SD card code is giving you problems. Also, having a return statement in setup() is a little strange, as it sends control back to main.cpp. You could replace the return with exit(0), but that’s weird, too. Perhaps some other reader will have a more graceful way of doing this.

I cant test it at the moment but will try it as soon as possible. Thanks

If you discover a show-stopper during setup(), then do:

  while (true) {}

Then nothing else will happen.

econjack:
and see if the LED behaves as expected. If so, the SD card code is giving you problems. Also, having a return statement in setup() is a little strange, as it sends control back to main.cpp. You could replace the return with exit(0), but that’s weird, too. Perhaps some other reader will have a more graceful way of doing this.

I tested first #define, but for some reason PinMode doesnt recognice define statements. (Edit:this sentence can be misleading. As UKHeliBob pointed out the sentence is not true. What happens is that when I open SPI bus I can’t use the led, but PinMode can be used with define statements.Read the full thread to understand the problem better.)

Then tested const int. The program works initializes the card but the led doesnt work.

I removed the part of the code suggested and the led blinks perfectly.

I tried to add an else after the if so that the led blinks like this

#include <SD.h>
const int ledpin = 13;
const int chipSelect = 4;

void setup()
{

  pinMode(ledpin, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(4, OUTPUT);
  


  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
    
  // see if the card is present and can be initialized:
  
  
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  else
  {
  
  Serial.println("card initialized.");
  
  delay(1000);
  digitalWrite(ledpin, HIGH); 
  delay(5000);        
  digitalWrite(ledpin, LOW); 
  delay(1000);
  digitalWrite(ledpin, HIGH); 
  delay(5000);        
  digitalWrite(ledpin, LOW); 
  delay(1000);
  }
}

void loop()
{}

But still doesnt work.

I’m using the code from the arduino example SD ReadWrite sketch.

If you are using an Uno, you can't blink the D13 LED if you are using the SPI bus. The SD cards use the SPI bus.

SurferTim:
If you are using an Uno, you can't blink the D13 LED if you are using the SPI bus. The SD cards use the SPI bus.

Ok that is the answer, thanks.

How can I know what can be used and what cant while I use the SPI bus?

The SPI reference covers which pins on the Arduino models are used for SPI.

On the Uno, it is D10 to D13.

for some reason PinMode doesnt recognice define statements.

I know that you have found the answer to your problem, but for the sake of anyone reading this thread and seeing what you said above, I think that I should point out that it is not true.

UKHeliBob:
I know that you have found the answer to your problem, but for the sake of anyone reading this thread and seeing what you said above, I think that I should point out that it is not true.

Added an edit to that sentence to clarify and avoid misleading. Thanks