Interrupt and VirtualWire

Hi Guys, from your point of view is possible use the interrupt in this way :

In the loop put Arduino in RX mode and when the button or keypad is pressed TX a message ,

in this way i can syncronize tx and rx without particular issues ... thanks for the information,

Andrea

Put the Arduino in Rx mode? You've lost me.

So, of course is not arduino in rx mode but ...

pushing keypad raise up the interrupt, inside the interrupt routing tx data, then exit from the interrupt and in the loop Rx in this way I've separation between the 2 fases,

please let me know is are necessary more information,

Andrea

More information? Post your code please.

I didn’t wrote anything again regarding it because I know the part related to virtual wire but i didn’t know the part related to the interrupt, i mean …so for example I’m using this :

For example could you use in this way ? below reported the code … so i know that the interrupt is on the digital pin 2 but is used also from the keypad could be an issues ? below you can find the code …

Thanks for the support,

#include <VirtualWire.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

int pin = 13;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, tx, CHANGE);
  vw_setup(1200);	 // Bits per sec
}

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

    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	int i;

        digitalWrite(13, true); // Flash a light to show received good message
	// Message with a good checksum received, dump it.
	Serial.print("Got: ");
	
	for (i = 0; i < buflen; i++)
	{
	    Serial.print(buf[i], HEX);
	    Serial.print(" ");
	}
	Serial.println("");
        digitalWrite(13, false);
    }
}

void tx()
{
    char msg = keypad.getKey();
    
    digitalWrite(13, true); // Flash a light to show transmitting
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite(13, false);
    detachInterrupt(0)
}

You should not be waiting in an interrupt:

void tx()
{
 ...
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx(); // Wait until the whole message is gone
...
}

Instead set a flag and test it in loop:

volatile boolean txWanted;
void tx()
{
  txWanted = true;
}

Good Morning Nick, first thanks you for your reply ... sorry if maybe i did a simple question for you ... so, you mean that is not necessary raise up the interrupt for that ?

But I didn't undestard the logical of the code, you declare a volatile boolean named txWanted (for check if want tx or not ?), then you assign the value true to this variabile inside a routines ... I've loss some pieces maybe ...

Thanks Andrea

Yes, and then inside loop() you test to see if that flag is set.

Ok then should be like this :-) ...

I can test directly when i push the keypad in this way

 char key = keypad.getKey();

  if (key != NO_KEY){
    tx etc etc 
else

  rx etc etc 
  }

correct ? thanks and warm regards :-) Andrea