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
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