Converting VirtualWire from Timer1 to Timer2

I'm trying to get VirtualWire and TVOut to work on one Uno, but they both use Timer1. So I'm trying to convert VirtualWire to Timer2, without succes. Also it seems others have tried, but also failed: Relevant stackexchange question

This is the relevant code in VirtualWire.cpp:

// Speed is in bits per sec RF rate
void vw_setup(uint16_t speed)
{
 
    Serial.println("First");
    
    uint16_t nticks; // number of prescaled ticks needed
    uint16_t prescaler; // Bit values for CS0[2:0]

    // This is the path for most Arduinos
    // figure out prescaler value and counter match value
    prescaler = _timer_calc(speed, (uint16_t)-1, &nticks); 
    if (!prescaler)
    {
        return; // fault
    }

    Serial.print("Second, prescaler: ");
    Serial.println(prescaler, DEC);
    Serial.print("nticks: ");
    Serial.println(nticks, DEC);
    
    TCCR1A = 0; // Output Compare pins disconnected
    TCCR1B = _BV(WGM12); // Turn on CTC mode

    Serial.println("Third");

    // convert prescaler index to TCCRnB prescaler bits CS10, CS11, CS12
    TCCR1B |= prescaler; // was prescaler
    
    Serial.println("Fourth");

    // Caution: special procedures for setting 16 bit regs
    // is handled by the compiler
    OCR1A = nticks;
    // Enable interrupt
    
    Serial.println("Fifth");

    // atmega168
    TIMSK1 |= _BV(OCIE1A);
    
    Serial.println("Sixth");

    // Set up digital IO pins
    pinMode(vw_tx_pin, OUTPUT);
    pinMode(vw_rx_pin, INPUT);
    pinMode(vw_ptt_pin, OUTPUT);
    digitalWrite(vw_ptt_pin, vw_ptt_inverted);
}

And of course the ISR:

SIGNAL(TIMER1_COMPA_vect)

This is the code in my sketch that calls vw_setup():

  Serial.println("Setup receiver");
  Serial.flush();
  
  vw_set_ptt_inverted(true);
  vw_set_rx_pin(RX_PIN);
  vw_setup(2000);
  vw_rx_start();  
  
  Serial.println("End setup receiver");
  Serial.flush();

This works as expected (it receives the right messages), and it prints:

Setup receiver
First
Second, prescaler: 1
nticks: 1000
Third
Fourth
Fifth
Sixth
End setup receiver

So then I try to convert it to use Timer2. I've replaced 1's by 2's and the max_ticks is now

(uint8_t)-1

instead of

(uint16_t)-1

(because Timer1 is 16 bit, while Timer2 is 8 bit).
I ended up with the following code:

// Speed is in bits per sec RF rate
void vw_setup(uint16_t speed)
{
 
    Serial.println("First");
    
    uint16_t nticks; // number of prescaled ticks needed
    uint16_t prescaler; // Bit values for CS0[2:0]

    // This is the path for most Arduinos
    // figure out prescaler value and counter match value
    prescaler = _timer_calc(speed, (uint8_t)-1, &nticks); // changed to (uint8_t)-1 from (uint16_t)-1
    if (!prescaler)
    {
        return; // fault
    }

    Serial.print("Second, prescaler: ");
    Serial.println(prescaler, DEC);
    Serial.print("nticks: ");
    Serial.println(nticks, DEC);
    
    TCCR2A = 0; // Output Compare pins disconnected
    TCCR2B = _BV(WGM22); // Turn on CTC mode

    Serial.println("Third");

    // convert prescaler index to TCCRnB prescaler bits CS10, CS11, CS12
    TCCR2B |= prescaler; // was prescaler
    
    Serial.println("Fourth");

    // Caution: special procedures for setting 16 bit regs
    // is handled by the compiler
    OCR2A = nticks;
    // Enable interrupt
    
    Serial.println("Fifth");

    // atmega168
    TIMSK2 |= _BV(OCIE2A);
    
    Serial.println("Sixth");

    // Set up digital IO pins
    pinMode(vw_tx_pin, OUTPUT);
    pinMode(vw_rx_pin, INPUT);
    pinMode(vw_ptt_pin, OUTPUT);
    digitalWrite(vw_ptt_pin, vw_ptt_inverted);
}

And the ISR:

SIGNAL(TIMER2_COMPA_vect)

But this doesn't work. It compiles just fine and it logs the following:

Setup receiver
First
Second, prescaler: 2
nticks: 125
Third
Fourth
Fifth
Sixth
End setup receiver

But it doesn't receive any messages from the TX any more.
Maybe I've forgotten something? I would really appreciate any help!

I think they will not work together on an Arduino Uno. You could add an other Arduino board for VirtualWire (I have a Pro Mini just for a VirtualWire receiver) and connect both boards via serial or I2C.

The VirtualWire uses the timer interrupt, and that is timing critical. With the TVOut also running, it might not receive very well.
The TVOut wants the complete Uno. I doubt if there is enough time for the VirtualWire.

The 433MHz receivers have an automatic gain. When there is nothing being transmitted, the automatic gain starts amplifying the noise. The VirtualWire is trying to detect the start pulses in that noise.
That means that the VirtualWire is always busy, regardless if something is being transmitted or not.

Thank you for the info! I guess I shall then add another 328p to the circuit.

Turns out I2C doesn't work well with TVOut either.
If found some information on this here.

The Wii nunchuk communicates using I2C and it is a bit tricky to use I2C in conjunction with TVout since the TVout library does not work well if hardware interrupts occur during the rendering of the screen. This can be solved by only communicating with the nunchuk during the vertical blanking interval, or VBI. The VBI is the period of time when the television is scanning lines below the picture and retracing the scan line back to the top of the screen. There are quite a few scan lines that are not rendered on the active screen, so it gives us enough time to communicate with the nunchuk device (which takes over 1 millisecond).

Edit: according to this page TVOut comes with pollserial, which is an alternative to the default, interrupt driven, I2C and serial.

This uses I2C, but it is VGA :confused:
http://www.gammon.com.au/forum/?id=11608