flip a relay on pin 2 on recieving of incoming data from bluetooth

I currently have the following sketch working where through a bluetooth phone app, I am able to write (with start + end maker @ + $) to an sd card attached to my uno.

I want to flip a relay attached to pin 2 upon reciept of incoming data from bluetooth, this must happen prior to initialising the sd card (because the sd card connects to the uno when the relay toggles). (Note I cannot use any “delay(1000);” due to blocking the run operation.)

Ive tried inserting this without success

if (Serial.available() >0 ) {
digitalWrite(ledPin, HIGH);

Any guidance would be greatly appreciated

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

int ledPin =  2;    // LED connected to digital pin 2
 
const byte csSD = 4;//10;
 
File testDataFile;
SoftwareSerial ssPort(5, 7);    //(RX, TX) thats to say tx on the hco6 goes to pin 5 uno, rx on hc06 to pin7
 
const byte numChars = 255; //**** 32 default ****adjust this number for the longest data set
                          // can be up to 255.
char receivedChars[numChars];
boolean newData = false;  // true whenever new data comes in
                          // must be set to false before new data can be read
void setup()
{
   Serial.begin(9600); 
   ssPort.begin(9600);
   initSD();
   Serial.println(F("starting test data reception"));
   delay(3000);
   //insert minimalist blink
   pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
  //delay(1000); ;                      // wait for 1 second
  delay(10000);                       // wait for 10 second//attach time delay relay to carry over
  //delay(100000); ;                      // wait for 100 second
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  //delay(9000);
}
 
void loop()
{
   recvWithStartEndMarkers();  // get new data if it is there
   if (newData == true) // got new data, save it to SD
   {
      Serial.print(F("This data just received   "));
      Serial.println(receivedChars);
      //testDataFile = SD.open("test.txt", FILE_WRITE); //appends to existing text
      testDataFile = SD.open("test.txt", O_WRITE | O_CREAT | O_TRUNC);  //replaces existing text

      if (testDataFile)
      {
         Serial.print(F("Writing to test.txt..."));
         testDataFile.println(receivedChars);
         testDataFile.close();
         Serial.println(F("done."));
      }
      else
      {
         Serial.println(F(" $$$$$ error opening test.txt $$$$"));
      }
      newData = false;  // OK ready for more data
   }
}
 
void recvWithStartEndMarkers()
{
   static boolean recvInProgress = false;
   static byte ndx = 0;
   char startMarker = '@';//'<'
   char endMarker = '

; //’>’
  char rc;

while (ssPort.available() > 0 && newData == false)
  //while (Serial.available() > 0 && newData == false)
  {
      rc = ssPort.read();
      //rc = Serial.read();

if (recvInProgress == true)
      {
        if (rc != endMarker)
        {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars)
            {
              ndx = numChars - 1;
            }
        }
        else
        {
            receivedChars[ndx] = ‘\0’; // terminate the string
            recvInProgress = false;
            ndx = 0;
            newData = true;
        }
      }
      else if (rc == startMarker)
      {
        recvInProgress = true;
      }
  }
}

void initSD()
{
  Serial.print(F(“Initializing SD card…”));
  if (!SD.begin(csSD))
  {
      Serial.println(F("############ initialization failed! ########"));
      while (1);
  }
  Serial.println(F(“initialization done.”));
}

I have heard of flipping cows, but never flipping a relay. What exactly do you have in mind?

Paul

sorry yes a poor choice of words, i meant toggle a relay

  digitalWrite(ledPin, !digitalRead(ledPin));  //toggle the state of the pin

Strange name for a pin controlling a relay

I want to flip a relay attached to pin 2 upon reciept of incoming data from bluetooth, this must happen prior to initialising the sd card (because the sd card connects to the uno when the relay toggles). (Note I cannot use any "delay(1000);" due to blocking the run operation.)

if (newData == true) // got new data, save it to SD
   {
      //flip relay attached to pin 2
      Serial.print(F("This data just received   "));
      Serial.println(receivedChars);
      //testDataFile = SD.open("test.txt", FILE_WRITE); //appends to existing text
      testDataFile = SD.open("test.txt", O_WRITE | O_CREAT | O_TRUNC);  //replaces existing text

Do you mean the code should be as follows ?? thanks

if (newData == true) // got new data, save it to SD
   {
      //flip relay attached to pin 2
       if (Serial.available() >0 ) {
      digitalWrite(ledPin, HIGH);
      Serial.print(F("This data just received   "));
      Serial.println(receivedChars);
      //testDataFile = SD.open("test.txt", FILE_WRITE); //appends to existing text
      testDataFile = SD.open("test.txt", O_WRITE | O_CREAT | O_TRUNC);  //replaces existing text

Do you mean the code should be as follows ?? thanks

No.

if (Serial.available() >0 ) {

You have already received the message and said that new data was available. There's no need to check if there is any more Serial.available(). Why do you think you need to do this? If you want to do something with a switch before you save the received message to the SD card just do it before you save the message.

Yes I think that about gives me what I wanted thankyou.

#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>

int ledPin =  2;    // LED connected to digital pin 2
 
const byte csSD = 4;//10;
 
File testDataFile;
SoftwareSerial ssPort(5, 7);    //(RX, TX) thats to say tx on the hco6 goes to pin 5 uno, rx on hc06 to pin7
 
const byte numChars = 255; //**** 32 default ****adjust this number for the longest data set
                          // can be up to 255.
char receivedChars[numChars];
boolean newData = false;  // true whenever new data comes in
                          // must be set to false before new data can be read
void setup()
{
   Serial.begin(9600);
   ssPort.begin(9600);
   //initSD();
//   Serial.println(F("starting test data reception"));
     Serial.println(F("waiting for BlueTooth Data before proceeding"));
//   delay(3000);
   //insert minimalist blink
   //pinMode(ledPin, OUTPUT);
   //digitalWrite(ledPin, HIGH);   // turn the LED on (HIGH is the voltage level)
   //delay(1000); ;                      // wait for 1 second
   //delay(10000);                       // wait for 10 second//attach time delay relay to carry over
   //delay(100000); ;                      // wait for 100 second
   //digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
   //delay(9000);
}
 
void loop()
{
   recvWithStartEndMarkers();  // get new data if it is there
   if (newData == true) // got new data, save it to SD
   {
      //flip relay attached to pin 2
      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, HIGH);   // turn the Relay on (HIGH is the voltage level)
      Serial.println(F("Turn relay on for 5 seconds"));
      //delay(1000); ;                      // wait for 1 second
      delay(6000); ;                      // wait for 6 seconds//attach time delay relay to carry over 5 minutes
      //delay(10000);                       // wait for 10 second//attach time delay relay to carry over
      //delay(100000); ;                      // wait for 100 second
      digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
      //ssPort.begin(9600);
      initSD();
      Serial.println(F("starting test data reception"));
      delay(3000);
      Serial.print(F("This data just received   "));
      Serial.println(receivedChars);
      //testDataFile = SD.open("test.txt", FILE_WRITE); //appends to existing text
      testDataFile = SD.open("test.txt", O_WRITE | O_CREAT | O_TRUNC);  //replaces existing text

      if (testDataFile)
      {
         Serial.print(F("Writing to test.txt..."));
         testDataFile.println(receivedChars);
         testDataFile.close();
         Serial.println(F("done."));
      }
      else
      {
         Serial.println(F(" $$$$$ error opening test.txt $$$$"));
      }
      newData = false;  // OK ready for more data
      Serial.println(F("OK ready for more data in 5 minutes"));
   }
}
 
void recvWithStartEndMarkers()
{
   static boolean recvInProgress = false;
   static byte ndx = 0;
   char startMarker = '@';//'<'
   char endMarker = '

; //’>’
  char rc;

while (ssPort.available() > 0 && newData == false)
  //while (Serial.available() > 0 && newData == false)
  {
      rc = ssPort.read();
      //rc = Serial.read();

if (recvInProgress == true)
      {
        if (rc != endMarker)
        {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars)
            {
              ndx = numChars - 1;
            }
        }
        else
        {
            receivedChars[ndx] = ‘\0’; // terminate the string
            recvInProgress = false;
            ndx = 0;
            newData = true;
        }
      }
      else if (rc == startMarker)
      {
        recvInProgress = true;
      }
  }
}

void initSD()
{
  Serial.print(F(“Initializing SD card…”));
  if (!SD.begin(csSD))
  {
      Serial.println(F("############ initialization failed! ########"));
      while (1);
  }
  Serial.println(F(“initialization done.”));
}