Tripwire project - problem with the alarm (sirene) and deactivation

Hello guys,

I have the following problem. I am working on a Project.
It’s a laser tripwire System (the laser is connected to another board, thus not in this Sketch).
It’s very simple: When you activate the System a LDR Module checks the light and when there is an obstacle the alarm starts. After entering the Password in terminal the System goes back to its initial state.

I have the mot important parts from the Sketch here (without the calibration): http://keithkay.com/2013/03/03/arduino-self-calibrating-laser-trip-wire/

There is only one Problem and I don’t know if its a Problem with Blynk or with my Sketch. I dont know how to describe it but sometimes after activating it with pressing the button in my Blynk App the alarm goes “crazy”. I just want it to play the sirene and the lights to blink when the alarm goes off as described in the Code. Most of the times this works great but sometimes for example when I wait some time the alarm becomes kind of a mix of Sirene and activation Sound and I am not able to deactivate it anymore with my Password. Immediately after entering the Password and deactivating the System it reactivates itself again. It seems like it permanently reactivates without pressing the button so the alarm and reactivation are parallely and cant be deactivated anymore. It only stops if I reset everything.

I dont know whats the Problem and how to solve it. And it kind of irritates me because it happens so rarely as I dont know the cause.

Most of it was in german but I translated it. If there are any questions or something is not clear please let me know. Here is the Code:



#define BLYNK_PRINT Serial
#define ESP8266_BAUD 19200

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <SoftwareSerial.h>

char auth[] = "x";
char ssid[] = "x";
char pass[] = "x";

SoftwareSerial EspSerial(11, 12); // RX, TX
ESP8266 wifi(&EspSerial);
WidgetTerminal terminal(V2);
BlynkTimer timer;
WidgetRTC rtc;

//Konstanten
const int redled = 3;           //Pin für rote LED
const int greenled = 4;         //Pin für grüne LED
const int ldrdigital = 10;   //Alternativ das Modul digital auslesen
const int speaker = 5;          //Pin für akkustischen Alarm
const int armbutton = 6;        //Pin für An- und Ausschalten des Systems
const int armbutton2 = 8;       //analog  
const int gelbled = 7;          //Pin für gelbe LED


//Variablen
boolean aktiv = true;       // Variable für Aktivierung
boolean alarm = false;      // Variable für Alarm
int buttonVal = 0;           // Variable um Buttonzustand festzustellen
int prev_buttonVal = 0;      // Variable um Buttonzustand zu vergleichen
int buttonVal2 = 0;          // Analog
int prev_buttonVal2 = 0;

// Konstanten für Sirene
const int lowrange = 2000;   // niedrigste Frequenz
const int highrange = 4000;  // höchste 

//Flags                 //Vermeiden von loop für Benachrichtigungen und Terminal
int flagAlarm = 0;        
int flagAktiv = 0;
int flagtime = 0;

//Strings
String password = "cool";

void setup()

{
  //Bestimmen der Output und Input Pins
  pinMode(redled,OUTPUT);
  pinMode(greenled,OUTPUT);
  pinMode(gelbled, OUTPUT);
  pinMode(speaker,OUTPUT);

  pinMode(ldrdigital, INPUT);
  pinMode(armbutton, INPUT);
  
  timer.setInterval(100L, data); //timer will run every sec 
  Serial.begin(19200);
  EspSerial.begin(ESP8266_BAUD);
  Blynk.begin(auth, wifi, ssid, pass);
  rtc.begin();
  setSyncInterval(10 * 60);
  
  Activate ();
  delay(10);

}

void loop()
{

  Blynk.run();
  timer.run();
}

void data()
{
  
  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + "." + month() + "." + year();

  if (flagtime == 0) { 
    delay(500);
    terminal.print (currentTime); 
    terminal.print (F("  -  "));
    terminal.println (currentDate);
    terminal.println (F("--------------------------------"));
    terminal.flush();
    flagtime = 1;
  }
  
  int buttonVal = digitalRead(armbutton);                     //liest Button aus und vergleicht
  if ((buttonVal == HIGH) && (prev_buttonVal == LOW)){
    Activate();                                             //Wenn Bedingung erfüllt wird das System ativiert
    if (flagAktiv == 0){
       terminal.println(F("You activated the system!"));
       terminal.flush();
       flagAktiv = 1;  
    }
    delay(50);
  }
  
  int digitalValue = digitalRead(ldrdigital);                   //reads the ldr module
  if ((aktiv) && (digitalValue == HIGH)){                   //activates the alarm
    alarm = true;
  }
  
  if (alarm){
    if (flagAlarm == 0){
       Blynk.notify("Alarm!");
       terminal.print(F("The Alarm was activated "));
       terminal.print (currentTime);
       terminal.println(F("Type in your password!"));
       terminal.flush();
       flagAlarm = 1;  
    }
    // Sirene
    for (int i = lowrange; i <= highrange; i++)
    {
      tone (speaker, i, 250);
    }
    for (int i = highrange; i >= lowrange; i--)
    {
      tone (speaker, i, 250);
    }

    // Blink
    digitalWrite(gelbled, HIGH);
    digitalWrite(redled, HIGH);
    digitalWrite(greenled, HIGH);
    delay(10);
    digitalWrite(greenled, LOW);
    digitalWrite(gelbled, LOW);
    digitalWrite(redled, LOW);
  }

    delay(20);
}

// Function activates the System
void Activate(){
  if (aktiv){
    digitalWrite(greenled, LOW);
    digitalWrite(redled, HIGH);
    alarm = false;
    aktiv = false;
  } else {
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, LOW);
    tone(speaker, 220, 125);
    delay(200);
    tone(speaker, 196, 250);
    aktiv = true;
  } 
}

//Verbindung mit Blynk 
BLYNK_CONNECTED() {
  Blynk.syncAll();
  terminal.clear();
  terminal.println(F("Blynk v" BLYNK_VERSION ": You are online."));
  terminal.flush();
}   


//Passwortschutz Terminal Blynk
BLYNK_WRITE(V2)
{

  if (alarm) {
     if (String (password) == param.asStr()){
         //deactivate alarm
          aktiv = false;
          alarm = false;
          flagAlarm = 0; 
          flagAktiv = 0;
          digitalWrite(greenled, LOW);
          digitalWrite(redled, HIGH);
          tone(speaker, 150, 200);
          delay(200);
          tone(speaker, 220, 125);
          terminal.println(F("Korrekt! Alarm deaktiviert."));
          terminal.flush();
        }
    else {
      terminal.println(F("Falsches Passwort!"));
      terminal.flush();
    }
  }
}


Since I only see a single Blynk Write Function in your code… I will assume that it is linked to this “button” you are referring to.

Whatever is happening in this Blynk Function is happening twice, once upon button press and once upon button release.

And this is pulling that parameter from the button state… this is not usually a string, but generally a 0 or 1 indicating the buttons state.

This is usually handled like this…

Blynk BLYNK_WRITE(V2) {
  if (param.asInt() == 1) {  // This param() gets the data or state of this associated Widget on V2
    // do something when button pressed
    // add in your other password checking if() processes here, but get the password from a previously assigned global variable, not the param() of this function.
  }
  // else do nothing
}

I’m not sure if what you say is still valid then but with the button I mean “const int armbutton = 6”, I have this one as a button connected to D6 in my Blynk App. And it is only used here:

  int buttonVal = digitalRead(armbutton);                     
  if ((buttonVal == HIGH) && (prev_buttonVal == LOW)){
    Activate();                                            
    if (flagAktiv == 0){
       terminal.println(F("You activated the system!"));
       terminal.flush();
       flagAktiv = 1;  
    }

So I use this button to activate it or to be precise to activate the function “Activate()”.
I don’t know how it would interfere with the Password lines.

The BLYNK_WRITE in my example is connected to the terminal (V2). I use this function to read the String I enter into the terminal and if it is the Password I declared earlier (“cool”) then it deactivates the alarm. I dont think it has anything to do with it because I didnt have this in my Sketch before as I was using a real button to deactivate it instead and I still had this Problem.

Maybe this is a mistake made by me but is it wrong to use the button like this? Do I have to use BLYNK_WRITE for using a button or connect it to a virtual pin?
I will try out to use it like you recommend and see if it changes anything.

Perhaps you need to clarify what you meen by the word “button” Since this is the Blynk forum, we tend to think along the lines of helping you with Blynk things… like a Virtual Button :stuck_out_tongue_winking_eye:

If however you are referring to a physical button and either monitoring that GPIO via a polling routine or hardware interrupt, then that is a whole other form of troubleshooting.

Google contact bounce to see one of the issues you are probably running into.

I think he has a button widget connected to a digital pin :grinning:
Not the way I’d do it, much better to have it connected to a virtual pin IMHO.

Pete.

Exactly. Sorry if I was not clear enough.

I connected it to a virtual pin as you guys recommended. Since then my problem did not occur anymore. However, it also didnt happen with my previous solution I posted in the starting post anymore. :smiley:
I am not sure if this really fixed it or it just didnt happen yet (as I mentioned before it happened very rarely). I’m afraid this wasnt the cause as the sound of the sirene is still not as clean as if I make a sketch looping the sirene only. But reason for this could also be the delays I have in my sketch.

The rarity of it occuring actually makes it not that much of a problem in reality but I have to present this project and with my luck I am afraid this will be the case then :slight_smile:

Until it happens again I will see this as solved and hope it won’t bother me anymore. Still, I would be very relieved if someone found the cause in my code or could say with certainty that the button is the cause.

Regarding a physical button… if you do NOT have any sort of anti bounce correction (code or circuit) then that is likely to be part, if not all, the issue… contact bounce can have your triggered action happening multiple times simultaneously.

Virtual buttons have absolutely NO contact bounce :wink: although a similar condition can happen if not accounting for press AND release each being a trigger for a function