1

I am making a security system, and the alarm starts when the button gets pressed. If i turn the alarm off by the serial communication, and want to turn it on again using the button, it will only let the alarm go on for about 0.5 seconds. Does someone see the mistake i'm making, and can help me? Thanks in advance!

const int ledrood = 11;
const int ledgroen = 12;
const int buttonPin = 2;
int buttonState = LOW;

int statePin = LOW;
const int trigPin = 9;
const int echoPin = 10;
long duration;
int distance;
bool alarm = false;
int speakerPin = 8;
String Message = "";
String NMessage = "";
String Command = "";
int index = 0;
bool Bericht = true;



void PlaySong()
{
  tone(speakerPin, 4699, 500);
  delay(1000);
  tone(speakerPin, 4699, 500);
  delay(1000);
  tone(speakerPin, 4699, 500);
  delay(1000);
  tone(speakerPin, 4699, 500);
  delay(1000);
  tone(speakerPin, 4699, 500);
  delay(1000);
  tone(speakerPin, 4699, 500);
  delay(1000);
  tone(speakerPin, 4699, 500);
  delay(1000);
}
void alarmaan()
{
 alarm = true;

  digitalWrite(ledrood, HIGH);
  digitalWrite(ledgroen, LOW);
  digitalWrite(trigPin, LOW);

 }

void AlarmUit() {
  Serial.println("alarm uit");
  alarm = false;
  digitalWrite(ledgroen, HIGH);
  digitalWrite(ledrood, LOW);
  digitalWrite(trigPin, LOW);

 }
void setup() {
  pinMode(ledrood, OUTPUT);
  pinMode(ledgroen, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600);
 }
String Doorsturen() //Methode om te communiceren via de serial monitor
{
 if (Serial.available() > 0)
  {
   char readChar = (char) Serial.read();
   NMessage = NMessage + readChar;
  }
  if (NMessage.startsWith("#") && NMessage.endsWith("%"))   
  {
   index = NMessage.indexOf("%");
   NMessage = NMessage.substring(1, index);
   Serial.println(NMessage);
   Command = NMessage;
   NMessage = "";
   Bericht = true;
  }
  else if (NMessage.endsWith("%"))
 {
   NMessage = "";
 }
 return Command;
}
void loop() {
 Message = Doorsturen();
 int buttonState = digitalRead(buttonPin);
 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = duration * 0.034 / 2;
 Serial.print("Distance: ");
 Serial.println(distance);


 if (alarm == false) {
   digitalWrite(ledgroen, HIGH);
 }

 if (Message == "alarm_off")
 {
   AlarmUit();
   Message = "";
 }
  if (Message == "1234")
  {
   AlarmUit();
   Message = "";
  }
  if (Message == "aan") {
   alarmaan();
   Message = "";
   }




  if (alarm == false) {
   if (buttonState == HIGH) {
    delay(50);
    while (digitalRead(buttonPin) == HIGH) {}
    alarmaan();
    Serial.println("fout");
   }
  }

  if (distance < 10 && alarm == true) {
   PlaySong();
  }

 if (alarm == true) {
   if (buttonState == HIGH) {
    delay(50);
    while (digitalRead(buttonPin) == HIGH) {}
    alarmaan();
    Serial.println("Test");
   }


 }
}
3
  • Please create a minimal, complete, verifiable example of the problem. Strip out all the extraneous stuff, tune playing and so on, and include only the code required to demonstrate the problem. You'll probably fix it yourself in the process, and if not, someone will have an easier time helping, and hence you're more likely to get useful help. – Mark Smith Jan 1 '18 at 19:55
  • Could you provide the serial output you are getting? How can you see that the alarm is only on for half a second? – Gerben Jan 1 '18 at 20:51
  • @Gerben Serial output is 'Distance: ...' + 'alarm uit', this keeps looping. I know the alarm is on for short because i see 'alarm on' (test in this case) only once and the red led (meaning the alarm is on) only flashes once, then goes back to green led (alarm off) – Dave Jan 1 '18 at 20:54
1

The problem is that Message = ""; doesn't actually do anything, because the next time doorsturen is called, it will just load the previous command from the command variable. I'd suggest having the doorsturen function only return the command once, and not on consecutive calls.

String Doorsturen() //Methode om te communiceren via de serial monitor
{
  Command = "";
  if (Serial.available() > 0)
  {
    char readChar = (char) Serial.read();
    NMessage = NMessage + readChar;

    if (NMessage.startsWith("#") && NMessage.endsWith("%"))   
    {
      index = NMessage.indexOf("%");
      NMessage = NMessage.substring(1, index);
      Serial.println(NMessage);
      Command = NMessage;
      NMessage = "";
      Bericht = true;
    }
    else if (NMessage.endsWith("%"))
    {
      NMessage = "";
    }
  }
  return Command;
}

see comment in the code

if (alarm == false) {
  if (buttonState == HIGH) {
    //here you set alarm to true
  }
}

...

if (alarm == true) { //now this if statement is true
  if (buttonState == HIGH) {//also buttonState is still high, because it's never updated after you set alarm to true

  }

to fix do this

if (alarm == false) {
   if (buttonState == HIGH) {
    delay(50);
    while (digitalRead(buttonPin) == HIGH) {}
    delay(50);// this is to negate switch bounce, on release, from interfering
    buttonState = false;// add this line
    alarmaan();
    Serial.println("fout");
   }
  }

3
  • This does not seem to work, the alarm 'on' only still works for about half a second, then it switches off again... – Dave Jan 1 '18 at 20:23
  • I think I found the problem. See my edited answer. – Gerben Jan 2 '18 at 18:56
  • It works! Thank you very much for the help, i really appreciate it. Have a nice day :) – Dave Jan 3 '18 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.