Software Serial issues - Solved #1

The code below works fine with the regular hardware Serial pins of the UNO ( actually an EtherTen PCB ) but not with the Software Serial on pins 10 & 11. Not sure why…

#include <SPI.h>
#include <SD.h>
#include <phi_interfaces.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include "Wire.h"
#include <SoftwareSerial.h>

const int chipSelect = 4;                                   // SD card select on shield

// Define the Machine Digital Inputs and Outputs
#define StartPB 8                                         // EtherTen mapping
#define total_buttons 1
char mapDIN[] = {'S'};
byte pinDIN[] = { StartPB};
phi_button_groups MyDIN(mapDIN, pinDIN, total_buttons);

char ValidDIN = '0';
const int numChars = 100;
char receivedChars[numChars] ;
char readCount[4];


unsigned int static recordCount = 0;
unsigned int noOfRecords = 0;
unsigned int ledState = LOW;
unsigned long scanMs = 10;

boolean newData = false;
boolean writeToSD = false;
boolean rdAllData = false;
boolean allRecIn = 0;
boolean waitForData = 0;

const byte recvLED = 13;


// DEFINE LCD PINS AND INSTANTIATE
LiquidCrystal_I2C  lcd(0x27, 2, 1, 0, 4, 5, 6, 7);          // 0x27 is the I2C bus address

SoftwareSerial mySerial(10, 11);                            // Tx of HM10 = 10 ; Rx of HM10 = 11

// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//                        SETUP
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

void setup()
{
  //Serial.begin(9600);

  mySerial.begin(9600);

  pinMode ( recvLED, OUTPUT );

  digitalWrite ( recvLED, LOW );

  Wire.begin();

  // START THE LCD INTERFACE
  lcd.begin(20, 4);
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.clear();
 
  if (!SD.begin(chipSelect))
  {
    lcd.print(F("Card failed....."));
    return;                                               // No card .. just return..
  }
  lcd.print(F("SD Card OK....."));

  pinMode(chipSelect, OUTPUT);

  newData = false;
}

// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//                      MAIN LOOP
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


void loop()
{

  ValidDIN = MyDIN.getKey();

  if ( ValidDIN == 'S' )
  {
    ValidDIN = '0';
    eraseOldFiles();
    waitForData = 1;
    lcd.setCursor(0, 0);
    lcd.print(F("Waiting for Data..."));
    allRecIn = 0;
  }

  if ( waitForData == 1 )
  {
    recvWithStartEndMarkers();                           // Read data from Serial port and store to SD card..
    writeNewData2SDC();
  }
  delay(scanMs);
}

// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//                       FUNCTIONS
// &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

// FUNCTION TO READ SERIAL PORT AND MOVE DATA TO CHAR ARRAY..

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '[';
  char endMarker = ']';
  char inChar;

  if (mySerial.available() > 0)
  { // Data available in In buffer ?
    while (mySerial.available() > 0 && writeToSD == false )
    { // As long as there is data...
      digitalWrite ( recvLED, HIGH );
      inChar = mySerial.read();                                     // Read the databyte from serial buffer
      if (recvInProgress == true)
      {
        if (inChar != endMarker)
        { // Upon StartMarker Read char and add to Char array
          receivedChars[ndx] = inChar;                            // till the EndMarker arrives
          ndx++;
          if (ndx >= numChars)
          {
            ndx = numChars - 1;                                   // To maintian fixed length records.
          }
        }
        else
        {
          recvInProgress = false;                                 // reset variable for next read..
          ndx = 0;
          writeToSD = true;
        }
      }
      else if ( inChar == startMarker) recvInProgress = true ;   // Await the start marker..
    }
  }
  digitalWrite ( recvLED, LOW );
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

// FUNCTION TO WRITE RECEIVED CHAR ARRAY TO SD CARD AND TRACK NO OF RECORDS RECEIVED.

void writeNewData2SDC()
{
  char dataForLCD[21];
  if ( writeToSD == true)
  {
    File dataFile = SD.open("GRPLAN.CSV", FILE_WRITE);       // Open the file on the SD card.
    if (dataFile)
    {
      dataFile.println(receivedChars);                       // Write the received characters to the SD card..
      dataFile.close();
    }
    else {
      lcd.clear();
      lcd.print(F("GRPLAN.CSV"));
      lcd.setCursor(0, 1);
      lcd.print(F("  SD File Error !!  "));
      lcd.setCursor(0, 2);
      lcd.print(F(" Reset and Verify.."));
      while (1);                                        // Hold the message...and wait for reset.
    }

    if (recordCount == 0)
    { // Is this the first Record count row ?
      File dataFile = SD.open("GRPLAN.CSV", FILE_READ);  // If so, grab and save the record Count.
      for (int i = 2; i < 5; i++)
      {
        dataFile.seek(i);
        readCount[i - 2] = dataFile.read();
      }
      noOfRecords = (atoi(readCount)) ;                  // Save the noOfRecords for comparison.
      dataFile.close();
      lcd.setCursor(0, 0);
      lcd.print(F("Serial link active !"));
      sprintf ( dataForLCD, "%03u Records to get.", noOfRecords);
      lcd.setCursor(0, 1);
      lcd.print(dataForLCD);
    }

    if ( recordCount > 0)
    { // Update Record count on LCD.
      lcd.setCursor(0, 3);
      sprintf( dataForLCD, "Record %03u recieved!", recordCount);
      lcd.print( dataForLCD);
    }

    recordCount++;                                       // Increment counter
    if (recordCount == noOfRecords + 1 )
    { // All records have been received..
      allRecIn = 1;
    }
    mySerial.println("OK");                                  // Data received and saved. Acknowledge to sender ..
    writeToSD = false;

    if (allRecIn == 1)
    {
      File planFile = SD.open("GRPLAN.CSV", FILE_READ);            // Arrange to copy the Plan to Actual file..
      File actualFile = SD.open("ACTUAL.CSV", FILE_WRITE);

      size_t n;                                                    // Copy... fast using large size buffer
      uint8_t buf[94];
      while ((n = planFile.read(buf, sizeof(buf))) > 0)
      {
        actualFile.write(buf, n);
      }
      planFile.close();
      actualFile.close();
      lcd.clear();
      lcd.print( "ACTUAL.CSV Created !");
      delay(2000);
      waitForData = 0;
    }
  }
}

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

//**********************************************************
// Code to remove old and create a new GRPLAN.CSV & ACTUAL.CSV file.

void eraseOldFiles()
{
  if (SD.exists("GRPLAN.CSV"))
  {
    if (!SD.remove("GRPLAN.CSV"))
    {
      lcd.setCursor( 0, 1);
      lcd.print(F( " Error Erasing #1 "));
      while (1);
    }
  }
  if (SD.exists("ACTUAL.CSV")) {
    if (!SD.remove("ACTUAL.CSV")) {
      lcd.setCursor( 0, 1);
      lcd.print(F( " Error Erasing #2 "));
      while (1);
    }
  }
  lcd.clear();
  lcd.print( "Old files cleared !!");
  delay (2000);
}
  if (!SD.begin(chipSelect))

The SD reader/writer is an SPI device, which uses the SPI pins (10, 11, 12, and 13). You can't use them for other purposes.

Oh ...

Thanks Paul !!