title: Write from Serial Port to SD card
body: <p>I am trying to write from the Serial Port to an SD Card in my Arduino Mega 2560, using a card module.</p>
  
  <p>I want to be able to write in a txt file what I type in the serial com.</p>
  
  <pre><code>#include &lt;SPI.h&gt;
  #include &lt;SD.h&gt;
  
  const int chipSelect = 4;
  void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    Serial.print("This is a test and should be ignored");
     if (!SD.begin(chipSelect)) {
      Serial.println("\nCard failed, or not present");
      // don't do anything more:
      return;
    }
    else{
    Serial.println("\ncard initialized.");
  }
  }
  
  
  void loop() {
    // put your main code here, to run repeatedly
  File OpenFile = SD.open("test.txt", FILE_WRITE);
    if(OpenFile and Serial.available());
    {
      OpenFile.println(Serial1.read());
      OpenFile.close();
    }
  }
  </code></pre>
  
  <p>However a continous line of "-1" and "1", without the ", is written to the SD.</p>
  
  <p>Yes, I am able to write to the SD card through other methods...</p>
  
  <p>Cheers, PoP</p>