On some systems, such as 3D printers, memory cards are used to save print files. Therefore, it is very important to check the memory card connection at the beginning and during the printing process.
Therefore, in case of any connection or card failure, the system must be able to detect a malfunction and inform the user on the system LCD screen.
In addition to the 3D printer, this method can be used for any system or device that uses a memory card.
Therefore, we present a circuit to test the solution, as shown in Figure 1.
Next, we will create and explain an algorithm to detect the failure or lack of connection of the memory card during the process of executing the system.
The logic of building the code is very simple. We need to check at the beginning (void setup function) and during code execution (inside the loop function) if the card is connected.
If the card is not detected, a message must be entered on the LCD screen to inform the user, according to is presented in Figure 2.
In this way, the user will insert the card and the system will re-operate again and present the message "Card Connected!", as is shown in Figure 3.
After the system verifies the status of the SD Card, the system will wait for the user to press the button and initiate the storage processing of 10 values of ADC in the SD Card. At this moment, it will show the message as is presented in Figure 4.
After the user press the button, the system will storage 10 units of ADC values in the SD Card and present the screen with the message: "Storing data..." and "Finishing Successfully", to inform the end of the storage process. These messages are presented below.
After all these processes, the system back to the beginning of the loop and initiate all logic again.
Hereafter, we will present and discuss the code developed to solve the problem.
Programming LogicAccording to the code below, the libraries of the used elements were inserted: LCD display, SD Card and declared all variables of the code.
#include <SD.h>
#include <SPI.h>
#include <LiquidCrystal.h>
File myFile;
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#define AnalogPin A0
int pinoSS = 10; // Pin 53 para Mega / Pin 10 para UNO
int DigitalValue = 0;
byte samples = 0;
bool SDCardTest = 0, ControlState = 0, LCDControl = 0;
After this code block, we will present the void loop function below. As is possible to see, the Display LCD and Serial was initialized. After, was performed the first test to verify if our SD Card it is connected or it is failing.
void setup()
{
Serial.begin(9600); // Define BaundRate
lcd.begin(16, 2);
pinMode(pinoSS, OUTPUT); // Declara pinoSS como saída
delay(500);
lcd.clear();
do
{
if (SD.begin())
{ // Inicializa o SD Card
lcd.setCursor(6,0);
lcd.print("Card");
lcd.setCursor(3,1);
lcd.print("Connected!");
delay(2000);
SDCardTest = 1;
}
else
{
lcd.clear();
Serial.println("imprimindo segunda mensagem de erro.");
lcd.setCursor(1,0);
lcd.print("Failed or Card");
lcd.setCursor(2,1);
lcd.print("disconnected");
SDCardTest = 0;
}
}while(SDCardTest == 0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Press the button");
lcd.setCursor(1,1);
lcd.print("To store data");
}
There is a do-while loop to verify the SD Card. In this process, the system does an initialization of the SD Card. If the initialization process occurs normally, so the SD Card it not with the problem. But, a case occurs anyone problem, the system will initialize the SD card.
This way will be presented the message "Failed or Card disconnected" in the Display LCD and the variable SDCardTest will receive value 0. This variable will be used to control the loop do while.
After solving the problem and connect the SD card again, it will be shown the message "Press the button to store data".
After here, the commands in the void loop function will be executed. The code of the void loop function is presented below.
void loop()
{
bool Button = digitalRead(8);
if(LCDControl == 0)
{
lcd.setCursor(0,0);
lcd.print("Press the button");
lcd.setCursor(1,1);
lcd.print("To store data");
LCDControl = 1;
}
if(Button == 0 && ControlState == 1)
{
ControlState = 0;
}
if(Button == 1 && ControlState == 0)
{
myFile = SD.open("silicioslab.txt", FILE_WRITE); // Create/Open File the txt
delay(500);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Storing");
lcd.setCursor(4,1);
lcd.print("data...");
do
{
DigitalValue = analogRead(AnalogPin);
myFile.println(DigitalValue);
delay(400);
samples++;
}while(samples < 10);
samples = 0;
lcd.clear();
lcd.setCursor(4,0);
lcd.print("Finished");
lcd.setCursor(2,1);
lcd.print("Successfully");
delay(2000);
myFile.close(); //Close file
LCDControl = 0;
ControlState = 0;
}
do
{
if (SD.begin())
{
SDCardTest = 1;
}
else
{
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Failed or Card");
lcd.setCursor(2,1);
lcd.print("disconnected");
SDCardTest = 0;
LCDControl = 0;
Serial.println("Verificando problema...");
}
}while(SDCardTest == 0);
}
In the void loop function, the state of the button will be read to verify if our user pressed the button. After the button to be read, there is the following condition:
if(LCDControl == 0)
{
lcd.setCursor(0,0);
lcd.print("Press the button");
lcd.setCursor(1,1);
lcd.print("To store data");
LCDControl = 1;
}
This condition is used to allow the "Press the button to store the data" message to be displayed only once. This prevents text from being displayed many times and can have a strange effect on the screen.
After this, if the button is pressed the file will open and the 10 values will be saved in the SD Card and following appear the message "Finishing Successfully", to inform the finish of the process.
And finally, the file will be closed. So, more one time the SD Card will be verified by the system.
AcknowledgmentThanks to the PCBWay for support the our YouTube Channel and produce and assembly PCBs with better quality.
The Silícios Lab thanks UTSOURCE to offer the electronic components.
Comments