I've devised a basic setup running an SD card shield to retrieve a number stored in a textfile on the SD card. However, the card fails to initialize no matter what transpires, and the number is never retrieved.
The code is as follows:-
#include<SD.h>
const int CS_pin=10;
const int power_pin=8;
float id=0.0;
void setup()
{
Serial.begin(9600);
Serial.println("Commencing setup!");
pinMode(CS_pin,OUTPUT);
pinMode(power_pin,OUTPUT);
digitalWrite(power_pin,HIGH);
if(!SD.begin(CS_pin))
{
Serial.println("Failed to initialize.");
return;
}
else
{
Serial.println("Initialization successful!");
}
File textfile=SD.open("id.txt");
if(textfile)
{
float decade=pow(10,(textfile.available()-1));
while(textfile.available()>0)
{
float temp=(textfile.read()-'0');
id=id+temp*decade;
decade=decade/10;
}
Serial.print("ID procured. ID is: ");
Serial.println(id);
}
else
{
Serial.println("Failed to read ID value.");
return;
}
}
void loop()
{
}
The code returns a false value at the !SD.begin() point, and the card fails to initialize.
Why is it not initializing? Are there any errors in my code?