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