if and case statements work funny with SD.begin

So in my setup() I am starting my SD and then checking to see if it was started correctly. If it was started correctly I print "initialization done." on an LCD and if not started correctly I print to the LCD "initialization failed!". The problem is when I check whether SD.begin(10) returns true or false I can no longer print to the LCD.

My if statement is the following:

    if (!SD.begin(10)) {
        Serial.println("initialization failed!");
        lcd.setCursor(0, 0);
        lcd.print("initialization");
        lcd.setCursor(0, 1);
        lcd.print("failed!");
        delay(5000);
        digitalWrite(red_light,HIGH);
      }
      else{
        Serial.println("initialization done.");
        lcd.setCursor(0, 0);
        lcd.print("initialization");
        lcd.setCursor(0, 1);
        lcd.print("done.");
        delay(5000);
        digitalWrite(yellow_light,HIGH);
      }

The if statement works fine if I use some other parameters. But when I use SD.begin the lcd.print statements no longer work, and I have no idea what the problem is.

Please post the whole sketch.

#include <SD.h>
#include <Wire.h>
#include "R8025.h"
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins

File temperature_File;

const int temp_monitor1=0;
const int temp_monitor2=1;
const int yellow_light=9;
const int red_light=6;
const int good_button=8;
const int bad_button=7;
int temp1,temp2;
int good_sd_initialization =0;
/******* The error variable will be used to identify and resolve errors ******
******** Error = 0 Then there is nothing wrong                          ******
******** Error = 1 Then the SD card is missing/can not be read          ******
******** Error = 2 Can not connect to other xbeee(not sure how to do)   ******
******** Error = 3 Temperature sensors have problems                    ******/
int error=0;                

/**** Variables for the RTC ****/
R8025 RTC;
DateTime now;//used to get the time when we need to print it

/****variables for good button debounce*****/
int good_lastButtonState = LOW;   // the previous reading from the input pin
long good_lastDebounceTime = 0;  // the last time the output pin was toggled
long good_debounceDelay = 150;    // the debounce time; increase if the output flickers
int good_buttonState = 0;         // variable for reading the pushbutton status

/****variables for bad  button debounce*****/
int bad_lastButtonState = LOW;   // the previous reading from the input pin
long bad_lastDebounceTime = 0;  // the last time the output pin was toggled
long bad_debounceDelay = 150;    // the debounce time; increase if the output flickers
int bad_buttonState = 0;         // variable for reading the pushbutton status

/***  LCD   ****/
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup()
{
     /***Setting up all the pins***/
      Wire.begin();
      Serial.begin(19200);
      pinMode(yellow_light,OUTPUT);
      pinMode(red_light,OUTPUT);
      pinMode(good_button,INPUT);
      pinMode(bad_button, INPUT);
      pinMode(10, OUTPUT);
     /************************************/
      
      
      /***Checking if SD card is available***/
      lcd.begin(16, 2);
      lcd.setCursor(0, 0);
      Serial.print("Initializing SD card...");
      lcd.clear();
      lcd.print("Initializing SD ");
      lcd.setCursor(0,1);
      lcd.print("card ...........");
      delay(3000);
      lcd.clear();


    if (!SD.begin(10)) {
        Serial.println("initialization failed!");
        lcd.setCursor(0, 0);
        lcd.print("initialization");
        lcd.setCursor(0, 1);
        lcd.print("failed!");
        delay(5000);
        digitalWrite(red_light,HIGH);
      }
      else{
        Serial.println("initialization done.");
        lcd.setCursor(0, 0);
        lcd.print("initialization");
        lcd.setCursor(0, 1);
        lcd.print("done.");
        delay(5000);
        digitalWrite(yellow_light,HIGH);
      }
     /* switch(SD.begin(10)){
        case false:
          Serial.println("initialization failed!");
          lcd.setCursor(0, 0);
          lcd.print("initialization");
          lcd.setCursor(0, 1);
          lcd.print("failed!");
          delay(5000);
          digitalWrite(red_light,HIGH);
          break;
        case true:
          Serial.println("initialization done.");
          lcd.setCursor(0, 0);
          lcd.print("initialization");
          lcd.setCursor(0, 1);
          lcd.print("done.");
          delay(5000);
          digitalWrite(yellow_light,HIGH);
          break;
      }*/
      lcd.clear();
      lcd.setCursor(0, 0);
       
     /*** Setting up the RTC ***/
        RTC.begin();
        RTC.adjust(DateTime(__DATE__, __TIME__));  //sets the date and time to the time file was compiled based on computer clock
      //note no idea why  RTC.begin() is needed twice but without them it does not work  
     /************************************/ 
       
       
}
 
void loop()
{ 
//code to be added still
}
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

Why are you connecting the LCD to the SPI pins? Move it!

Sorry I am new too Android but which ones are SPI pins and why do I want to avoid using them?

Assuming an Uno...

sacredbandofthebes: Sorry I am new too Android but which ones are SPI pins and why do I want to avoid using them?

SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). http://arduino.cc/en/Main/ArduinoBoardUno

You can't use the SPI pins for your LCD because the SD-Card is a SPI device. So when you start using the SD-Card, communication with the LCD is no longer possible.

Hopefully you now see why you should post complete sketches, rather than just the bits you think are relevant.

noob here..wanting to jump on the band wagon here, real quick... to learn/understand a bit more about the SPI bus.

can you NOT have more than one device on the SPI bus at a time? I thought that was the point?

I have read tings like making sure the SS_pin (hardware) is set to output first..to allow more than one device to be on the same SPI bus.. but have different CS pins?

Is this specific to the SD device? I sorta ran into the same problem with having the uSD card and DAC on the same SPI bus (sharing the same, 11, 12, 13 pins..but each having different/separate CS pins, 10 & 9 respectively)

but the WaveHC library I had planned on using doesnt seem to work that way.. and use a 'bit bang' method to keep reading info form the card..and pass to the DAC..

so I wanted to jump in here and learn a bit more to understand how it works..or should work..

thanks :)

can you NOT have more than one device on the SPI bus at a time? I thought that was the point?

You can have more than one SPI device on the SPI bus. The LCD is not an SPI device. It doesn't belong on the SPI bus.

I have read tings like making sure the SS_pin (hardware) is set to output first..to allow more than one device to be on the same SPI bus.. but have different CS pins?

Yes, that is needed.

one another quick question what is the best way to wire the LCD then?

What I was trying is taking what was at pin 11 and 12 and moving them to 6 and 7 but when I do this it does not seem to work.

This part of the code determines what pins your lcd must be connected to.

/***  LCD   ****/
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

The wire that was on 12 goes to 7 and 11 goes to 6. Change to:

/***  LCD   ****/
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

When I switched the pins I did change the code to say LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
however it still does not work

Does the LCD work by itself when you make that change?

Well I got the lcd working by using pins 8 and 9 and calling LiquidCrystal lcd(8, 9, 5, 4, 3, 2);

by using pins 8 and 9 will I have to worry about the SPI?

Well, look at what pins are used by SPI. Hint: I already gave you the pins AND where to find them yourself.

As noted.. Nick already posted this for you: :)

SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK).

http://arduino.cc/en/Main/ArduinoBoardUno

those 'are' the SPI lines used.. so if you are not using those then you dont have to worry.