How to print to Serial only once in if statement without stopping main loop?

Hello guys. I designed a system for my room lamp with relay module.

There’s a LDR and PIR sensor for automatic mode. I’ve a IR remote control and it will be the main control unit. I’m controlling the system with this remote control.

There’s 3 command. “ON” “OFF” and “AUTOMATIC MOD”.

My problem is this : When I change the mods I want to create a buzzer beep in my program to give information for me. But in if statements, it’s beeping forever. I just want to make a beep sound once, without stopping the main loop. “ON” “OFF” or “AUTOMATIC MODE” should continue after beeping. How can I do this ? Thank you.

Here is my all code :

#include "IRremote.h"

int LDR = A0;
byte sensor = 11;
byte bulb = 12;
int val = 0;
int val2 = 0;
int RCV_PIN = 10;

IRrecv irrecv(RCV_PIN);
decode_results results;

void setup()
{
 pinMode(sensor,INPUT);
 pinMode(bulb,OUTPUT);
 digitalWrite(bulb, HIGH); //rölenin başlangıç durumu
 Serial.begin(9600);
 irrecv.enableIRIn();
}


void loop()  {

 val=analogRead(LDR);
 val = map(val,0,1024,0,100);

 if (irrecv.decode(&results)) { // have we received an IR signal?

       

   irrecv.resume(); // receive the next value
   delay (500);
 }  
 
 if (results.value == 0xFFE21D) {
   digitalWrite(bulb,LOW);
   Serial.println("ANA KUMANDA: sistem acik");
   
 }

 if (results.value == 0xFFA25D){
   digitalWrite(bulb,HIGH);
   Serial.println("ANA KUMANDA: sistem kapali");
   }

 if (results.value == 0xFF629D) {
   
   Serial.println("ANA KUMANDA: sistem otomatik modda");
 
 if (results.value !=0xFF629D){
 if (digitalRead(sensor) == HIGH && val<=50 ) {
   Serial.println("Hareket belirlendi !");
   digitalWrite(bulb,LOW);
   delay(1000);
   }
 else if (digitalRead(sensor) == LOW || val>50 ){
 digitalWrite(bulb,HIGH);
 }

}

else 

if (digitalRead(sensor) == HIGH && val<=50 ) {
   Serial.println("Hareket belirlendi !");
   digitalWrite(bulb,LOW);
   delay(1000);
   }
 else if (digitalRead(sensor) == LOW || val>50 ){
 digitalWrite(bulb,HIGH);
 }
}
}

versiyon4.ino (1.37 KB)

I think you need to look at the State Change example that comes with the Arduino IDE.

Rather than detecting the value received you need to detect the moment when it changes and only send the message / sound the buzzer when it changes.

...R

Please put your code between brackets “</>”. Below you see example code which prints only once.

Regards

int plaa;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); 
plaa=0;
}

void loop() {
  // put your main code here, to run repeatedly:
if (plaa==0)
{
    Serial.println("Print once");
    plaa=1;
}
    Serial.println("Fowever");
}

First of all thanks for your help. But actually I just want to create little "beep" sound when I change my mods without stopping new commands scanning in program. I couldn't modify your example to my program.