BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How can i get segment switch to work?

I have an basement alarm system. I want to trigger a siren when my PIR sensor detects movement. That part works fine.

But i want to have the ability to turn the siren OFF with segment switch if the alarm is triggered.

This is what i want:
Look at the screenshot from my Blynk app. In the bottom i have an segment switch attached to pin V2. “Alarm PÅ” = siren is on and motion has been detected.
“Alarm AV” = siren is OFF.

I want the segment switch to show “Alarm PÅ” when siren is active and “Alarm OFF” when siren is deactivated.

I also want the opportunity to be able to press the “Alarm OFF” button to tunr off the siren when it is on and turn on the siren when pressing “Alarm PÅ”.

How can i do that?

Check my code under, and screenshot from my app.

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

int siren = 18;
int pirSensor = 4;
int orangeLED = 27;
int activatedLED = 26;

int alarmStatus;

int pirValue = 0;

//---SET THE AMOUNT OF LED BLINK ON STARTUP--------------

int LEDblink = 5;
int fastBlink = 10;

//-------------------------------------------------------

//---WIFI CRIDENTIALS FOR CONNECTING TO BLYNK SERVER-----

char auth[] = "*****";

char ssid[] = "*****";
char pass[] = *****";

//-------------------------------------------------------

//---DECLARATION OF BLYNK TIMER -------------------------

BlynkTimer timer;

//-------------------------------------------------------

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(siren, OUTPUT);
  pinMode(pirSensor, INPUT);
  pinMode(orangeLED, OUTPUT);
  pinMode(activatedLED, OUTPUT);
  digitalWrite(siren, LOW);
  Sequence();
  timer.setInterval(100L, alarm);
}

BLYNK_CONNECTED() {
  Blynk.syncVirtual(V2);
}


BLYNK_WRITE(V2) {
  switch (param.asInt()) {
    case 1: {
        if (alarmStatus == 1) {
          Blynk.virtualWrite(V2, 1);
          break;
        }
      }
    case 2: {
        if (alarmStatus == 0) {
          digitalWrite(siren, LOW);
          Blynk.virtualWrite(V2, 2);
          break;
        }
      }
  }
}
void Sequence() {
  for (int i = 0; i < LEDblink; i++) {
    digitalWrite(orangeLED, HIGH);
    delay(1000);
    digitalWrite(orangeLED, LOW);
    delay(1000);
  }
  for (int b = 0; b < fastBlink; b++) {
    digitalWrite(orangeLED, HIGH);
    delay(150);
    digitalWrite(orangeLED, LOW);
    delay(150);
  }
  digitalWrite(activatedLED, HIGH);
  digitalWrite(orangeLED, LOW);
  alarm();
}

void alarm() {
  pirValue = digitalRead(pirSensor);
  Serial.println(pirValue);

  if (pirValue == 1) {
    digitalWrite(siren, HIGH);
    Blynk.virtualWrite(V1, "ALARM AKTIVERT!");
    alarmStatus = 1;
    timer.setTimeout(10000L, autoOFF);
  }
}

void autoOFF() {
  digitalWrite(siren, LOW);
  Blynk.virtualWrite(V1, "  ");
  alarmStatus = 0;
}

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