As a title, i can control an analog pin in specific range.
I Have a V2 that enable or disable a command:
when V2 is enable, the analog pin A3 read the value and switch on or off digitalpin 10, when V2 is disable stop the control. Now work but in both position of V2. Why?
#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
char auth[] = "char_auth";
int btnSV0 = 0; //Stato pin Virtual 0
int btnSV1 = 0; //Stato pin Virtual 1
int btnSV2 = 0; //Stato pin Virtual 2
bool BTN1 = false;
bool BTN2 = false; //flags
int luminosita;
BlynkTimer timer;
WidgetLED led1(V3);
int relay;
void setup()
{
// Debug console
SwSerial.begin(9600);
Serial.begin(9600);
Blynk.begin(Serial, auth);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(10, OUTPUT);
pinMode (A3, LOW);
}
void readLight() {
if (analogRead(A3) > 500) {
digitalWrite(10, HIGH);
}
else {
digitalWrite(10, LOW);
}
}
BLYNK_CONNECTED() {
Blynk.syncAll();
}
void relay_giu() {
digitalWrite(relay, LOW);
}
BLYNK_WRITE(V2) {
BTN1 = true;
led1.on();
btnSV2 = param.asInt();
if (btnSV2 == 0) {
Blynk.virtualWrite(V0, LOW);
Blynk.virtualWrite(V1, LOW);
timer.setInterval(500L, readLight);
} else {
BTN1 = false;
led1.off();
}
}
BLYNK_WRITE(V0) { // Alza serranda 1
if (BTN1 == false) {
btnSV0 = param.asInt();
if (btnSV0 == 0)
{
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}
else
{
digitalWrite(4, LOW);
}
}
}
BLYNK_WRITE(V1) // Abbassa serranda 1
{
relay = 4;
if (BTN1 == false) {
btnSV1 = param.asInt();
if (btnSV1 == 0)
{
digitalWrite(4, HIGH);
digitalWrite(3, HIGH);
}
else
{
digitalWrite(3, LOW);
timer.setTimeout(150, relay_giu);
}
}
}
void loop()
{
Blynk.run();
timer.run();
}