Timing and Multi-Taksing: PWM, Serial Coms, Input Capture, Output Compare

Hello Arduino enthusiasts!
I am a college student in my final term and working on my final project. I am new to the world of arduino and the ATmega328p, however have worked with the HCS12 in some past classes so programming MCUs is not entirely new to me.

A quick overview of my project is such:
Control the speed of two 12v Brush DC motors with PWM and H-bridge design, each motor has a Hall Effect encoder. Pololu #1443 1:29 gear ratio. The control is coming from a PC running a VisualBasic.net Windows Application connected through USB to the Arduino - UNO, and eventually wirelessly with Zigbee transceivers.

Measuring the encoder signal is achieved with an input capture routine which counts the number of pulses in a 0.1s interval. The 0.1s interval is achieved with an output compare configured with Timer1:

void TIMER1_init(void)
{       
    cli();          // disable global interrupts
    TCCR1A = 0;     // set entire TCCR1A register to 0
    TCCR1B = 0;     // same for TCCR1B
    // set compare match register to desired timer count:
    OCR1A = 1562;
    // turn on CTC mode:
    TCCR1B |= (1 << WGM12);
    // Set CS10 and CS12 bits for 1024 prescaler:
    TCCR1B |= (1 << CS10);
    TCCR1B |= (1 << CS12);
    // enable timer compare interrupt:
    TIMSK1 |= (1 << OCIE1A);
    sei();        // enable global interrupts:
}

I have taken the TIMER1_init() from an example found online and have modified it slightly to achieve my 0.1s interval and it worked like a charm to measure the number of pulses (Rising edges only) to obtain the encoder frequency.

Now I am faced with a dilemma, I have tried to incorporate two PWM outputs to control the brightness of LEDs (later on, the motors) which I have done on it’s own without the input capture and output compare measurements. But when I put it all together, as soon as I increase the value of the PWM above zero, my output compare timing basically conks out for a second or two, and decreases it’s interval to ~0.01s. I have the feeling that the PWM i am using on pin 9 and 10 on the arduino is using timer1 which is the same timer i want to do output compare with to achieve my 0.1s interval.

I have been reading the ATmega328P data sheets to understand which functions are using which timers, but to no avail. I have found several tutorials on PWM generation, Timer0,1,2 operations, but i fail to understand all the cross functionality of the timers with output compare, input capture, and especially PWM generation.

What I really want to know is this:
If I want to have PWM on two pins, an Input Capture ISR on two pins, and a 0.1s Output compare timer happening behind the scenes, as well as serial communications (SPI): What is my optimal configurations with timers and I/O pins? Which timer is PWM on pin 9 and 10 using(I am not restriced to pins 9-10). should I switch to different PWM pins?
One restriction I do know is that the two Input capture routines MUST occur on pins 2 and 3 with the UNO.
I am having trouble understanding the Pin mapping on the 328 datasheet, and how the arduino libraries use each pin for each function such as PWM and timing.

My full code is as follows:

 (code tags added by moderator, color tagging is hosing this output tho)
[color=#7E7E7E]//NOT WORKING - conflict with TIMER1 and PWM generation[/color]

[color=#7E7E7E]// avr-libc library includes[/color]
#include <avr/io.h>
#include <avr/interrupt.h>

#define pinIn 0 [color=#7E7E7E]//PIN 2 ON ARDUINO[/color]
#define pinIn2 1[color=#7E7E7E]//PIN 3 ON ARDUINO[/color]
#define LEDPIN 5[color=#7E7E7E]//PIN 5 ON ARDUINO[/color]
#define PWMPIN1 9[color=#7E7E7E]//PIN 9 ON ARDUINO[/color]
#define PWMPIN2 10[color=#7E7E7E]//PIN 10 ON ARDUINO[/color]

volatile [color=#CC6600]int[/color] Timer1 = 0; [color=#7E7E7E]//Output compare timer[/color]
volatile [color=#CC6600]int[/color] countx = 0;[color=#7E7E7E]//PULSE COUNTER1[/color]
volatile [color=#CC6600]int[/color] county = 0;[color=#7E7E7E]//PULSE COUNTER2[/color]

const [color=#CC6600]char[/color] STX = 2;   
const [color=#CC6600]char[/color] ETX = 3;
[color=#CC6600]byte[/color] buffer[100];    [color=#7E7E7E]//serial read buffer[/color]
[color=#CC6600]int[/color] i = 0;

[color=#CC6600]void[/color] TIMER1_init([color=#CC6600]void[/color]);

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()
{
    [color=#CC6600]pinMode[/color](LEDPIN, [color=#006699]OUTPUT[/color]);
    [color=#CC6600]pinMode[/color](PWMPIN1, [color=#006699]OUTPUT[/color]);
    [color=#CC6600]pinMode[/color](PWMPIN2, [color=#006699]OUTPUT[/color]);
    
    [color=#7E7E7E]// initialize Timer1[/color]
    TIMER1_init();
    [color=#CC6600]attachInterrupt[/color](pinIn, timeread, [color=#006699]RISING[/color]);    [color=#7E7E7E]//interrupt routine to calculate period[/color]
    [color=#CC6600]attachInterrupt[/color](pinIn2, timeread2, [color=#006699]RISING[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);   
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"\tTime\tSample1\tSample2"[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]flush[/color]();
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()
{
    [color=#CC6600]if[/color] ([color=#CC6600][b]Serial[/b][/color].[color=#CC6600]available[/color]()>0) [color=#7E7E7E]//read the serial [/color]
    {  [color=#7E7E7E]// read the most recent byte [/color]
       buffer[i] = [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]read[/color]();
       i++;
    }
    [color=#CC6600]if[/color](buffer[i-1] == ETX) [color=#7E7E7E]//check if last character rec'd was ETX[/color]
    {
        [color=#CC6600]if[/color](buffer[0] == STX && buffer[i-1] == ETX)[color=#7E7E7E]//check for complete message[/color]
        {
           [color=#CC6600]analogWrite[/color](PWMPIN1, [color=#CC6600]byte[/color](buffer[1])); [color=#7E7E7E]//assign PWM Values[/color]
           [color=#CC6600]analogWrite[/color](PWMPIN2, [color=#CC6600]byte[/color](buffer[2]));       
        }
        i = 0;
    }
    [color=#CC6600]if[/color] (Timer1 >=1) [color=#7E7E7E]//Print the data to VB every 0.1s[/color]
    {
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]write[/color](2);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]write[/color](9);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#CC6600]millis[/color]()/100);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]write[/color](9);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](countx);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]write[/color](9);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](county);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]write[/color](9);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]write[/color](3);
            [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]();
            Timer1 = 0;
            county = countx = 0;
    }
    
}

[color=#7E7E7E]// Interrupt Service Routine attached to INT0 vector[/color]
[color=#CC6600]void[/color] timeread()
{
    countx++;
}
[color=#CC6600]void[/color] timeread2()
{
    county++;
}
[color=#7E7E7E]//output compare ISR to time 0.1sec[/color]
ISR(TIMER1_COMPA_vect)
{
    Timer1++; [color=#7E7E7E]//counts every 0.1 sec[/color]
    [color=#CC6600]digitalWrite[/color](LEDPIN, ![color=#CC6600]digitalRead[/color](LEDPIN));[color=#7E7E7E]//flashing led on 5 times per sec[/color]
            
}
[color=#CC6600]void[/color] TIMER1_init([color=#CC6600]void[/color])
{       
    cli();          [color=#7E7E7E]// disable global interrupts[/color]
    TCCR1A = 0;     [color=#7E7E7E]// set entire TCCR1A register to 0[/color]
    TCCR1B = 0;     [color=#7E7E7E]// same for TCCR1B[/color]
    [color=#7E7E7E]// set compare match register to desired timer count:[/color]
    OCR1A = 1562;
    [color=#7E7E7E]// turn on CTC mode:[/color]
    TCCR1B |= (1 << WGM12);
    [color=#7E7E7E]// Set CS10 and CS12 bits for 1024 prescaler:[/color]
    TCCR1B |= (1 << CS10);
    TCCR1B |= (1 << CS12);
    [color=#7E7E7E]// enable timer compare interrupt:[/color]
    TIMSK1 |= (1 << OCIE1A);
    sei();        [color=#7E7E7E]// enable global interrupts:[/color]
}

Other tidbits of useful information:
Encoder signal is ~2kHz with MAX f = ~2.8kHz
On start, PWM value is Zero on pin nine and ten
as soon as the PWM values are changed is when my problems start happening.

Any questions, comments, suggestions are greatly appreciated and may be payable with apple pie or cookies.

Just choose different pins for PWM: http://arduino.cc/playground/Main/TimerPWMCheatsheet

That worked perfectly. Thankyou, and the link is just what i was looking for