Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.88 KB | None | 0 0
  1. // Libraries to drive the SD Card. ******************************
  2. #include <SD.h>
  3.  
  4. /* The SD card communicates over pins for the Arduino Mega:
  5.  MOSI - Pin 51
  6.  MISO - Pin 50
  7.  Clock - Pin 52
  8.  Chip Select - pin 53
  9.  
  10.  Yellow LED goes on during a read operation.
  11.  Red LED goes on during a write operation.
  12.  
  13.  Use '(char *)filename.c_str()' to pass a string variable to concatinate the file name.
  14.  Use 'filename.c_str()' to pass a variable file name to the FILE.open(fn,option) command.
  15.  Make sure to disconnect the power to the SD card shield prior to removing
  16.  the chip.
  17.  */
  18.  
  19.  
  20. int baudrate = 9600; //set the baud rate.
  21. int buzzerPin = 2; //set buzzer pin.
  22. const int redLED = 8; //set yellow LED.
  23. const int yellowLED = 7; //set red LED.
  24. int yellowLEDstate = LOW; //save the red LED state.
  25. int redLEDstate = LOW; //save the yellow LED state.
  26. String filename; //Store file name.
  27. boolean isDirectory = false; //Set for directory flag
  28. boolean isRemoveDirectory = false; //Set delete (file/director) flag.
  29. boolean isRemoveFile = false;
  30. boolean isChangeDirectory = false; //Set change directory flag.
  31. boolean isReadFromFile = false;
  32. boolean isWriteToFile = false;
  33. String root = "/";
  34. String tvRemoteOption;
  35. String dataInput;
  36. int var; //Case statement variable.
  37. int countReadFromFile = 0; //Test.
  38. int countWriteToFile =0;//Test.
  39. int countRemoveDirectory =0;//Test.
  40. int countRemoveFile =0;//Test.
  41. int countMakeDirectory = 0;//Test.
  42. int countMakeFile = 0;//Test.
  43. int countChangeDirectory = 0;//Test.
  44. int countListDirectory = 0;//Test.
  45. int countMultipleFilesOpen = 0; //Test.
  46. int loopCounter = 0;//Test.
  47. int rootCounter = 1;//test.
  48. int CSpin = 53; //Chip Select pin.
  49. File myFile, myFile2, myFile3,myFile4, //Define files to open.
  50. myFile5,myFile6,myFile7,myFile8,myFile9,
  51. myFile10,myFile11,myFile12,myFile13,myFile14,
  52. myFile15,myFile16,myFile17,myFile18,myFile19,
  53. myFile20;
  54. File myFiles[20] = {myFile, myFile2, myFile3,myFile4, //Create an array of files to open.
  55. myFile5,myFile6,myFile7,myFile8,myFile9,
  56. myFile10,myFile11,myFile12,myFile13,myFile14,
  57. myFile15,myFile16,myFile17,myFile18,myFile19,
  58. myFile20};
  59.  
  60.  
  61.  
  62.  
  63.  
  64. //setup *************************************************************************
  65. void setup()
  66. {
  67.   Serial.begin(baudrate); // Start serial communication.
  68.  // pinMode(10,OUTPUT);//Required by the SD library?
  69.   pinMode(CSpin,OUTPUT);//Required by the SD library?
  70.  
  71.  
  72.   pinMode(redLED, OUTPUT); //Setup yellow LED.
  73.   pinMode(yellowLED, OUTPUT);  //Setup red LED.
  74.   pinMode(buzzerPin, OUTPUT); //Setup buzzer.
  75.  
  76.   Serial.println(F("************************************************************"));
  77.   Serial.println(F("************************************************************"));
  78.   Serial.println();
  79.  
  80.   Serial.println(F("Initialize the SD card..."));
  81.   delay(2000);
  82.  
  83.   if (!SD.begin(CSpin)){  //Test for initialized.
  84.     ErrorHandler(9);
  85.   }//End if.
  86.  
  87.  
  88.   Serial.println(F("Initialization Complete."));
  89.   tone(buzzerPin, 440, 10);
  90.   delay(50);
  91.   noTone(buzzerPin); // turn off tone function.
  92.  
  93.   root = "/"; //Set the root directory.
  94.  
  95.   digitalWrite(yellowLED, LOW); //Initialize the LED to OFF.
  96.   digitalWrite(redLED,LOW); //Initialize the LED to OFF.
  97.  
  98.  
  99.  
  100. }//End setup.
  101.  
  102.  
  103. //loop *********************************************************************************
  104. void loop()
  105. {
  106. loopCounter = loopCounter + 1;  //Test.
  107.  
  108.  Serial.println();
  109.  Serial.print(F("Loop Counter--> "));
  110.  Serial.println(loopCounter);
  111.  
  112.  var= random(1,10); //Test random CASE option.
  113.  filename = ""; //test: reset filename. line 200
  114.  
  115.  
  116.   var = 1; //Choose specific CASE option to test.
  117.  
  118. switch(var){
  119.  
  120. case 1:  
  121.   Serial.println(F("List File..."));
  122.   ListDirectory();//Call function.
  123.   countListDirectory = countListDirectory + 1;
  124. break;
  125.  
  126. case 2:
  127.  Serial.println(F("Change Directory..."));
  128.   ChangeDirectory(); //Call function.
  129.   countChangeDirectory = countChangeDirectory + 1;
  130. break;
  131.  
  132. case 3:
  133.    Serial.println(F("Make File..."));
  134.   MakeFile(); //Call function.
  135.   countMakeFile = countMakeFile + 1;
  136. break;
  137.  
  138. case 4:  
  139.    Serial.println(F("Make Directory..."));
  140.   MakeDirectory(); //Call function.
  141.   countMakeDirectory = countMakeDirectory + 1;
  142. break;
  143.  
  144. case 5:  
  145.  Serial.println(F("Remove File..."));
  146.   RemoveFile(); //Call function.
  147.   countRemoveFile = countRemoveFile + 1;
  148. break;
  149.  
  150. case 6:
  151.  Serial.println(F("Remove Directory..."));
  152.   RemoveDirectory(); //Call function.
  153.   countRemoveDirectory = countRemoveDirectory + 1;
  154. break;
  155.  
  156. case 7:  
  157.   Serial.println(F("Write To File..."));
  158.   WriteToFile(); //Call function.
  159.   countWriteToFile = countWriteToFile + 1;
  160. break;
  161.  
  162. case 8:
  163.  Serial.println(F("Read From File..."));
  164.   ReadFromFile(); //Call function.
  165.   countReadFromFile = countReadFromFile + 1;
  166. break;
  167.  
  168. case 9:
  169.  Serial.println(F("Multiple Files Opened..."));
  170.  MultipleFilesOpen();
  171.  countMultipleFilesOpen = countMultipleFilesOpen + 1;
  172. break;
  173.  
  174.  
  175.  } //End case statement.
  176.  
  177.  
  178.  
  179. if( loopCounter >= 1){
  180.  
  181. Serial.println(F("****************************************"));
  182.  
  183. Serial.print(F("Total List Directory..."));
  184. Serial.println(countListDirectory);
  185.  
  186. Serial.print(F("Total Change Directory..."));
  187. Serial.println(countChangeDirectory);
  188.  
  189. Serial.print(F("Total Make File..."));
  190. Serial.println(countMakeFile);
  191.  
  192. Serial.print(F("Total Make Directory..."));
  193. Serial.println(countMakeDirectory);
  194.  
  195. Serial.print(F("Total Remove File..."));
  196. Serial.println(countRemoveFile);
  197.  
  198. Serial.print(F("Total Remove Directory..."));
  199. Serial.println(countRemoveDirectory );
  200.  
  201. Serial.print(F("Total Write To File..."));
  202. Serial.println(countWriteToFile);
  203.  
  204. Serial.print(F("Total Read From File..."));
  205. Serial.println(countReadFromFile);
  206.  
  207. Serial.print(F("Total Multiple Files Opened..."));
  208. Serial.println(countMultipleFilesOpen);
  209.  
  210. //ReadFromFile(); //Call function.
  211.  
  212. while(true); //Test.Stop program.
  213. } //End if.
  214.  
  215. } //End loop.
  216.  
  217.  
  218.  
  219. //WriteToFile  ****************************************************
  220. //Note: 2 files are open simultaneously.
  221.  void WriteToFile(){
  222.    redLEDstate = flashLED(redLED, redLEDstate); //Turn LED on.
  223.    isWriteToFile = true;
  224.  
  225.    Serial.println(F("(1)Write to the file...")); //debug.
  226.  
  227.  
  228.    
  229.     myFiles[0] = SD.open("data1.txt", FILE_WRITE); //Open data file.
  230.     delay(200);//Wait for file to open.
  231.     if(!SD.exists("data1.txt")){ //Check if file exists.
  232.     filename = "data1.txt";
  233.     ErrorHandler(4);}
  234.    
  235.     myFiles[1] = SD.open("test.txt",FILE_READ); //Open input file.
  236.     delay(200);//Wait for file to open.
  237.     if(!SD.exists("test.txt")){ //Check if file exists.
  238.     filename = "test.txt";
  239.     ErrorHandler(4);}
  240.  
  241.  
  242.  /*
  243.  //This code resets the data1.txt output file. **********************************
  244.   myFiles[1] = SD.open("data1.txt", FILE_WRITE); //Select file to clear.
  245.   delay(200);//Wait for file to open.
  246.  // SD.remove("datalog.txt"); //Must use (char *)filename.c_str() to work.
  247.  // delay(200); //Wait for SD card to complete.
  248.   myFiles[1].seek(0); //reset to the beginning of the file each loop.
  249.   for(int i = 1; i < 7; i = i + 1){
  250.   myFiles[1].print("          "); //Initialize the file datalog.txt with 10 blanks.
  251.  }//end for loop.
  252.   myFiles[1].close(); //Write the file to the SD card.
  253. //*******************************************************************************
  254. */    
  255.    
  256.     myFiles[1].seek(10);//set input file position to be read at startup.
  257.     for (int i = 1; i < 11; i++){ //Number of characters to read.
  258.     dataInput += (char) myFiles[1].read(); //Read each character and concatinate to data string.
  259.     }//end for.
  260.    
  261.  //   int filePosition = random( 0, myFile.size() );
  262.  
  263.     tvRemoteOption = "1POWER"; //Set veriable.
  264.  
  265.     int x = 0; //use to index the seek() option on the SD Card.
  266.  
  267.  
  268.     myFiles[0].seek(x);
  269.     myFiles[0].print(tvRemoteOption.c_str()); //Pass a variable to save to the SD Card.
  270.  
  271.     myFiles[0].seek(x+10);
  272.     myFiles[0].print(dataInput); //Write data from file 2 to file 1 on SD card.
  273.     dataInput = "";//Reset dataInput string for next use.
  274.    
  275.      myFiles[0].seek(x+20);
  276.      myFiles[0].print("3CHAN UP");    
  277.    
  278.      myFiles[0].seek(x+30);
  279.      myFiles[0].print("4CHAN DWN");
  280.    
  281.     delay(200);
  282.    
  283.     myFiles[0].close(); //Close the file and save the data.
  284.     delay(200); //Wait for file to close.
  285.     myFiles[1].close(); //Close the file and save the data.
  286.     delay(200); //Wait for file to close.
  287.    
  288.    
  289.   isWriteToFile = false;
  290.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED off.
  291. } //end WriteToFile function.
  292.  
  293.  
  294. //MultipleFilesOpen *************************************************
  295. void MultipleFilesOpen()
  296.  {  
  297.      redLEDstate = flashLED(redLED, redLEDstate); //Turn LED on
  298.    
  299.     Serial.println(F("(1)Multiple files opened...")); //debug.
  300.    
  301.    
  302.     myFiles[0] = SD.open("test.txt", FILE_WRITE); //Open data file.
  303.     delay(200);//Wait for file to open.
  304.  
  305.    
  306. //Use an array of myFiles to open and name each text file to write to. *****************  
  307.     for( int y = 1; y < 20; y ++){
  308. //Open the file to write to.Converts int to string.Concatinate to create the text filename.
  309.      myFiles[y] = SD.open(("data" + String(y) + ".txt").c_str() , FILE_WRITE);
  310.    
  311.     } //End for.
  312.  //************************************************************************************    
  313.    
  314.  
  315.  
  316.    for (int x = 1; x < 20; x++){ //Exclude test.txt at position 0.
  317.      myFiles[0].seek( random(0,myFiles[0].size() - 100 ) );//Set read test.txt position.
  318.      for ( int i =1; i < 11; i++){  //Read 10 characters from test.txt.
  319.        dataInput += (char) myFiles[0].read(); //Read the characters from test.txt.
  320.      } //End for.
  321.      myFiles[x].print(dataInput.c_str()); //Write to the open file.
  322.      dataInput = ""; //Reset the input data.
  323.    }//End For.
  324.  
  325.  
  326. //   int filePosition = random( 0, myFiles[x].size() );
  327.  
  328. //    myFiles2.seek(13);//set input file offset position to be read.  
  329. //    myFiles[0].seek(140);//set input file offset position to be read.
  330. //    for (int i = 1; i < 11; i++){ //Number of characters to read.
  331.   //  dataInput += (char) myFile2.read(); //Read each character and concatinate to data string.
  332. //  dataInput += (char) myFiles[0].read(); //Read each character and concatinate to data string.
  333. //    }//end for.
  334.    
  335. /*  
  336. //Create data to write to file. **************************
  337.     tvRemoteOption = "Multiple ("; //Set veriable.
  338.  
  339.     myFiles[1].seek(0);
  340.     myFiles[1].print(tvRemoteOption.c_str()); //Pass a variable to save to the SD Card.
  341.  
  342.     myFiles[1].seek(10);
  343.     myFiles[1].print(dataInput.c_str()); //Write data from file 2 to file 1 on SD card.
  344.     dataInput = "";//reset dataInput.
  345.  
  346.     myFiles[1].seek(20);
  347.     myFiles[1].print(")files are");    
  348.    
  349.      myFiles[1].seek(30);
  350.      myFiles[1].print(" working.");
  351.    
  352.     delay(200);
  353.  ********************************************************/
  354.  
  355.  
  356. //Close all the open files. *****************************************************
  357.   for (int x = 0; x < 20; x++){  
  358.     myFiles[x].close(); //Close the file and save the data.
  359.     delay(200); //Wait for file to close.
  360.   } //Close all the files.
  361.  
  362. //**********************************************************************************  
  363.  
  364.  
  365.  
  366.    
  367.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED off.  
  368. }//End MultipleFilesOpen.
  369.  
  370.  
  371.  
  372. //ReadFromFile ***************************************************
  373. void ReadFromFile(){
  374.   yellowLEDstate = flashLED(yellowLED, yellowLEDstate); //Turn LED on.
  375.   isReadFromFile = true;
  376.  
  377.    Serial.println(F("(1)Read from the file...")); //debug.
  378.  
  379.  
  380.     myFiles[0] = SD.open("data1.txt", FILE_READ);
  381.     delay(200);//Wait for file to open.
  382.     if(!SD.exists("data1.txt")){ //Check if file exists.
  383.     filename = "data1.txt";
  384.     ErrorHandler(4);}
  385.  
  386.  
  387.     while (myFiles[0].available()) { //Read until EOF.
  388.     Serial.write(myFiles[0].read()); //Read data from SD Card and write the file contents to the Serial Monitor.
  389.     } //End while
  390.     myFiles[0].close(); //Close the file and save the data.
  391.     delay(200); //Wait for file to close.
  392.  
  393.    Serial.println();
  394.    
  395.    isReadFromFile = false;
  396.    yellowLEDstate = flashLED(yellowLED, yellowLEDstate); //Turn LED off.
  397.  
  398.   } //end ReadFromFile function.
  399.  
  400.  
  401.  
  402.  
  403. //ChangeDirectory ****************************************************
  404. //The ChangeDirectory function is the only function that modifies the
  405. //'root' variable.
  406. //********************************************************************
  407. void ChangeDirectory(){
  408.   isDirectory = true; //Set directory flag.
  409.   isChangeDirectory = true; //Debug.Set change directory flag.
  410.  
  411.   Serial.println(F("(1)Change Directory to...")); //debug.
  412.  
  413.  //   while (Serial.available() < 1) {} //Wait for serial montior input.
  414. //    filename = Serial.readString();
  415.  
  416.   //generate a random file name.Remove.
  417.   int i = random(65,90);//test.
  418.   filename = String((char) i);//test.
  419.  
  420.  //   if(filename == "/"){   //Must enter "/" to return to root directory.
  421.  //     root = "/";
  422.  //   } //end if.
  423.   if(rootCounter <= 2){
  424.       root = root + filename.c_str() + "/"; //Add in '(char *)' ????? Build root directory.
  425.       rootCounter = rootCounter + 1;//test.
  426.       Serial.print(F("(2)ROOT: "));//test.
  427.       Serial.println(root);//test.
  428.   } //end if.
  429.     else {
  430.       root = "/"; //test.
  431.       rootCounter = 1; //test.
  432.       Serial.print(F("(2)ROOT: "));//test.
  433.       Serial.println(root);//test.  
  434.     } //End else.
  435.      
  436.   isDirectory = false; //Set directory flag.
  437.   isChangeDirectory = false; //Debug.Set change directory flag.
  438.  
  439. } //End ChangeDirectory function.
  440.  
  441.  
  442. //MakeDirectory **************************************************
  443. void MakeDirectory(){
  444.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED on.
  445.   isDirectory = true; //This may need to be moved to the filename function.
  446.  
  447.   Serial.println(F("(1)Make the directory...")); //debug.
  448.  
  449.    //generate a random file name.Remove.
  450.   int i = random(65,90);//test.
  451.   filename = String((char) i);//test.
  452.   Serial.print(F("(2)Directory: "));//test.
  453.  
  454.  
  455.  // while (Serial.available() < 1) {} //Wait for serial montior input.
  456.    filename = root + filename;
  457.    Serial.println(filename);//test.
  458.  
  459.   SD.mkdir((char *)filename.c_str());
  460.   delay(500); //Wait for SD card to complete.
  461.   myFile.close(); //Write the file to the SD card.
  462.   delay(500); //Wait for SD card to complete.
  463.  
  464.   isDirectory = false; //reset the directory flag.
  465.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED off.
  466.  
  467. } //End MakeDirectory function.
  468.  
  469.  
  470.  
  471.  
  472. //MakeFile  **********************************************************
  473.  void MakeFile(){
  474.    redLEDstate = flashLED(redLED, redLEDstate); //Turn LED on.
  475.    Serial.println(F("(1)Make a file...")); //debug.
  476.    
  477. //   EnterFileName();//Check the file name.
  478.  
  479.   //generate a random file name.Remove.
  480.   int i = random(65,90);//test.Create a chara A to Z.
  481.   filename = root + String((char) i) + ".txt";//test.
  482.   Serial.print(F("(2)"));//test.
  483.   Serial.println(filename);//test.
  484.  
  485.    myFiles[0] = SD.open(filename.c_str(), FILE_WRITE);
  486.    delay(200);//Wait for file to open.
  487.    myFiles[0].close(); //Close the file and save it to the SD card.
  488.    delay(200);//Wait for file to close.
  489.  
  490.    if(!SD.exists((char *)filename.c_str())){ //Check if file exists.
  491.    ErrorHandler(3);}
  492.  
  493.  
  494.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED off.
  495.  
  496. } //end MakeFile function.
  497.  
  498.  
  499.  
  500. //RemoveFile ******************************************************
  501. void RemoveFile(){
  502.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED on.
  503.   isDirectory = false; //reset the directory flag.
  504.   isRemoveFile = true; //set delete flag.
  505.  
  506.    
  507.    Serial.println(F("(1)Remove the file...")); //debug.
  508.  
  509. //  EnterFileName();//Check the file name.
  510.  
  511.   //generate a random file name.Remove.
  512.   int i = random(65,90);//test.
  513.   filename = root + String((char) i) + ".txt";//test.
  514.   Serial.print(F("(2)"));//test.
  515.   Serial.println(filename);//test.
  516.  
  517.  
  518.   SD.remove((char *)filename.c_str()); //Must use (char *)filename.c_str() to work.
  519.   delay(500); //Wait for SD card to complete.
  520.   myFile.close(); //Write the file to the SD card.
  521.   delay(500); //Wait for file to close.
  522.  
  523.   isRemoveFile = false; //set delete flag.
  524.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED off.
  525.  
  526. } //End MakeDirectory function.
  527.  
  528.  
  529.  
  530. //RemoveDirectory **************************************************
  531. void RemoveDirectory(){
  532.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED on.
  533.   isDirectory = true; //This may need to be moved to the filename function.
  534.   isRemoveDirectory = true; //set delete flag.
  535.  
  536.  
  537.  
  538.   Serial.println(F("(1)Remove the directory...")); //debug.
  539.  
  540.  // EnterFileName();//Check the file name.
  541.  
  542.  
  543.   //generate a random file name.Remove.
  544.   int i = random(65,90);//test.
  545.   filename = root + String((char) i);//test.
  546.   Serial.print(F("(2)Directory: "));//test.
  547.  
  548.  
  549.   Serial.println(filename);//test.
  550.  
  551.  
  552.   SD.rmdir((char *)filename.c_str());
  553.   delay(500); //Wait for SD card to complete.
  554.   myFile.close(); //Write the file to the SD card.
  555.   delay(500); //Wait for file to close.
  556.  
  557.   isDirectory = false; //reset the directory flag.
  558.   isRemoveDirectory = false; //set delete flag.
  559.   redLEDstate = flashLED(redLED, redLEDstate); //Turn LED off.
  560.  
  561. } //End RemoveDirectory function.
  562.  
  563.  
  564.  
  565.  
  566. //ListDirectory **************************************************
  567. void ListDirectory(){
  568.  
  569.     yellowLEDstate = flashLED(yellowLED, yellowLEDstate); //Turn LED on.
  570.  
  571.     myFile = SD.open("/"); //Root directory.
  572.     delay(200);//Wait for myFile to open.
  573.     PrintDirectory(myFile, 0);
  574.     myFile.close();//Close the myFile.
  575.     delay(200);//Wait for file to close.
  576.    
  577.     yellowLEDstate = flashLED(yellowLED, yellowLEDstate); //Turn LED off.
  578.    
  579. }//end ListDirectory function.
  580.  
  581.  
  582. // PrintDirectory **************************************************
  583. // This code is from the arduino library SD Listfiles ( https://www.arduino.cc/en/Tutorial/listfiles ).
  584. // The code does not work correctly. The last entrys in a subdirectory does not display.
  585.  
  586. void PrintDirectory(File dir, int numTabs) {
  587.   dir.seek(0); // Add seek!!!
  588.  
  589.   while (true) {
  590.  
  591.     File entry =  dir.openNextFile();
  592.      if (! entry) {
  593.       dir.rewindDirectory();
  594.       break;
  595.     } //End if.
  596.    
  597.     for (uint8_t i = 0; i < numTabs; i++) {
  598.       Serial.print('\t');
  599.     } //End for.
  600.    
  601.     Serial.print(entry.name());
  602.     if (entry.isDirectory()) {
  603.       Serial.println("/");
  604.       PrintDirectory(entry, numTabs + 1);
  605.     } //End if.
  606.    
  607.     else
  608.     {
  609.       // files have sizes, directories do not
  610.       Serial.print("\t\t");
  611.       Serial.println(entry.size(), DEC);
  612.     } //End else
  613.    
  614.     entry.close(); //Write the file to the disk.
  615.    
  616.   } //End while.
  617.  
  618. } //End PrintDirectory function.
  619.  
  620. //flashLED **********************************************************
  621. int flashLED(int ledPin,int ledState){
  622.  
  623.  
  624.     // if the LED is off turn it on and vice-versa:
  625.     if (ledState == LOW) {
  626.       ledState = HIGH;
  627.     } //end if.
  628.    
  629.     else {
  630.       ledState = LOW;
  631.     } //end else.
  632.  
  633.     // set the LED with the ledState of the variable:
  634.     digitalWrite(ledPin, ledState);
  635. //  } //end if.
  636.  
  637. return(ledState); //Return the LED state.
  638.  
  639. } //end flashLED function.
  640.  
  641.  
  642.  
  643. //ErrorHandler ***************************************************
  644. //Use this function to trap and display error messages. If an error
  645. //occurs stop all processing.
  646. void ErrorHandler(int errorCode){
  647.  
  648.  
  649. switch(errorCode){
  650.  
  651. case 1:  
  652.   Serial.print(F("ERROR[1]: "));
  653.   Serial.print(filename);
  654.   Serial.println(F(" file not found."));
  655.   tone(buzzerPin, 494, 100);  //Debug.
  656.   delay(200); //Debug.
  657.   noTone(buzzerPin);//Debug. Turn off tone function.
  658.   while(true); //Stop running.
  659. break;
  660.  
  661. case 2:
  662.     Serial.print(F("ERROR[2]: directory "));
  663.     Serial.print(filename);
  664.     Serial.println(F(" does not exist.")); //Error message.
  665.     tone(buzzerPin, 494, 100);  //Debug.
  666.     delay(200); //Debug.
  667.     noTone(buzzerPin);//Debug. Turn off tone function.
  668.     while(true); //Stop running.
  669. break;
  670.  
  671. case 3:
  672.     Serial.print(F("ERROR[3]: Can not create file "));
  673.     Serial.print(filename);
  674.     Serial.println("."); //Error message.
  675.     tone(buzzerPin, 494, 100);  //Debug.
  676.     delay(200); //Debug.
  677.     noTone(buzzerPin);//Debug. Turn off tone function.
  678.     while(true); //Stop running.
  679. break;
  680.  
  681.  
  682. case 4:  
  683.   Serial.print(F("ERROR[4]: Could not OPEN file "));
  684.   Serial.print(filename);
  685.   Serial.println(".");
  686.   tone(buzzerPin, 494, 100);  //Debug.
  687.   delay(200); //Debug.
  688.   noTone(buzzerPin);//Debug. Turn off tone function.
  689.   while(true); //Stop running.
  690. break;
  691.  
  692.  
  693. case 9:
  694.     Serial.println(F("ERROR[9]: SD card initialization failed!"));
  695.     Serial.println(F("Program execution is halted."));
  696.     tone(buzzerPin, 494, 100);  //Debug.
  697.     delay(200); //Debug.
  698.     noTone(buzzerPin);//Debug. Turn off tone function.
  699.     while(true); //Stop running.
  700. break;
  701.  
  702.  
  703.   } //End case statement.
  704.  
  705.  
  706. }//End ErrorHandler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement