Combining two separate codes issues

BOARD : ARDUINO UNO
SHIELD : HC 05 BLUETOOTH MODULE

This is my first code , which is meant to read the digital pins of arduino , say pin11 and 10 and write the value to pin13 . i interfaced the bluetooth module and saw the program working fine and dislayed the necessary string like “over voltage” or " normal " in lcd widget .

int relay=12;
int inpin_ru=11;
int inpin_ro=10;
int val_ru=0;
int val_ro=0;

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
    
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>


char auth[] = "523d3815fd454238899b9e9ce5aa1dff";
SoftwareSerial SerialBLE(2, 3); // RX, TX
WidgetTerminal terminal(V0);

BlynkTimer timer;
void myTimerEvent()
{
  val_ru = digitalRead(inpin_ru);
  val_ro = digitalRead(inpin_ro);
  if (val_ru == 1)
  {
    Blynk.virtualWrite(V0,"R-UNDER VOLTAGE");
  }
  
  if (val_ro == 1)
  {
    Blynk.virtualWrite(V0,"R-OVER VOLTAGE");
  }
  if (val_ru == 0 && val_ro == 0)
  {
    Blynk.virtualWrite(V0,"NORMAL");
  }
  
}

void setup()
{ 
  Serial.begin(9600);
  pinMode(relay, OUTPUT);
  pinMode(inpin_ru, INPUT); 
  pinMode(inpin_ro, INPUT);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);

  Serial.println("Waiting for connections...");
  timer.setInterval(1000L,myTimerEvent);
  
}

void loop()
{
 val_ru = digitalRead(inpin_ru);
 val_ro = digitalRead(inpin_ro);

 
 digitalWrite(relay,val_ru||val_ro);
 Blynk.run();
 timer.run();
 
}

below is my second code to monitor current using ACS712 30A current sensor and the program worked fine when it ran separately in the arduino and displayed the current in super chart .

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
    
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>


char auth[] = "523d3815fd454238899b9e9ce5aa1dff";
SoftwareSerial SerialBLE(2, 3); // RX, TX

BlynkTimer timer;

const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module


double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void getACS712() 
{
 Voltage = getVPP();
 VRMS = (Voltage/2.0) *0.707; 
 AmpsRMS = (VRMS * 1000)/mVperAmp;
 Serial.print(AmpsRMS);
 Serial.println(" Amps RMS");

 Blynk.virtualWrite(V1 , String(AmpsRMS, 3));
}

float getVPP()
{
  float result;
  
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here
  
   uint32_t start_time = millis();
   while((millis()-start_time) < 3000) //sample for 1 Sec
   {
       readValue = analogRead(sensorIn);
       // see if you have a new maxValue
       if (readValue > maxValue) 
       {
           /*record the maximum sensor value*/
           maxValue = readValue;
       }
       if (readValue < minValue) 
       {
           /*record the maximum sensor value*/
           minValue = readValue;
       }
   }
   
   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;
      
   return result;
 }

 void setup()
{ 
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);

  Serial.println("Waiting for connections...");
  timer.setInterval(3000L, getACS712); 
  
}

void loop() 
{
  Blynk.run(); 
  timer.run();
}



Next when i tried to combine both programs into one , what happened is only the first program (sensing digital pins and writing in lcd) got worked , current part was idle . i dont know if its an issue with the timer or whatever ? below is the combined code .

int relay=12;
int inpin_ru=11;
int inpin_ro=10;
int val_ru=0;
int val_ro=0;

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
    
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>


char auth[] = "523d3815fd454238899b9e9ce5aa1dff";
SoftwareSerial SerialBLE(2, 3); // RX, TX

BlynkTimer timer;

const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module


double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void myTimerEvent()
{
  val_ru = digitalRead(inpin_ru);
  val_ro = digitalRead(inpin_ro);
  if (val_ru == 1)
  {
    Blynk.virtualWrite(V0,"R-UNDER VOLTAGE");
  }
  
  if (val_ro == 1)
  {
    Blynk.virtualWrite(V0,"R-OVER VOLTAGE");
  }
  if (val_ru == 0 && val_ro == 0)
  {
    Blynk.virtualWrite(V0,"NORMAL");
  }
  
}

void getACS712() 
{
 Voltage = getVPP();
 VRMS = (Voltage/2.0) *0.707; 
 AmpsRMS = (VRMS * 1000)/mVperAmp;
 Serial.print(AmpsRMS);
 Serial.println(" Amps RMS");

 Blynk.virtualWrite(V1 , String(AmpsRMS, 3));
}

float getVPP()
{
  float result;
  
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here
  
   uint32_t start_time = millis();
   while((millis()-start_time) < 3000) //sample for 1 Sec
   {
       readValue = analogRead(sensorIn);
       // see if you have a new maxValue
       if (readValue > maxValue) 
       {
           /*record the maximum sensor value*/
           maxValue = readValue;
       }
       if (readValue < minValue) 
       {
           /*record the maximum sensor value*/
           minValue = readValue;
       }
   }
   
   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;
      
   return result;
 }

 void setup()
{ 
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);

  pinMode(relay, OUTPUT);
  pinMode(inpin_ru, INPUT); 
  pinMode(inpin_ro, INPUT);

  Serial.println("Waiting for connections...");
  timer.setInterval(1000L,myTimerEvent);
  timer.setInterval(3000L, getACS712); 
  
}

void loop() 
{
  
 val_ru = digitalRead(inpin_ru);
 val_ro = digitalRead(inpin_ro);
 digitalWrite(relay,val_ru||val_ro);
 Blynk.run(); 
 timer.run();
}

please help ! i’m a noobie :slight_smile:

You have tried to include the software serial library twice (wouldn’t cause a problem but bad practise) and the other main issue I see is that you need to remove the SoftwareSerial SerialSW because 1. It’s not used and 2. You’re trying to initialise it with the same pins as your other software serial instance.

sir , please be more specific ! i didnt understand

Try this:

int relay=12;
int inpin_ru=11;
int inpin_ro=10;
int val_ru=0;
int val_ro=0;

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

//#include <SoftwareSerial.h>
//SoftwareSerial SwSerial(2, 3); // RX, TX
    
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>


char auth[] = "523d3815fd454238899b9e9ce5aa1dff";
SoftwareSerial SerialBLE(2, 3); // RX, TX

BlynkTimer timer;

const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module


double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void myTimerEvent()
{
  val_ru = digitalRead(inpin_ru);
  val_ro = digitalRead(inpin_ro);
  if (val_ru == 1)
  {
    Blynk.virtualWrite(V0,"R-UNDER VOLTAGE");
  }
  
  if (val_ro == 1)
  {
    Blynk.virtualWrite(V0,"R-OVER VOLTAGE");
  }
  if (val_ru == 0 && val_ro == 0)
  {
    Blynk.virtualWrite(V0,"NORMAL");
  }
  
}

void getACS712() 
{
 Voltage = getVPP();
 VRMS = (Voltage/2.0) *0.707; 
 AmpsRMS = (VRMS * 1000)/mVperAmp;
 Serial.print(AmpsRMS);
 Serial.println(" Amps RMS");

 Blynk.virtualWrite(V1 , String(AmpsRMS, 3));
}

float getVPP()
{
  float result;
  
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here
  
   uint32_t start_time = millis();
   while((millis()-start_time) < 3000) //sample for 1 Sec
   {
       readValue = analogRead(sensorIn);
       // see if you have a new maxValue
       if (readValue > maxValue) 
       {
           /*record the maximum sensor value*/
           maxValue = readValue;
       }
       if (readValue < minValue) 
       {
           /*record the maximum sensor value*/
           minValue = readValue;
       }
   }
   
   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;
      
   return result;
 }

 void setup()
{ 
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);

  pinMode(relay, OUTPUT);
  pinMode(inpin_ru, INPUT); 
  pinMode(inpin_ro, INPUT);

  Serial.println("Waiting for connections...");
  timer.setInterval(1000L,myTimerEvent);
  timer.setInterval(3000L, getACS712); 
  
}

void loop() 
{
  
 val_ru = digitalRead(inpin_ru);
 val_ro = digitalRead(inpin_ro);
 digitalWrite(relay,val_ru||val_ro);
 Blynk.run(); 
 timer.run();
}

didnt work !

How are you trying to display the data? Just on superchart? Or on terminal too?

just on superchart

Someone please be kind ! I know it’s wrong asking some others to do my job .I did so hard to get both these codes getting executed independently . But when it was combined , only the first part (digital pin based relay operation ) gets working . Current value is not lively plotted along with the first task .
I don’t know if it’s up with common timer issue… theres no issue regarding interfacing , nothing such …
Just the current part is not returning the value back

Which value is this?

If V1,

I don’t think you can plot a string value

1 Like

See , when I compiled that ACS code alone , I got the value Amprms value displaying in a super chart widget .

The same thing not happening in the combined code !

Please post the current combined code you are using.

1 Like

Third code from top !

Clearly mentioned all the three cases

  1. Digital pin sensing logic (code A )
  2. Acs current sensing part (code B)
  3. Combined both !

Did you take the advise of @JustBertC and remove the second software serial call?

Yes ! It didn’t work .

Sir actually I was thinking if my timer got mixed up. Can you please check that part ??

If possible the first code ( ie , digital pin sensing ) could remain active all time .
Let the current only return the value after some delay ?
How is it sir ?? Is it practical ?

So post your LATEST code,with the revisions that you’ve made, along with clear information about what appears in your serial monitor and in Blynk.

Pete.

2 Likes
int relay=12;
int inpin_ru=11;
int inpin_ro=10;
int val_ru=0;
int val_ro=0;

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(2, 3); // RX, TX
    
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>


char auth[] = "523d3815fd454238899b9e9ce5aa1dff";
SoftwareSerial SerialBLE(2, 3); // RX, TX

BlynkTimer timer;

const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module


double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void myTimerEvent()
{
  val_ru = digitalRead(inpin_ru);
  val_ro = digitalRead(inpin_ro);
  if (val_ru == 1)
  {
    Blynk.virtualWrite(V0,"R-UNDER VOLTAGE");
  }
  
  if (val_ro == 1)
  {
    Blynk.virtualWrite(V0,"R-OVER VOLTAGE");
  }
  if (val_ru == 0 && val_ro == 0)
  {
    Blynk.virtualWrite(V0,"NORMAL");
  }
  
}

void getACS712() 
{
 Voltage = getVPP();
 VRMS = (Voltage/2.0) *0.707; 
 AmpsRMS = (VRMS * 1000)/mVperAmp;
 Serial.print(AmpsRMS);
 Serial.println(" Amps RMS");

 Blynk.virtualWrite(V1 , String(AmpsRMS, 3));
}

float getVPP()
{
  float result;
  
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
  int minValue = 1024;          // store min value here
  
   uint32_t start_time = millis();
   while((millis()-start_time) < 3000) //sample for 1 Sec
   {
       readValue = analogRead(sensorIn);
       // see if you have a new maxValue
       if (readValue > maxValue) 
       {
           /*record the maximum sensor value*/
           maxValue = readValue;
       }
       if (readValue < minValue) 
       {
           /*record the maximum sensor value*/
           minValue = readValue;
       }
   }
   
   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;
      
   return result;
 }

 void setup()
{ 
  Serial.begin(9600);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);

  pinMode(relay, OUTPUT);
  pinMode(inpin_ru, INPUT); 
  pinMode(inpin_ro, INPUT);

  Serial.println("Waiting for connections...");
  timer.setInterval(1000L,myTimerEvent);
  timer.setInterval(3000L, getACS712); 
  
}

void loop() 
{
  
 val_ru = digitalRead(inpin_ru);
 val_ro = digitalRead(inpin_ro);
 digitalWrite(relay,val_ru||val_ro);
 Blynk.run(); 
 timer.run();
}

This one was a combined code . I know its illogical to combine things randomly.
The code dealing with sensing the digital pins worked , the current part never came back to super chart widget . If I’m right the serial monitor displayed current as Zero even though there was finite amount of current flowing which was previously measured using the same current sensor when it ran separately in arduino .

I’ve two areas of doubt !

  1. Serial (2 , 3) part is creating this problem since both tasks is being referred to this .

  2. Whether the timer part needs to corrected.

You were asked:

To which you replied:

Either this isn’t your latest code, or you didn’t take the advice given.

When we ask for your serial monitor output we expect to see the data copied and pasted (with backticks).

Pete.

Well he gave me a new code to execute, compiled it , now that my bluetooth module isn’t even connecting to blynk as it did before . So that approach was failure.

this is not taking a sample for 1 second, it is for 3 seconds. Which happens to be the same amount of time the timer is looping at. try increasing your timer interval, or decreasing the sampling time.

val_ru = digitalRead(inpin_ru);
 val_ro = digitalRead(inpin_ro);
 digitalWrite(relay,val_ru||val_ro);

this also doesn’t make sense to me. What if one pin is HIGH and the other is LOW? And it should not be in the loop().

Either pin 10 or 11 goes high = Pin 12 writes to high !

Which controls the relay !