Hi
great, really complex question. Composite video is an amazing pain in the ..um...
I used to build video edit suites, with miles of precisely cut cable and ten thousand dollar little boxes (time-base correctors) whose sole purpose was to correct the timing of the sync signals coming from VTR's. All that was so that you could get this thing called a "dissolve". A lot of executives at Sony bought a lot of hot tubs and fancy cars from that technology.
I am not sure you can really do what you want to do do anything else useful at the same time with the Arduino, as generating video has to be done constantly and very accurately, or the screen loses sync and the image disppears rolls, tears, etc etc. That's probably why most designs use an outboard video chip that is devoted to that job.
Anyway. I don't remember everything, but you are sort of on the right track. You have described the horizontal blanking interval, which is the time the beam is between drawing lines. In the HBI you've got the horizontal sync, and then some quiet time, and then the colour burst. There's something called the "breezeway" and the "back porch" in there too. (In fact, if you look closely, you can almost see abunch of old TV engineers in white lab coats sitting around in front of miles of tall grey rack mount gear, with their coffee cups perched on their Tektronix roll-around oscilloscopes.)
This standard has been around for a long time, and they have all kinds of cool names for each part of it. I'm assuming you will ignore the colour burst, as I doubt you could generate it in the Arduino (the colour is phase encoded onto a 3.58Mhz signal, which gets sent at the right end of the HBI for about 2.5 microseconds. Most Tv's will just default to B/W if the colour burst is there or minimally there, as this was the work-around, in NTSC at least, to make colour TV's receive stations that were transmitting B/W back when colour was introduced. Yes this stuff is that old.
There are two things you haven't mentioned: the colour standard and the vertical blanking interval. You need to find the specs for the standard waveform for wherever you are: NTSC (aka 'never twice same colour'), SECAM or PAL? The blanking intervals are different for each. Diferent countries use different standards.. it's all very political.
Next, you've got to have some code for the Vertical blanking interval, which includes the vertical sync. (This is probably why your code doesn't work-- you don't have a vertical blanking interval.) Vertical sync, If I remember correctly, is easy. They used to derive it by half-wave rectifying the power line frequency... so in Canada it's 60hz/2= 30hz, and in Europe it's 50hz/2= 25 hz. ( there are some finicky exceptions to this rule, as later on they started deriving the sync signal from the colour burst crystal at 3.58Mhz, and the closest they could get to 30 was 29.94, which led to a whhole industry in machines to convert between those rates in production houses) Anyway, the vertical sync has to be in the middle of the vertical blanking signal.
So, in NTSC for example, you have this happening thirty times per second:
- vertical blanking interval, with vertical sync moves beam to top
- horizontal blanking interval, with H sync, moves beam to left of screen
- phase of the colour burst signal locks the colour decoded in the TV
- line gets drawn
- repeat sync, burst and horizontal line for odd lines
- another vertical interval, with vertical sync,
- repeat sync, burst and horizontal line for even lines
- Voila, you have a frame!
The even lines and odd lines get drawn separately, since if they were drawn consecutively, the phosphor on the top part of the screen would fade before the linews were all drawn.
Last but not least, all this stuff has to be really precise! A little "jitter" in the signal, and it doesn't work.
The good news is that this is an extensively documented standard: hundreds of millions of tv's and video cameras have been made for NTSC, and they all use the same signal standard. Here's one excellent document on the video signal:
But hey.. there are lots of chips around that are designed to do this for you, why not use one of those?
Example of a really old video chip: Motorola 6847 - Wikipedia
Example of recent one that can insert 'arbitrary graphics' into 8 video sources...
Mixed-signal and digital signal processing ICs | Analog Devices
The MC1377 is another old old old chip.. it takes RGB analog values and the chip adds the required sync.
Example of a video generator module: serial in, video out: http://www.smarthome.com/78553.html
There's also the classic 'Bob' video module...
There are lots of others, so have a look around.
Wow this was a long post, and I am sad to know that I have stored so much irrelevant information in my head for this long.
I will try to forget it now.
Disclaimer: I think most of what I remember above is right, but there's probably something not right in there. It was a while ago.