Rf 433mhz emitter-receiver 4 led - 4 push button

Hi everyone,
I am new here and also new in arduino/programming, so please be gentle with me :slight_smile:

I am stuck with a sketch for a rf433mhz emitter-receiver that has 4 buttons on the emitter side and 4 leds and a buzzer on the receiver side. Each button has a corresponding led with differrent color. I would like it to do soething like this: when press a button on the emitter side, an led should light up on the receiver side and buzz for 3 times and the led remain on for 15s. What I did actually works and does this for all 4 buttons with their coresponding leds. But, here is the problem i"m facing, I would also like for it to stop what it's doing if the button is pressed again or any other button is pressed and do the new command. Is this even possible. Here is what I did(modified some sketches that I found around here):

Emitter side

#include <VirtualWire.h>
char *controller;
void setup() {
  pinMode(2,INPUT);
  pinMode(3,INPUT);
  pinMode(4,INPUT);
  pinMode(5,INPUT);
vw_set_ptt_inverted(true); //
vw_set_tx_pin(12);
vw_setup(2000);// speed of data transfer Kbps
}
 
void loop(){
if(digitalRead(2)== 0){
controller="2"  ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
}

if(digitalRead(3)== 0){
controller="3"  ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
}

if(digitalRead(4)== 0){
controller="4"  ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
}

if(digitalRead(5)== 0){
controller="5"  ;
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone
}
}

Receiver side

#include <VirtualWire.h>
int Buzzer = 6;
int tones[] = {261, 277, 293, 311, 329, 349, 369, 392, 415, 440, 466, 493, 523 ,554};
uint8_t dely=0;
void setup()
{
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_set_rx_pin(12);
    vw_setup(2000);  // Bits per sec
    pinMode(Buzzer, OUTPUT);
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);

    vw_rx_start();       // Start the receiver PLL running
}
    void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
      if(buf[0]=='2'){  // button 2 ise
                          
    digitalWrite(2,1);    // LED2 HIGH
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(100);
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(100);
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(50);
    digitalWrite(2,1);
    delay(15000);
    digitalWrite(2,0);
       }                 
else 
      {
        digitalWrite(2,0);
        digitalWrite(6,0);
      }
                     
      if(buf[0]=='3'){ // button 3 ise

   digitalWrite(3,1); // led 3 HIGH
   digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(100);
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(100);
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(50);
    digitalWrite(3,1);
    delay(15000);
    digitalWrite(3,0);
      }
else
      {
        digitalWrite(3,0);
        digitalWrite(6,0);
      }
       
      if(buf[0]=='4'){ // button 4 ise

   digitalWrite(4,1); // LED 4 HIGH
   digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(100);
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(100);
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(50);
    digitalWrite(4,1);
    delay(15000);
    digitalWrite(4,0);
      }
      else
      {
        digitalWrite(4,0);
        digitalWrite(6,0);
      }
     
      if(buf[0]=='5'){  // BUTTON 5 ise
   
   digitalWrite(5,1); // LED 5 HIGH
   digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(100);
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(100);
    digitalWrite(6,1);
    tone(Buzzer, tones[5]);
    delay(150);
    digitalWrite(6,0);
    noTone(Buzzer);
    delay(50);  
    digitalWrite(5,1);
    delay(15000);
    digitalWrite(5,0);
      }
       else 
      {
        digitalWrite(5,0);
        digitalWrite(6,0);
      }
        }
          }

Any ideea how I can do what I want?
Thanks!

I would also like for it to stop what it's doing if the button is pressed again

this is a common feature

you can add a state variable indicating whether the thing is active or not. when the receiver receives the command, it checks the state variable. If it's off, then do something, and if on, stop doing something. In both cases, change the state values to reflect what is happening.

with all those delays, you'll probably next ask "how can I stop it before it completes"?

Thanks for your quick answer!

You misunderstood what I want. I don't want it to stop when the button is pressed again.
I want it to stop before it completes the task if a new command is received(button is pressed-any button) and do the new command(even if is the command is from the same button as the first time).

I hope I made myself clear this time.

It might help if you structure you code with some functions...

void StartThing1(void)
{
   //blah...
}

void StopThing1(void)
{
   //blah...
}

And then in your receiver code...

if(buf[0]=='1'){  // button 1 ise
     StopThing2();
     StopThing3();
     StartThing1();
} else if(buf[0]=='2'){  // button 2 ise
     StopThing1();
     StopThing3();
     StartThing2();
}  else .....

Edit: And you'll also want to read up on...

How to do multiple things at once *
Another thread about doing multiple things "at the same time".

State machines *
Discussion about "state machines" which are ways of managing complex logic situations easily, by Nick Gammon.

State machines *
Another tutorial about state machines, by Mike Cook.

scatiu:
I want it to stop before it completes the task if a new command is received(button is pressed-any button) and do the new command(even if is the command is from the same button as the first time).

you need to get rid of the delays, use millis() to track the passage of time, and use a state variable to keep track of which part of the sequence you are currently doing so that you can continually monitor for other commands

It sounds more difficult than I expected to be. I think I'll leave it like this and get used to it :frowning:
Thanks to all of you!