newbie with a few questions,

hello everyone, this is my first post here but ive been reading quite a bit the last few days.

this post has got a lot longer than i planned so please skip to the last paragraph if you dont want to read the backstory on why i am even doing this :slight_smile:

im working on a what started off as a simple remote controlled switch system for some equipment i am setting up on/in my works trailer, its now turned into something a lot more complicated as it always does, and i have come across some problems i have been getting stuck trying to fix, so i am turning to the experts for some help.

i have got some basic experience with c programming but i am struggling with the slight differences in arduino code.

firstly i will list what things i am using and try to explain my code below.

arduino UNO R3 (i know its not particually power conservative but i am using 2 35ah 12v electric wheelchair batteries that will charge while trailer is connected to car so i do not foresee a power problem)
HC-05 Bluetooth Module
ACS712 Current Monitor
Generic 8 channel relay panel

below is the code i am currently using, i will explain more afterwards

#include <SoftwareSerial.h> 
SoftwareSerial MyBlue(3, 2); // RX | TX 
int flag = 0; 
int relay1 = 4;
int relay2 = 5;
int relay3 = 6;
int relay4 = 7;
int relay5 = 8;
int relay6 = 9;
int relay7 = 10;
int relay8 = 11;

int j=0,k=0; 
char data_temp;
int read_count=0,tag_count=0;

String inputString = ""; 

int analogInput = 4;
float vout = 0.0;
float vin = 0.0;
float R1 = 100000.0; // resistance of R1 (100K) -see text!
float R2 = 10000.0; // resistance of R2 (10K) - see text!
int value = 0;

//Measuring Current Using ACS712

const int analogIn = 5; //Connect current sensor with A0 of Arduino
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500; 
double Voltage = 0; //voltage measuring
double Amps = 0;// Current measuring

void setup() 
{   
 Serial.begin(9600); 
 MyBlue.begin(9600); 
 pinMode(relay1, OUTPUT); 
 pinMode(relay2, OUTPUT); 
 pinMode(relay3, OUTPUT); 
 pinMode(relay4, OUTPUT); 
 pinMode(relay5, OUTPUT); 
 pinMode(relay6, OUTPUT); 
 pinMode(relay7, OUTPUT); 
 pinMode(relay8, OUTPUT); 
 digitalWrite(relay1, HIGH);
 digitalWrite(relay2, HIGH);
 digitalWrite(relay3, HIGH);
 digitalWrite(relay4, HIGH);
 digitalWrite(relay5, HIGH);
 digitalWrite(relay6, HIGH);
 digitalWrite(relay7, HIGH);
 digitalWrite(relay8, HIGH);
 Serial.println("Ready to connect\nDefualt password is 1234 or 000"); 
} 
void loop() 
{ 
 if (MyBlue.available())
  { 
   data_temp = Serial.read(); 
   inputString += data_temp;
// Debug print
  Serial.print(inputString);

 if (inputString=="Front Beacon") 
 { 
      digitalWrite(relay1, LOW); 
      Serial.println("LED On/ Beacon Front On"); 
      MyBlue.println("Front Beacon On");
      inputString="";
 } 
 else if (inputString=="Off") 
 { 
   digitalWrite(relay1, HIGH); 
   digitalWrite(relay2, HIGH); 
   digitalWrite(relay3, HIGH); 
   digitalWrite(relay4, HIGH); 
   digitalWrite(relay5, HIGH); 
   digitalWrite(relay6, HIGH); 
   digitalWrite(relay7, HIGH); 
   digitalWrite(relay8, HIGH); 
   Serial.println("System Off"); 
   MyBlue.println("System Going To Sleep"); 
   inputString="";
 } 
 else if (inputString=="Rear Beacon") 
 { 
      digitalWrite(relay2, LOW); 
      Serial.println("Beacon Rear On");
       MyBlue.println("Rear Beacon On");
       inputString="";
 } 
 else if (inputString=="Side Beacon") 
 { 
      digitalWrite(relay3, LOW); 
      Serial.println("Side Beacons On");
       MyBlue.println("Side Beacons On");
       inputString="";
 } 
 else if (inputString=="All Beacon") 
 { 
      digitalWrite(relay1, LOW); 
      digitalWrite(relay2, LOW); 
      digitalWrite(relay3, LOW); 

      Serial.println("All Beacons ON");
       MyBlue.println("All Beacons ON");
       inputString="";
   } 
 else if (inputString=="Interior Lights") 
 { 
  Serial.println("int lights");

      digitalWrite(relay4, LOW); 
      Serial.println("Interior Lights On");
       MyBlue.println("Interior Lights On");
       inputString="";
 }
 else if (inputString=="Work Light") 
 { 
      digitalWrite(relay5, LOW); 
      Serial.println("Work Light On");
       MyBlue.println("Work Light On");
       inputString="";
   } 
 else if (inputString=="Voltage") 
 { 
   value = analogRead(analogInput);
   vout = (value * 5.0) / 1024.0; // see text
   vin = vout / (R2/(R1+R2)); 
   if (vin<0.09) {
   vin=0.0;//statement to quash undesired reading !
  }
  Serial.print(vin);
  Serial.print(" V\n");
  MyBlue.println("Main System Battery Voltage:- "); 
  MyBlue.print(vin);
  MyBlue.print(" V\n");
  MyBlue.println();
  
  RawValue = analogRead(analogIn);//reading the value from the analog pin
 Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
 Amps = ((Voltage - ACSoffset) / mVperAmp);
 
//Prints on the serial port
 Serial.print("Raw Value = " ); // prints on the serial monitor
 Serial.print(RawValue); //prints the results on the serial monitor

 Serial.print("\t mV = "); // shows the voltage measured 
 Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point

 Serial.print("\t Amps = "); // shows the voltage measured 
 Serial.println(Amps,3);// the '3' after voltage allows you to display 3 digits after decimal point
 
  //Serial.println(AcsValueF);//Print the read current on Serial monitor
  //MyBlue.println("Current System Power Draw:-");
  //MyBlue.println(AcsValueF);
  
  delay(5000);
  inputString="";
 }
 }
}

my trailer is planned to have a number of different items fitted that are going to have a direct switch bank in the trailer to control them but i am also hoping to be able to remotely turn things on and off aswell as monitor battery voltage and rough current draws via a bluetooth terminal connection.

the issue i am having is i cant seem to get the inputString to clear after each use so basicly i can issue one command and then have to restart the arduino to use it again. also i can seem to work out how to deal with an invalid request to allow continued use.

sorry for the longggggg post :slight_smile:

dmpdiscos:
the issue i am having is i cant seem to get the inputString to clear after each use so basicly i can issue one command and then have to restart the arduino to use it again.

did you mean, "the issue i am having is i cant seem to get the inputString to clear after each use BUT INSTEAD i can ONLY issue one command and then have to restart the arduino to use it again"
?

aarg:
did you mean, "the issue i am having is i cant seem to get the inputString to clear after each use BUT INSTEAD i can ONLY issue one command and then have to restart the arduino to use it again"
?

not exactly, i can send one command of any of the options i have setup in the various if statements but after that command if i send another it does not recognise it at all so i am assuming it is not Clearing the inputString unless i have completely missed something - i havent dont c code in a number of years so im very rusty :slight_smile:

You are checking your bluetooth module and if data is available there, you are reading from the Serial port. It is kind of like hearing someone knock on your front door so you answer the back door

  if (MyBlue.available())
  {
    data_temp = Serial.read();
    inputString += data_temp;
    //...

You also need to get rid of the String variables and learn how to use regular C-style strings or else your Uno will eventually crash. Read Robin2's excellent tutorial Serial Basic input to do it properly.

blh64:
You are checking your bluetooth module and if data is available there, you are reading from the Serial port. It is kind of like hearing someone knock on your front door so you answer the back door

  if (MyBlue.available())

{
    data_temp = Serial.read();
    inputString += data_temp;
    //...




You also need to get rid of the String variables and learn how to use regular C-style strings or else your Uno will eventually crash. Read Robin2's excellent tutorial [Serial Basic input](http://forum.arduino.cc/index.php?topic=396450.0) to do it properly.

ok cheers for that one, i am used to having to check and recheck everything with regards to checking if somethings available :slight_smile: but i can sort that bit, i will take a look at the tutorial also

ok i have tweaked the serial settings to use a char array from what i can gather and although again i can run ONE command, i cannot repeatedly run commands as after the first command the arduino does not respond to any others

updates code below

/*   
HC05 - Bluetooth AT-Command mode  
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/ 
*/ 
#include <SoftwareSerial.h> 
SoftwareSerial MyBlue(3, 2); // RX | TX 
int flag = 0; 
int relay1 = 4;
int relay2 = 5;
int relay3 = 6;
int relay4 = 7;
int relay5 = 8;
int relay6 = 9;
int relay7 = 10;
int relay8 = 11;

int r1 = 0;
int r2 = 0;
int r3 = 0;
int r4 = 0;
int r5 = 0;
int r6 = 0;
int r7 = 0;
int r8 = 0;

int all=0;
 
int j=0,k=0; 
char data_temp;
int read_count=0,tag_count=0;

String inputString = ""; 

int analogInput = 4;
float vout = 0.0;
float vin = 0.0;
float R1 = 100000.0; // resistance of R1 (100K) -see text!
float R2 = 10000.0; // resistance of R2 (10K) - see text!
int value = 0;

//Measuring Current Using ACS712

const int analogIn = 5; //Connect current sensor with A0 of Arduino
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500; 
double Voltage = 0; //voltage measuring
double Amps = 0;// Current measuring

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;



void setup() 
{   
 Serial.begin(57600); 
 MyBlue.begin(9600); 
 pinMode(relay1, OUTPUT); 
 pinMode(relay2, OUTPUT); 
 pinMode(relay3, OUTPUT); 
 pinMode(relay4, OUTPUT); 
 pinMode(relay5, OUTPUT); 
 pinMode(relay6, OUTPUT); 
 pinMode(relay7, OUTPUT); 
 pinMode(relay8, OUTPUT); 
 digitalWrite(relay1, HIGH);
 digitalWrite(relay2, HIGH);
 digitalWrite(relay3, HIGH);
 digitalWrite(relay4, HIGH);
 digitalWrite(relay5, HIGH);
 digitalWrite(relay6, HIGH);
 digitalWrite(relay7, HIGH);
 digitalWrite(relay8, HIGH);
 Serial.println("Ready to connect\nDefualt password is 1234 or 000"); 
} 

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = ',';
    char endMarker = '.';
    char rc;
 
    while (MyBlue.available() > 0 && newData == false) {
        rc = MyBlue.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 showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

void loop() 
{ 

recvWithStartEndMarkers();





 if (strcmp(receivedChars, "Front Beacon") == 0)
 { 
      digitalWrite(relay1, LOW); 
      Serial.println("LED On/ Beacon Front On"); 
      MyBlue.println("Front Beacon On");
      r1=1;
      memset(0, receivedChars, sizeof(receivedChars));
      
 } 
 if (strcmp(receivedChars, "Off") == 0) 
 { 
   digitalWrite(relay1, HIGH); 
   digitalWrite(relay2, HIGH); 
   digitalWrite(relay3, HIGH); 
   digitalWrite(relay4, HIGH); 
   digitalWrite(relay5, HIGH); 
   digitalWrite(relay6, HIGH); 
   digitalWrite(relay7, HIGH); 
   digitalWrite(relay8, HIGH); 
   Serial.println("System Off"); 
   MyBlue.println("System Going To Sleep"); 
   r1=0;
   r2=0;
   r3=0;
   r4=0;
   r5=0;
   r6=0;
   r7=0;
   r8=0;
   memset(0, receivedChars, sizeof(receivedChars));
 } 
 if (strcmp(receivedChars, "Rear Beacon") == 0) 
 { 
  if(r2==0){
      digitalWrite(relay2, LOW); 
      Serial.println("Beacon Rear On");
       MyBlue.println("Rear Beacon On");
       r2=1;
       memset(0, receivedChars, sizeof(receivedChars));
   }else{
       digitalWrite(relay1, HIGH); 
       Serial.println("Beacon Rear Off"); 
      MyBlue.println("Rear Beacon Off");
      r2=0; 
       memset(0, receivedChars, sizeof(receivedChars));
   }
 } 
 if (strcmp(receivedChars, "Side Beacon") == 0) 
 { 
     if(r3==0){
      digitalWrite(relay3, LOW); 
      Serial.println("Side Beacons On");
       MyBlue.println("Side Beacons On");
       r3=1;
       memset(0, receivedChars, sizeof(receivedChars));
   }else{
       digitalWrite(relay3, HIGH); 
       Serial.println("Side Beacons Off"); 
      MyBlue.println("Side Beacons Off");
      r3=0; 
      memset(0, receivedChars, sizeof(receivedChars));
   }
 } 
 if (strcmp(receivedChars, "All Beacon")  == 0)
 { 
   if(all==0){
      digitalWrite(relay1, LOW); 
      digitalWrite(relay2, LOW); 
      digitalWrite(relay3, LOW); 

      Serial.println("All Beacons ON");
       MyBlue.println("All Beacons ON");
       all=1;
       memset(0, receivedChars, sizeof(receivedChars));
   }else{
       digitalWrite(relay1, HIGH);
       digitalWrite(relay2, HIGH); 
       digitalWrite(relay3, HIGH); 
 
       Serial.println("All Beacons OFF"); 
      MyBlue.println("All Beacons OFF");
      all=0; 
      memset(0, receivedChars, sizeof(receivedChars));
   } 
   } 
 if (strcmp(receivedChars, "Interior Lights") == 0) 
 { 

    if(r4==0){
      digitalWrite(relay4, LOW); 
      Serial.println("Interior Lights On");
       MyBlue.println("Interior Lights On");
       r4=1;
       memset(0, receivedChars, sizeof(receivedChars));
   }else if(r4==1){
       digitalWrite(relay4, HIGH); 
       Serial.println("Interior Lights Off"); 
      MyBlue.println("Interior Lights Off");
      r4=0; 
      memset(0, receivedChars, sizeof(receivedChars));
   }
 }
 if (strcmp(receivedChars, "Work Light") == 0)
 { 
      if(r5==0){
      digitalWrite(relay5, LOW); 
      Serial.println("Work Light On");
       MyBlue.println("Work Light On");
       r5=1;
       memset(0, receivedChars, sizeof(receivedChars));
   }else{
       digitalWrite(relay5, HIGH); 
       Serial.println("Work Light Off"); 
      MyBlue.println("Work Light Off");
      r5=0; 
      memset(0, receivedChars, sizeof(receivedChars));
   }
   } 
 if (strcmp(receivedChars, "Voltage") == 0)
 { 
   value = analogRead(analogInput);
   vout = (value * 5.0) / 1024.0; // see text
   vin = vout / (R2/(R1+R2)); 
   if (vin<0.09) {
   vin=0.0;//statement to quash undesired reading !
  }
  Serial.print(vin);
  Serial.print(" V\n");
  MyBlue.println("Main System Battery Voltage:- "); 
  MyBlue.print(vin);
  MyBlue.print(" V\n");
  MyBlue.println();
  
  RawValue = analogRead(analogIn);//reading the value from the analog pin
 Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
 Amps = ((Voltage - ACSoffset) / mVperAmp);
 
//Prints on the serial port
 Serial.print("Raw Value = " ); // prints on the serial monitor
 Serial.print(RawValue); //prints the results on the serial monitor

 Serial.print("\t mV = "); // shows the voltage measured 
 Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point

 Serial.print("\t Amps = "); // shows the voltage measured 
 Serial.println(Amps,3);// the '3' after voltage allows you to display 3 digits after decimal point
 
  //Serial.println(AcsValueF);//Print the read current on Serial monitor
  //MyBlue.println("Current System Power Draw:-");
  //MyBlue.println(AcsValueF);
  
  delay(5000);
  memset(0, receivedChars, sizeof(receivedChars));
 }
 }
    memset(0, receivedChars, sizeof(receivedChars));

You are setting the destination to zero (beginning of RAM), the value to the low byte of whatever receivedChars value happens to be currently, and the length to the size of the char array.

I suspect that is not what you had in mind when you guessed what memset() does.

When you receive a complete message, 'newData' is true. You need to reset it backto false so you can receive more messages. Besides having the call to memset() wrong, you don't need it.

You also have some very odd start/end markers for your transmission. It is more typical to use '<' and '>' or not start marker and a newline '\n' as the end marker

/*
  HC05 - Bluetooth AT-Command mode
  modified on 10 Feb 2019
  by Saeed Hosseini
  https://electropeak.com/learn/
*/
#include <SoftwareSerial.h>
SoftwareSerial MyBlue(3, 2); // RX | TX
int flag = 0;
int relay1 = 4;
int relay2 = 5;
int relay3 = 6;
int relay4 = 7;
int relay5 = 8;
int relay6 = 9;
int relay7 = 10;
int relay8 = 11;

int r1 = 0;
int r2 = 0;
int r3 = 0;
int r4 = 0;
int r5 = 0;
int r6 = 0;
int r7 = 0;
int r8 = 0;

int all = 0;

int j = 0, k = 0;
char data_temp;
int read_count = 0, tag_count = 0;

String inputString = "";

int analogInput = 4;
float vout = 0.0;
float vin = 0.0;
float R1 = 100000.0; // resistance of R1 (100K) -see text!
float R2 = 10000.0; // resistance of R2 (10K) - see text!
int value = 0;

//Measuring Current Using ACS712

const int analogIn = 5; //Connect current sensor with A0 of Arduino
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue = 0;
int ACSoffset = 2500;
double Voltage = 0; //voltage measuring
double Amps = 0;// Current measuring

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;



void setup()
{
  Serial.begin(57600);
  MyBlue.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  pinMode(relay6, OUTPUT);
  pinMode(relay7, OUTPUT);
  pinMode(relay8, OUTPUT);
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);
  digitalWrite(relay5, HIGH);
  digitalWrite(relay6, HIGH);
  digitalWrite(relay7, HIGH);
  digitalWrite(relay8, HIGH);
  Serial.println("Ready to connect\nDefualt password is 1234 or 000");
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = ',';
  char endMarker = '.';
  char rc;

  while (MyBlue.available() > 0 && newData == false) {
    rc = MyBlue.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 showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;
  }
}

void loop()
{

  recvWithStartEndMarkers();

  if ( newData == false ) {
    // no use doing anything, we don't have a command
    return; // exit loop to start again
  }

  // we have new data
  newData = false;  // mark it as processed so we can receive more

  if (strcmp(receivedChars, "Front Beacon") == 0)
  {
    digitalWrite(relay1, LOW);
    Serial.println("LED On/ Beacon Front On");
    MyBlue.println("Front Beacon On");
    r1 = 1;
  }
  if (strcmp(receivedChars, "Off") == 0)
  {
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    digitalWrite(relay5, HIGH);
    digitalWrite(relay6, HIGH);
    digitalWrite(relay7, HIGH);
    digitalWrite(relay8, HIGH);
    Serial.println("System Off");
    MyBlue.println("System Going To Sleep");
    r1 = 0;
    r2 = 0;
    r3 = 0;
    r4 = 0;
    r5 = 0;
    r6 = 0;
    r7 = 0;
    r8 = 0;
  }
  if (strcmp(receivedChars, "Rear Beacon") == 0)
  {
    if (r2 == 0) {
      digitalWrite(relay2, LOW);
      Serial.println("Beacon Rear On");
      MyBlue.println("Rear Beacon On");
      r2 = 1;
      memset(0, receivedChars, sizeof(receivedChars));
    } else {
      digitalWrite(relay1, HIGH);
      Serial.println("Beacon Rear Off");
      MyBlue.println("Rear Beacon Off");
      r2 = 0;
    }
  }
  if (strcmp(receivedChars, "Side Beacon") == 0)
  {
    if (r3 == 0) {
      digitalWrite(relay3, LOW);
      Serial.println("Side Beacons On");
      MyBlue.println("Side Beacons On");
      r3 = 1;
    } else {
      digitalWrite(relay3, HIGH);
      Serial.println("Side Beacons Off");
      MyBlue.println("Side Beacons Off");
      r3 = 0;
    }
  }
  if (strcmp(receivedChars, "All Beacon")  == 0)
  {
    if (all == 0) {
      digitalWrite(relay1, LOW);
      digitalWrite(relay2, LOW);
      digitalWrite(relay3, LOW);

      Serial.println("All Beacons ON");
      MyBlue.println("All Beacons ON");
      all = 1;
    } else {
      digitalWrite(relay1, HIGH);
      digitalWrite(relay2, HIGH);
      digitalWrite(relay3, HIGH);

      Serial.println("All Beacons OFF");
      MyBlue.println("All Beacons OFF");
      all = 0;
    }
  }
  if (strcmp(receivedChars, "Interior Lights") == 0)
  {
    if (r4 == 0) {
      digitalWrite(relay4, LOW);
      Serial.println("Interior Lights On");
      MyBlue.println("Interior Lights On");
      r4 = 1;
    } else if (r4 == 1) {
      digitalWrite(relay4, HIGH);
      Serial.println("Interior Lights Off");
      MyBlue.println("Interior Lights Off");
      r4 = 0;
    }
  }
  if (strcmp(receivedChars, "Work Light") == 0)
  {
    if (r5 == 0) {
      digitalWrite(relay5, LOW);
      Serial.println("Work Light On");
      MyBlue.println("Work Light On");
      r5 = 1;
    } else {
      digitalWrite(relay5, HIGH);
      Serial.println("Work Light Off");
      MyBlue.println("Work Light Off");
      r5 = 0;
    }
  }
  if (strcmp(receivedChars, "Voltage") == 0)
  {
    value = analogRead(analogInput);
    vout = (value * 5.0) / 1024.0; // see text
    vin = vout / (R2 / (R1 + R2));
    if (vin < 0.09) {
      vin = 0.0; //statement to quash undesired reading !
    }
    Serial.print(vin);
    Serial.print(" V\n");
    MyBlue.println("Main System Battery Voltage:- ");
    MyBlue.print(vin);
    MyBlue.print(" V\n");
    MyBlue.println();

    RawValue = analogRead(analogIn);//reading the value from the analog pin
    Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
    Amps = ((Voltage - ACSoffset) / mVperAmp);

    //Prints on the serial port
    Serial.print("Raw Value = " ); // prints on the serial monitor
    Serial.print(RawValue); //prints the results on the serial monitor

    Serial.print("\t mV = "); // shows the voltage measured
    Serial.print(Voltage, 3); // the '3' after voltage allows you to display 3 digits after decimal point

    Serial.print("\t Amps = "); // shows the voltage measured
    Serial.println(Amps, 3); // the '3' after voltage allows you to display 3 digits after decimal point

    //Serial.println(AcsValueF);//Print the read current on Serial monitor
    //MyBlue.println("Current System Power Draw:-");
    //MyBlue.println(AcsValueF);

    delay(5000);
  }
}

blh64:
When you receive a complete message, 'newData' is true. You need to reset it backto false so you can receive more messages. Besides having the call to memset() wrong, you don't need it.

You also have some very odd start/end markers for your transmission. It is more typical to use '<' and '>' or not start marker and a newline '\n' as the end marker

thanks ill try that, as for the start/end markers i have picked those simply because it saves me having to change keyboard screens on my phone, as i am controlling the board via a bluetooth terminal from an android phone and to get to < or > you have to change keyboard to numbers and then symbols at least on my device

ok,so i changed the start and end chars back to < and > for now. and although i can run at least more than one command, each command continuosly loops over and over rather than running once and returning to waiting for a command like it should.

i have added line to revert newData to false in each if statement which i am now beginning to think is the issue, but i dont know of any other way to reset that variable other wise

code below i have removed most of the if statements to save you all some reading :slight_smile:

/*   
HC05 - Bluetooth AT-Command mode  
modified on 10 Feb 2019 
by Saeed Hosseini 
https://electropeak.com/learn/ 
*/ 
#include <SoftwareSerial.h> 
SoftwareSerial MyBlue(2, 3); // RX | TX 
int flag = 0; 
int relay1 = 4;
int relay2 = 5;
int relay3 = 6;
int relay4 = 7;
int relay5 = 8;
int relay6 = 9;
int relay7 = 10;
int relay8 = 11;

int r1 = 0;
int r2 = 0;
int r3 = 0;
int r4 = 0;
int r5 = 0;
int r6 = 0;
int r7 = 0;
int r8 = 0;

int all=0;
 
int j=0,k=0; 
char data_temp;
int read_count=0,tag_count=0;

String inputString = ""; 

int analogInput = 4;
float vout = 0.0;
float vin = 0.0;
float R1 = 100000.0; // resistance of R1 (100K) -see text!
float R2 = 10000.0; // resistance of R2 (10K) - see text!
int value = 0;

//Measuring Current Using ACS712

const int analogIn = 5; //Connect current sensor with A0 of Arduino
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500; 
double Voltage = 0; //voltage measuring
double Amps = 0;// Current measuring

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;



void setup() 
{   
 Serial.begin(9600); 
 MyBlue.begin(9600); 
 pinMode(relay1, OUTPUT); 
 pinMode(relay2, OUTPUT); 
 pinMode(relay3, OUTPUT); 
 pinMode(relay4, OUTPUT); 
 pinMode(relay5, OUTPUT); 
 pinMode(relay6, OUTPUT); 
 pinMode(relay7, OUTPUT); 
 pinMode(relay8, OUTPUT); 
 digitalWrite(relay1, HIGH);
 digitalWrite(relay2, HIGH);
 digitalWrite(relay3, HIGH);
 digitalWrite(relay4, HIGH);
 digitalWrite(relay5, HIGH);
 digitalWrite(relay6, HIGH);
 digitalWrite(relay7, HIGH);
 digitalWrite(relay8, HIGH);
 Serial.println("Ready to connect\nDefualt password is 1234 or 0000"); 
} 

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (MyBlue.available() > 0 && newData == false) {
        rc = MyBlue.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 showNewData() {
  if (newData == true) {
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    newData = false;
  }
}

void loop() 
{ 

recvWithStartEndMarkers();



 if (strcmp(receivedChars, "Front Beacon") == 0)
 { 
   if(r1==0){
      digitalWrite(relay1, LOW); 
      Serial.println("Beacon Front On"); 
      MyBlue.println("Front Beacon On");
      r1=1;
      newData = false;      
   }else{
       digitalWrite(relay1, HIGH); 
       Serial.println("Beacon Front Off"); 
      MyBlue.println("Front Beacon Off");
      r1=0; 
      newData = false;
   }
 }else if (strcmp(receivedChars, "Off") == 0) 
 { 
   digitalWrite(relay1, HIGH); 
   digitalWrite(relay2, HIGH); 
   digitalWrite(relay3, HIGH); 
   digitalWrite(relay4, HIGH); 
   digitalWrite(relay5, HIGH); 
   digitalWrite(relay6, HIGH); 
   digitalWrite(relay7, HIGH); 
   digitalWrite(relay8, HIGH); 
   Serial.println("System Off"); 
   MyBlue.println("System Going To Sleep"); 
   r1=0;
   r2=0;
   r3=0;
   r4=0;
   r5=0;
   r6=0;
   r7=0;
   r8=0;
   newData = false;
 }else if (strcmp(receivedChars, "Rear Beacon") == 0) 
 { 
  if(r2==0){
      digitalWrite(relay2, LOW); 
      Serial.println("Beacon Rear On");
       MyBlue.println("Rear Beacon On");
       r2=1;
      newData = false;
   }else{
       digitalWrite(relay2, HIGH); 
       Serial.println("Beacon Rear Off"); 
      MyBlue.println("Rear Beacon Off");
      r2=0; 
      newData = false;
   }
 }
}

of course with the command looping it just constantly triggers the relay on and off every second which i am sure is not good for relays :slight_smile:

In this code (from Reply #9)

void loop() 
{ 

recvWithStartEndMarkers();

 if (strcmp(receivedChars, "Front Beacon") == 0)
 {

you are checking the content without first checking if a new message has been received. Try it like this

void loop() 
{ 
    recvWithStartEndMarkers();
    if (newData == true) {

         if (strcmp(receivedChars, "Front Beacon") == 0)
         {

...R

Robin2:
In this code (from Reply #9)

void loop() 

{

recvWithStartEndMarkers();

if (strcmp(receivedChars, "Front Beacon") == 0)
{



you are checking the content without first checking if a new message has been received. Try it like this


void loop()
{
    recvWithStartEndMarkers();
    if (newData == true) {

if (strcmp(receivedChars, "Front Beacon") == 0)
        {





...R

i dont know how i missed that one :slight_smile: this whole being stuck in isolation must be effecting my memory :slight_smile: that appears to of fixed the issue.

so for my next question which way would people advise i power this from said 12v batteries, will the arduino handle a direct 12-15v input or am i safer to run it through a power supply (i have a buck/boost converter spare that can go from upto 30v down to whatever voltage at upto 5 amps.) for safety?

HI dmpdiscos,l

the specs says inputvoltage 7-12V. The onboard voltage-regulator is a linear-line-regulator which means he has to burn up all the power to heat Power-Dissipation (12V - 5V) * Current-consumption of the Arduino

For bigger currents you might even have to cool the onboard-regulator
So I would go with the buck-converter which has a high efficiency. Your battery will last longer.

best regards

Stefan

StefanL38:
HI dmpdiscos,l

the specs says inputvoltage 7-12V. The onboard voltage-regulator is a linear-line-regulator which means he has to burn up all the power to heat Power-Dissipation (12V - 5V) * Current-consumption of the Arduino

For bigger currents you might even have to cool the onboard-regulator
So I would go with the buck-converter which has a high efficiency. Your battery will last longer.

best regards

Stefan

thats what i suspected thanks. though battery life isnt really going to be an issue for me i doubt.

If you are powering the Arduino from a vehicle's 12v (nominal) power supply I suggest you use a regulator to produce 5v and power the Arduino using the 5v connected to its 5v pin. A 7805 voltage regulator will work or a USB charger designed for a vehicle.

...R

Robin2:
If you are powering the Arduino from a vehicle's 12v (nominal) power supply I suggest you use a regulator to produce 5v and power the Arduino using the 5v connected to its 5v pin. A 7805 voltage regulator will work or a USB charger designed for a vehicle.

...R

well arduino will be in a trailer connected to trailers batteries (which would be charging from car when connected of course- but is their any difference in running arduino from 5v via barrell jack or 5v pin ?

dmpdiscos:
but is their any difference in running arduino from 5v via barrell jack or 5v pin ?

It won't work with 5v applied to the barrel jack. The jack needs a minimum of 6v and a recommended 7v to 12v

...R

Robin2:
It won't work with 5v applied to the barrel jack.

I can testify to that: when I first bought an Uno I saw "5V" and "barrel jack" in the docs but didn't realise they don't work together until I bought a 5V wall wart with a connector for the jack.

So it became a nice power source for servos and the like, with an adapter.

Robin2:
It won't work with 5v applied to the barrel jack. The jack needs a minimum of 6v and a recommended 7v to 12v

...R

ok thats handy to know, though specs should really make that clearer :), lickily its just screw terminals on my buck/boost so easy to move the wiring around to fix that one, will 5v via the 5v pin also use less power then if it bypasses some of the voltage regulator

dmpdiscos:
will 5v via the 5v pin also use less power then if it bypasses some of the voltage regulator

You will probably have a different voltage regulator to produce the 5v and that will have some level of inefficiency.

...R