搜索代码

Lesson 106-3: Creating Two Files Simultaneously

Lesson 106-3: Creating Two Files Simultaneously

In this lesson, we learn how to use a Micro SD card. We learn how to write to the card and how to read text. How to log voltage from a potentiometer to the memory card. How to save the data in CSV format so we can open it in a spreadsheet like Excel for plotting and other analysis.

This is a simple way to create two files simultaneously on a microSD card.

398-Lesson 106: Using a Micro SD Card and Data Logging with Arduino
语言: C++
/*

 Original from File->Examples->SD  SD card read/write
 
 
  * Lesson 106-2: Reading voltage from potentiometer and saving it on microSD card
  In this example we read A0 pin to read the potentiometer's voltage
 and writing it to the Micro SD card.
 We have also a push button, when pressed, it will print the 
 file data on the serial monitor.
 
 In the lesson https://youtu.be/TduSOX6CMr4
 Example 1: Card info demonstrated
 Example 2: file list demonstrated
 Example 3: Datalogger
 Example 4: DoNotBlockWrite
 Example 5 (this code): Reading potentiometer, saving on micro SD card and using push button to read it
 Example 6: Using example 5 but creating a CSV file so we can open it in Excel
 Example 7 (this code): Writing into two files at the same time in micro SD Card
 
  * Watch Video instruction for this code:https://youtu.be/TduSOX6CMr4
  * 
  * This code is part of Arduino Step by Step Course which starts here:  https://youtu.be/-6qSrDUA5a8
  * 
  * for library of this code visit http://robojax.com/
  * 
 If you found this tutorial helpful, please support me so I can continue creating 
 content like this. Make a donation using PayPal by credit card https://bit.ly/donate-robojax 
 
 written/updated by Ahmad Shamshiri on Jun 18, 2022
 www.Robojax.com
 
  *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
  * This code has been downloaded from Robojax.com
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation, either version 3 of the License, or
     (at your option) any later version.
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 
    SD card attached to SPI bus as follows:
  ** MOSI - pin 11
  ** MISO - pin 12
  ** CLK - pin 13
  ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
 
   created   Nov 2010
   by David A. Mellis
   modified 9 Apr 2012
   by Tom Igoe
 
   This example code is in the public domain.
 
 */
#include <SPI.h>
#include <SD.h>

//serial monitor print while writing log
boolean serialPrint = true;//true or false
char *logFile1 ="datalog1.txt";//file name. must be max 8 character excluding the extension (book.txt is 4 char)
char *logFile2 ="datalog2.txt";//file two



File myFile1;//define object for file 
File myFile2;
void writLog(float);//prototype of writLog funciton at the bottom of this code

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }



  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
   SD.remove(logFile1);
   SD.remove(logFile2);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
    myFile1 = SD.open(logFile1, FILE_WRITE);
    myFile2 = SD.open(logFile2, FILE_WRITE);

   if (!myFile1) {
    Serial.println("log file missing");
    while(1);
   }



}

void loop() {
  int sensor = analogRead(A0);//read A0 pin

  //convert value of 0 to 1023 to voltage. 
  //Arduino UNO is 5V so we use 5
  float voltage = sensor * (5.0 / 1023.0);
  


   writLog(voltage);   

}

void writLog(float voltage)
{
   myFile1 = SD.open(logFile1, FILE_WRITE);
   myFile2 = SD.open(logFile2, FILE_WRITE);

    myFile1.print("Time: ");//writing the text: Time:
    myFile1.print(millis()/100); //recod time  
    myFile1.print("\tVoltage: ");//writing the text Voltage after a tab \t
    myFile1.print(voltage);//Writing voltage
    myFile1.println("V");//adding V at the end

    myFile2.print("Just time:");  
    myFile2.println(millis()); //recod time  
      
   if(serialPrint){
      Serial.print ("Time:");
      Serial.print(millis()/1000);  
      Serial.print ("\tVoltage: ");
      Serial.print(voltage);       
   }        
    // close the file1, file2:
    myFile1.close();
    myFile2.close();    
    if(serialPrint){
      Serial.println(" File1 done.");
    }

}//writLog

|||您可能需要的东西

文件📁

没有可用的文件。