#include <TVout.h>
#include <fontALL.h>
#include "car.h"

TVout TV;
unsigned char x,y,z,pixel_num,d_num;
void setup()  {
  x=0;
  y=0;
  z=0;
  pixel_num=0;
  d_num=0;
  Serial.begin(9600);
  TV.begin(PAL);  //for devices with only 1k sram(m168) use TV.begin(_NTSC,128,56)
  TV.select_font(font6x8);
  Serial.println(TV.vres());
  Serial.println(TV.hres());
  
}

void loop() {
  TV.clear_screen();
  pixel_num=0;
  d_num=0;
  for(x=0;x<TV.hres();x++){
    z=random(TV.vres());
    if(analogRead(A1)<340){
      pixel_num=1;
      d_num=1000;
    }
    if(analogRead(A1)>340 && analogRead(A1)<680){
      pixel_num=2;
      d_num=400;
      //TV.clear_screen();
    }
    if(analogRead(A1)>680){
      pixel_num=4;
      d_num=0;
    }
    for(y=0;y<TV.vres();y++){
      if(analogRead(A0)<205){
      TV.set_pixel(x+random(TV.hres()/12),random(z,TV.vres()/4-z),pixel_num);
      }
      if(analogRead(A0)>205 && analogRead(A0)<410){
      TV.set_pixel(x+random(TV.hres()/10),random(z,TV.vres()/2.3-z),pixel_num);
      }
      if(analogRead(A0)>410 && analogRead(A0)<615){
      TV.set_pixel(x+random(TV.hres()/8),random(z,TV.vres()/2-z),pixel_num);
      }
      if(analogRead(A0)>615 && analogRead(A0)<820){
      TV.set_pixel(x+random(TV.hres()/6),random(z,TV.vres()/1.2-z),pixel_num);
      TV.bitmap(analogRead(A1),0,car);
      }
       if(analogRead(A0)>820){
      TV.set_pixel(x+random(TV.hres()/4),random(z,TV.vres()-z),pixel_num);
      }
      //TV.set_pixel(x+random(50),y+random(y,y*5),analogRead(A0)/10);
      //TV.delay(1000);
    }
     TV.delay(d_num);
  }     
}
