Introduction

High Level Design

Program/Hardware Design

Results

Conclusions

Appendix A: Code

Appendix B: Schematics

Appendix C: Parts Listing 

Appendix D: Specific Tasks

References

Pictures

 

Xuemin Hang: xh24@cornell.edu

Marcel Xu: mx23@cornell.edu

 

 

Appendix A       
       
Audio Code

 

/**************AUDIO CODE FOR MUSIC SYNTHESIZER *****************/          
#include <Mega32.h>   //the chip
#include <stdio.h>       //for debugging using printf, etc
#include <Math.h>      // for sine function
 
// definitions
#define begin {
#define end   }
 
void initialize(void);              //to set up ports  
void chordsPlay(void);
 
//the following must not be registers
#pragma regalloc-  
unsigned long increment,increment2 ;
unsigned char r_char;            //received char    
unsigned int i, j;                     //index to sineTable and counter for number of iterations in the assembly loop
unsigned char notes;             //counter for number of notes played simultaneously
//flags for modulating the wave envelope
unsigned char modulate, brass, count, instrumentcount, instrumentshift;    
unsigned char sineTable[256] @0x300;  //Table of values; need location to avoid glitch   
#pragma regalloc+  
 
//**************************************************      
void main(void)
begin 
  initialize();   
 
  //main loop never exits
  while(1)
  begin
   chordsPlay();  //executes the pushbutton detection and music synthesis
  end       
end 
//**********************************************************
void chordsPlay(void) //function that detects pushbuttons and activates the digital music synthesis
begin
     if(brass==1){  //if the instrument requires waveform modulation
                if(count<instrumentcount) count++; //increment counter when not yet time for modulation
                else{ 
                     if(modulate>instrumentshift) modulate=modulate>>1;          //shift in zeroes from the left
                     count=0;           //reset counter
                }
     }
     else modulate=0xff;  //if the instrument does not require waveform modulation
    
     notes=0;   //reset counter for notes
    
     if(PINC.7==0){       // 493.883 Hz
                if(notes==0){        //if no notes played yet
                                notes=1; //one note played
                                increment = 20715;//frequency*41.94304
                }
     }
     if(PINC.6==0){               // 466.164 Hz---467           
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 19552;//frequency*41.94304
                     }
                     else if(notes==1){//if one note played
                                notes=2; //two notes played
                                increment2 = 19552;//frequency*41.94304
                     }
}    
if(PINC.5==0){  // 440 Hz --441   
if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 18455;//frequency*41.94304
                } 
                else if(notes==1){ //if one note played
                                notes=2;    //two notes played
                                increment2 = 18455;//frequency*41.94304
                }             
}
if(PINC.4==0){      // 415.305 Hz---416
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 17419;//frequency*41.94304
                     }
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 17419;//frequency*41.94304
     }
}  
f(PINC.3==0){      // 391.995 Hz  ---390
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 16441;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2;  //two notes played
                                increment2 = 16441;//frequency*41.94304
     }
}
if(PINC.2==0){     // 369.994 Hz ---370
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 15519;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 15519;//frequency*41.94304
                }
}   
if(PINC.1==0){    //349.228 Hz     ---350
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 14648;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2;    //two notes played
                increment2 = 14648;//frequency*41.94304
     }
 }            
if(PINC.0==0){    // 329.628 Hz     ---328
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment =13826;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 13826;//frequency*41.94304
                }
}
if(PINB.7==0){ // 311.127 Hz --- 310
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 13050;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 13050;//frequency*41.94304
                }
}
if(PINB.6==0){    //293.665 Hz --- 294
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 12317;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 12317;//frequency*41.94304
                }
}
if(PINB.5==0){ //277.183 Hz --- 277
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 11626;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 11626;//frequency*41.94304
                }
}
if(PINB.4==0){    //261.626 Hz --- 262
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 10973;//frequency*41.94304
                }            
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 10973;//frequency*41.94304
                }
}
if(PINB.3==0){    //246.942 Hz --- 247
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 10357;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 10357;//frequency*41.94304
                }
}
if(PINB.2==0){    //233.082 Hz --- 233
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 9776;//frequency*41.94304 
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 9776;//frequency*41.94304
                }
}
if(PINB.1==0){    //220 Hz --- 220
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 9227;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 9227;//frequency*41.94304
                }
}
if(PINB.0==0){    //207.652 Hz --- 208
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 8710;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                notes=2; //two notes played
                                increment2 = 8710;//frequency*41.94304
                }
}
if(PIND.7==0){   //195.998 Hz --- 196
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 8221;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 8221;//frequency*41.94304
                }
}
if(PIND.6==0){   //184.997 Hz --- 185
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 7759;//frequency*41.94304                
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 7759;//frequency*41.94304
                }
}
if(PIND.5==0){   // 174.614 Hz --- 175
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 7324;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 7324;//frequency*41.94304
                }
}
if(PIND.4==0){   // 164.814 Hz --- 165
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 6913;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 6913;//frequency*41.94304
                }
}
if(PIND.3==0){   // 146.832Hz   --- 147  
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 6159;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 6159;//frequency*41.94304
                }
}
if(PIND.2==0){   // 130.813 Hz  --- 131
                if(notes==0){ //if no notes played yet
                                notes=1; //one note played
                                increment = 5487;//frequency*41.94304
                }           
                else if(notes==1){ //if one note played
                                notes=2; //two notes played
                                increment2 = 5487;//frequency*41.94304
                }
}
               
if(notes==1) increment2=0;
if(notes==0) modulate=0xff;
 
if(notes>0) //one or more notes played
      begin    
                #asm
                                ;load the increment bytes into registers
                                lds r9, _increment                 
                                lds r10,_increment+1
                                lds r11, _increment+2    
                 
      
                                ;load the increment bytes into registers
                                lds r3, _increment2                               
                                lds r4,_increment2+1
                                lds r5, _increment2+2    
                               
                                ;code with *** are responsible for modulating the waveform
lds r29, _modulate                                ;load the modulation flag into register ***
         
                                ldi  R31, 0x3                           ;high byte of Z is always pointing to start of the sineTable    
                                lds  r26,_j                               ;load int j into registers
                                lds  r27,_j+1  
                                   
                                Loop:
                                ;40-cycle loop
                                                add r12, r9                              ;accumulator = accumulator + increment;  
                                                adc r13, r10                             ;24-bit accumulator: r14 is highbyte1 (r14, r13, r12)
                                                adc r14, r11                             ;increment: r11 is highbyte1 (r11,r10,r9)      
                                                mov r30, r14                       ;put highbyte1 into address Z
                                                ld  r2, Z                  ;2 cycles (load sineTable[highbyte1] at address Z to register 2)
                                               
                                                add r6, r3                                ;accumulator = accumulator + increment;  
                                                adc r7, r4                 ;24-bit accumulator: r8 is highbyte (r8, r7, r6)
                                                adc r8, r5                                 ;increment: r5 is highbyte2 (r5,r4,r3) 
                                                mov r30, r8           ;put highbyte2 into address Z
                                                ld  r15, Z                ;2 cycles (load sineTable[highbyte2] at address Z to register 15)
                                               
                                                add r15, r2                              ;add the 2 contents together
                                               
                                                sbrs r29, 0                               ;skip if bit 0 is 0 ***
                                                lsr r15                                      ;shift right (/2)  ***
                                                                    
                                                sbrs r29, 1                               ;skip if bit 1 is 0 ***
                                                lsr r15                                      ;shift right (/2)  ***
                                               
                                                sbrs r29, 2                               ;skip if bit 2 is 0 ***
                                                lsr r15                                      ;shift right (/2)  ***
                                   
                                                sbrs r29, 3                               ;skip if bit 3=0  ***
                                                lsr r15                                      ;shift right (/2)  ***
                                               
                                                sbrs r29, 4                               ;skip if bit 4 =0 ***
                                                lsr r15                                      ;shift right (/2)  ***
                                                                    
                                                sbrs r29, 5                               ;skip if bit5=0    ***
                                                lsr r15                                      ;shift right (/2)  ***
                                               
                                                sbrs r29, 6                               ;skip if bit6=0   ***
                                                lsr r15                                      ;shift right (/2) ***
                                   
                                              sbrs r29, 7                 ;skip if bit7=0  ***
                                                lsr r15                                      ;shift right (/2)***
 
                                                ;PORTA = sineTable[highbyte1] + sineTable[highbyte2]
                                                out  0x1B,r15                                        
 
               ldi r28, 0                                  ;put constants into registers
                                                ldi r25, 1                                 ;put constants into registers
                                                add r26, r25                            ;j++;   
                                                adc r27, r28
               
               
                                                cpi  r26, low(0x07d0)              ;1 cycle
                                                ldi  r28, high(0x07d0)             ;1 cycle
                                                cpc  r27, r28                            ;1 cycle
                                                brsh EndLoop                      ;while(j<2000) ( 1 cycle if false)
                                                rjmp Loop                             ;2 cycles      else, repeat the loop 
                                                                  
                                                EndLoop:
 
                                #endasm                     
                  
      end
   
               
end  
//*************************************************************************
interrupt [USART_RXC] void usart_rec(void) //implements the menu commands from the PC
begin
   r_char=UDR;                      // get a char
  
   switch(r_char){   //command depends on the representative char                               
                case 'd':   //default
                                brass=0;
                                for(i=0;i<256;i++) sineTable[i]=64+(char)(63.5*sin(6.283*((float)i)/256.0));
                                break;                                     
                                                                  
                                                                                                                                               
                //WOODWINDS
                                                                               
                case 'c':     //clarinet sound ....very good (clarinet and bass clarinet) ----------- DEMO  
                                brass=0;
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((19.01 *sin(6.283*((float)i)/256.0)) + (0.76 *sin(2*6.283*((float)i)/256.0))+ (18.82*sin(3*6.283*((float)i)/256.0)) + (2.28*sin(4*6.283*((float)i)/256.0)) + (10.08*sin(5*6.283*((float)i)/256.0)) + (2.09 *sin(6*6.283*((float)i)/256.0))+ (4.94*sin(7*6.283*((float)i)/256.0)) + (0.95 *sin(8*6.283*((float)i)/256.0))+(4.56*sin(9*6.283*((float)i)/256.0)));
                                break;                                                                                     
                                                                                  
                case 'f':     //flute sound 
                                brass=0;
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((29.81 *sin(6.283*((float)i)/256.0)) + (23.95*sin(2*6.283*((float)i)/256.0)) + (5.37*sin(3*6.283*((float)i)/256.0)) + (3.28*sin(4*6.283*((float)i)/256.0)) + (1.49*sin(5*6.283*((float)i)/256.0)) + (0.596*sin(6*6.283*((float)i)/256.0)));
                                break;  
                               
                case 'p':   //piccolo
                                brass=0;
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((29.81 *sin(2*6.283*((float)i)/256.0)) + (23.95*sin(4*6.283*((float)i)/256.0)) + (5.37*sin(6*6.283*((float)i)/256.0)) + (3.28*sin(8*6.283*((float)i)/256.0)) + (1.49*sin(10*6.283*((float)i)/256.0)) + (0.596*sin(12*6.283*((float)i)/256.0)));
                                break;                                                                                                                                     
                                          
                case 'o':    //oboe sound
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((3.01*sin(6.283*((float)i)/256.0)) + (11.14*sin(2*6.283*((float)i)/256.0))+ (10.08*sin(3*6.283*((float)i)/256.0)) + (17.33*sin(4*6.283*((float)i)/256.0)) + (9.905*sin(5*6.283*((float)i)/256.0)) + (6.72*sin(6*6.283*((float)i)/256.0))+ (3.36*sin(7*6.283*((float)i)/256.0))+ (0.0884*sin(8*6.283*((float)i)/256.0)) + (0.531*sin(9*6.283*((float)i)/256.0)));
                                brass=1;
                                instrumentcount=40;
                                instrumentshift=0x7f;
                                break;     
                                                                               
                                                                                                                                         
                //BRASS
                                                                               
                case 'h':    //horn long sound (good for lower octave)            
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((24.8 *sin(6.283*((float)i)/256.0)) + (22.076*sin(2*6.283*((float)i)/256.0))+ (12.4*sin(3*6.283*((float)i)/256.0)) + (3.72*sin(4*6.283*((float)i)/256.0)) + (0.248*sin(5*6.283*((float)i)/256.0)) + (0.248*sin(8*6.283*((float)i)/256.0)));
                                brass=1;
                                instrumentcount=50;
                                instrumentshift=0x7f;
                                break;       
                                                                               
                case 'i':     //horn bounce sound (good for lower octave)
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((24.8 *sin(6.283*((float)i)/256.0)) + (22.076*sin(2*6.283*((float)i)/256.0))+ (12.4*sin(3*6.283*((float)i)/256.0)) + (3.72*sin(4*6.283*((float)i)/256.0)) + (0.248*sin(5*6.283*((float)i)/256.0)) + (0.248*sin(8*6.283*((float)i)/256.0)));
                                brass=1;
                                instrumentcount=30;
                                instrumentshift=0;
                                break;                                     
                                                                                                  
                case 't':     //trombone long sound (good for lower octave) ----------- DEMO    
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((4.12*sin(6.283*((float)i)/256.0)) + (19.42*sin(2*6.283*((float)i)/256.0))+ (37.35*sin(3*6.283*((float)i)/256.0)) + (0.374*sin(4*6.283*((float)i)/256.0)) + (1.494*sin(5*6.283*((float)i)/256.0)) + (0.747*sin(6*6.283*((float)i)/256.0)));
                                brass=1;
                                instrumentcount=45;
                                instrumentshift=0x7f;
                                break;   
                                                                                                                                                               
                 case 'u':   //trombone bounce sound (good for lower octave) ----------- DEMO  
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((4.12*sin(6.283*((float)i)/256.0)) + (19.42*sin(2*6.283*((float)i)/256.0))+ (37.35*sin(3*6.283*((float)i)/256.0)) + (0.374*sin(4*6.283*((float)i)/256.0)) + (1.494*sin(5*6.283*((float)i)/256.0)) + (0.747*sin(6*6.283*((float)i)/256.0)));
                                brass=1;
                                instrumentcount=30;
                                instrumentshift=0x0;
                                break;              
                                 
                case 'r': //trumpet long sound
                                for(i=0;i<256;i++) sineTable[i]=(char)(0.5*i);
                                brass=1;                                
                                instrumentcount=20;
                                instrumentshift=0x7f;
                                break;                        
                                                          
                case 's': //trumpet bouncesound
                                for(i=0;i<256;i++) sineTable[i]=(char)(0.5*i);
                                brass=1;                                
                                instrumentcount=20;
                                instrumentshift=0;
                                break;                
                                                                               
                                                                               
                                //STRINGS  
                               
                case 'z':    //guitar (plucked string) ----------- DEMO
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((4.12*sin(2*6.283*((float)i)/256.0)) + (20.1*sin(4*6.283*((float)i)/256.0))+ (37.35*sin(6*6.283*((float)i)/256.0)) + (0.374*sin(8*6.283*((float)i)/256.0)) + (1.494*sin(10*6.283*((float)i)/256.0)) );
                                    brass=1;
                                    instrumentcount=15;
                                instrumentshift=0;
                                break;                                                  
                                                                                                               
        //PERCUSSION
 
case 'y':     //steel drum
                                for(i=0;i<256;i++) sineTable[i]=64+(char)((sin(6.283*((float)i)/256.0)) + (19.42*sin(2*6.283*((float)i)/256.0))+ (37.35*sin(3*6.283*((float)i)/256.0))); 
                                brass=1;
                                instrumentcount=8;
                                instrumentshift=0x0;
                                break; 
                                                                               
 
   }
end 
//**********************************************************
//Set it all up
void initialize(void)
begin
  UCSRB = 0x98 ;//enable RxC,Rx,Tx
  UBRRL = 103 ;   //using a 16 MHz crystal (9600)
 
  //set up the ports  
  DDRD=0x00;     // PORT D is an input with pullup
  PORTD=0xff;
  DDRB=0x00;      // PORT B is an input with pullup
  PORTB=0xff; 
  DDRC=0x00;      // PORT C is an input with pullup
  PORTC=0xff;            
 
  DDRA=0xff;    // PORT A is an output 
  PORTA=0;
 
  //make sine table
  for(i=0;i<256;i++) sineTable[i]=64+(char)(63.5*sin(6.283*((float)i)/256.0)); 
 
  //init the flags
  increment = 0;  
  increment2=0;
 
  notes=0;
  j=1; 
  modulate=0xff;     
  brass=1;
  count=0;
  instrumentcount=0;
  instrumentshift=0xff;
 
  //crank up the ISRs
  #asm
                sei
                clr r14
                clr r13  
                clr r12 
                clr r6
                clr r7  
                clr r8
  #endasm
 
end 
 

 

  
 
Video Code
 
//video gen and sound
//D.5 is sync:1000 ohm + diode to 75 ohm resistor
//D.6 is video:330 ohm + diode to 75 ohm resistor 
//B.3 is sound  and should have a 10k resistor to gnd
 
#pragma regalloc-    //I allocate the registers myself
#pragma optsize-     //optimize for speed
                   
#include <Mega32.h>  
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <delay.h>  
 
//cycles = 63.625 * 16 Note NTSC is 63.55
//but this line duration makes each frame exactly 1/60 sec
//which is nice for keeping a realtime clock
#define lineTime 1018
 
#define begin {
#define end   }
#define ScreenTop 30
#define ScreenBot 230
#define MINUTE 60
 
//NOTE that v1 to v8 and i must be in registers! 
register char v1 @4;
register char v2 @5;
register char v3 @6;
register char v4 @7;
register char v5 @8;
register char v6 @9;
register char v7 @10;
register char v8 @11;
register int i @12;
 
#pragma regalloc+
 
char syncON, syncOFF;
int LineCount;
 
 
//flags for game and music modes
char x1, y1, x2, y2, sharp1, sharp2, flat1, flat2, flatcounter; //sharp or flat flags
char flatmode, gamemode, gamecounter, gamenote, gamenote2,notecounter, notetimer;            //mode flags
char score, scored, scored2, buttoncounter, seconds;          //other variables
char realtime;                          //time to be displayed
char screen[1600];   //array for holding bits to be shown on screen                
char text1[]="MUSIC";         //array to hold the word “MUSIC”
char text2[]="NOTES";        //array to hold the word “NOTES”
char text3[]="FLATS";          //array to hold the word “FLATS”
char text4[]="SHARP";         //array to hold the word “SHARP”
char text5[]="MODE";         //array to hold the word “MODE”
char test1[]="TEST1";          //array to hold the word “TEST1”
char test2[]="TEST2";          //array to hold the word “TEST2”
char scoretext[]="SCORE"; //array to hold the word “SCORE”
char timertext[]="LEVEL";  //array to hold the word “LEVEL” 
char games[]="GAMES";     //array to hold the word “GAMES”
char game[]="GAME";        //array to hold the word “GAME”
char over[]="OVER";           //array to hold the word “OVER”
char time[]="TIME";            //array to hold the word “TIME”
char left[]="LEFT";               //array to hold the word “LEFT”
char menu[]="MENU";       //array to hold the word “MENU”
char flat[]="FLAT";               //array to hold the word “FLAT”
char xx[]="XX";                    //array to hold the word “XX”
char yy[]="YY";                     //array to hold the word “YY”
char sc[5];                                //array to hold values
char t[5];                                 //array to hold values
char rt[5];                                //array to hold values
//Point plot lookup table  
//One bit masks
flash char pos[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};                                    
 
//define some character bitmaps
//5x7 characters
flash char bitmap[38][7]={
                //0
                0b01110000,
                0b10001000,
                0b10011000,
                0b10101000,
                0b11001000,
                0b10001000,
                0b01110000,
                //1
                0b00100000,
                0b01100000,
                0b00100000,
                0b00100000,
                0b00100000,
                0b00100000,
                0b01110000, 
                //2
                0b01110000,
                0b10001000,
                0b00001000,
                0b00010000,
                0b00100000,
                0b01000000,
                0b11111000,
    //3
                0b11111000,
                0b00010000,
                0b00100000,
                0b00010000,
                0b00001000,
                0b10001000,
                0b01110000,
                //4
                0b00010000,
                0b00110000,
                0b01010000,
                0b10010000,
                0b11111000,
                0b00010000,
                0b00010000,
                //5
                0b11111000,
                0b10000000,
                0b11110000,
                0b00001000,
                0b00001000,
                0b10001000,
                0b01110000,
                //6
                0b01000000,
                0b10000000,
                0b10000000,
                0b11110000,
                0b10001000,
                0b10001000,
                0b01110000,
                //7
                0b11111000,
                0b00001000,
                0b00010000,
                0b00100000,
                0b01000000,
                0b10000000,
                0b10000000,
                //8
                0b01110000,
                0b10001000,
                0b10001000,
                0b01110000,
                0b10001000,
                0b10001000,
                0b01110000,
                //9
                0b01110000,
                0b10001000,
                0b10001000,
                0b01111000,
                0b00001000,
                0b00001000,
                0b00010000, 
                //A
                0b01110000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b11111000,
                0b10001000,
                0b10001000,
                //B
                0b11110000,
                0b10001000,
                0b10001000,
                0b11110000,
                0b10001000,
                0b10001000,
                0b11110000,
                //C
                0b01110000,
                0b10001000,
                0b10000000,
                0b10000000,
                0b10000000,
                0b10001000,
                0b01110000,
                //D
                0b11110000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b11110000,
                //E
                0b11111000,
                0b10000000,
                0b10000000,
                0b11111000,
                0b10000000,
                0b10000000,
                0b11111000,
                //F
                0b11111000,
                0b10000000,
                0b10000000,
                0b11111000,
                0b10000000,
                0b10000000,
                0b10000000,
                //G
                0b01110000,
                0b10001000,
                0b10000000,
                0b10011000,
                0b10001000,
                0b10001000,
                0b01110000,
                //H
                0b10001000,
                0b10001000,
                0b10001000,
                0b11111000,
                0b10001000,
                0b10001000,
                0b10001000,
                //I
                0b01110000,
                0b00100000,
                0b00100000,
                0b00100000,
                0b00100000,
                0b00100000,
                0b01110000,
                //J
                0b00111000,
                0b00010000,
                0b00010000,
                0b00010000,
                0b00010000,
                0b10010000,
                0b01100000,
                //K
                0b10001000,
                0b10010000,
                0b10100000,
                0b11000000,
                0b10100000,
                0b10010000,
                0b10001000,
                //L
                0b10000000,
                0b10000000,
                0b10000000,
                0b10000000,
                0b10000000,
                0b10000000,
                0b11111000,
                //M
                0b10001000,
                0b11011000,
                0b10101000,
                0b10101000,
                0b10001000,
                0b10001000,
                0b10001000,
                //N
                0b10001000,
                0b10001000,
                0b11001000,
                0b10101000,
                0b10011000,
                0b10001000,
                0b10001000,
                //O
                0b01110000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b01110000,
                //P
                0b11110000,
                0b10001000,
                0b10001000,
                0b11110000,
                0b10000000,
                0b10000000,
                0b10000000,
                //Q
                0b01110000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b10101000,
                0b10010000,
                0b01101000,
                //R
                0b11110000,
                0b10001000,
                0b10001000,
                0b11110000,
                0b10100000,
                0b10010000,
                0b10001000,
                //S
                0b01111000,
                0b10000000,
                0b10000000,
                0b01110000,
                0b00001000,
                0b00001000,
                0b11110000,
                //T
                0b11111000,
                0b00100000,
                0b00100000,
                0b00100000,
                0b00100000,
                0b00100000,
                0b00100000,
                //U
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b01110000,
                //V
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b01010000,
                0b00100000,
                //W
                0b10001000,
                0b10001000,
                0b10001000,
                0b10101000,
                0b10101000,
                0b10101000,
                0b01010000,
                //X
                0b10001000,
                0b10001000,
                0b01010000,
                0b00100000,
                0b01010000,
                0b10001000,
                0b10001000,
                //Y
                0b10001000,
                0b10001000,
                0b10001000,
                0b01010000,
                0b00100000,
                0b00100000,
                0b00100000,
                //Z
                0b11111000,
                0b00001000,
                0b00010000,
                0b00100000,
                0b01000000,
                0b10000000,
                0b11111000,
                //figure1
                0b01110000,
                0b00100000,
                0b01110000,
                0b10101000,
                0b00100000,
                0b01010000,
                0b10001000,
                //figure2
                0b01110000,
                0b10101000,
                0b01110000,
                0b00100000,
                0b00100000,
                0b01010000,
                0b10001000};
 
 
//================================
//3x5 font numbers, then letters
//packed two per definition for fast
//copy to the screen at x-position divisible by 4
flash char smallbitmap[39][5]={
                //0
    0b11101110,
                0b10101010,
                0b10101010,
                0b10101010,
                0b11101110,
                //1
                0b01000100,
                0b11001100,
                0b01000100,
                0b01000100,
                0b11101110,
                //2
                0b11101110,
                0b00100010,
                0b11101110,
                0b10001000,
                0b11101110,
                //3
                0b11101110,
                0b00100010,
                0b11101110,
                0b00100010,
                0b11101110,
                //4
                0b10101010,
                0b10101010,
                0b11101110,
                0b00100010,
                0b00100010,
                //5
                0b11101110,
                0b10001000,
                0b11101110,
                0b00100010,
                0b11101110,
                //6
                0b11001100,
                0b10001000,
                0b11101110,
                0b10101010,
                0b11101110,
                //7
                0b11101110,
                0b00100010,
                0b01000100,
                0b10001000,
                0b10001000,
                //8
                0b11101110,
                0b10101010,
                0b11101110,
                0b10101010,
                0b11101110,
                //9
                0b11101110,
                0b10101010,
                0b11101110,
                0b00100010,
                0b01100110,
                //:
                0b00000000,
                0b01000100,
                0b00000000,
                0b01000100,
                0b00000000,
                //=
                0b00000000,
                0b11101110,
                0b00000000,
                0b11101110,
                0b00000000,
                //blank
                0b00000000,
                0b00000000,
                0b00000000,
                0b00000000,
                0b00000000,
                //A
                0b11101110,
                0b10101010,
                0b11101110,
                0b10101010,
                0b10101010,
                //B
                0b11001100,
                0b10101010,
                0b11101110,
                0b10101010,
                0b11001100,
                //C
                0b11101110,
                0b10001000,
                0b10001000,
                0b10001000,
                0b11101110,
                //D
                0b11001100,
                0b10101010,
                0b10101010,
                0b10101010,
                0b11001100,
                //E
                0b11101110,
                0b10001000,
                0b11101110,
                0b10001000,
                0b11101110,
                //F
                0b11101110,
                0b10001000,
                0b11101110,
                0b10001000,
                0b10001000,
                //G
                0b11101110,
                0b10001000,
                0b10001000,
                0b10101010,
                0b11101110,
                //H
                0b10101010,
                0b10101010,
                0b11101110,
                0b10101010,
                0b10101010,
                //I
                0b11101110,
                0b01000100,
                0b01000100,
                0b01000100,
                0b11101110,
                //J
                0b00100010,
                0b00100010,
                0b00100010,
                0b10101010,
                0b11101110,
                //K
                0b10001000,
                0b10101010,
                0b11001100,
                0b11001100,
                0b10101010,
                //L
                0b10001000,
                0b10001000,
                0b10001000,
                0b10001000,
                0b11101110,
                //M
                0b10101010,
                0b11101110,
                0b11101110,
                0b10101010,
                0b10101010,
                //N
                0b00000000,
                0b11001100,
                0b10101010,
                0b10101010,
                0b10101010,
                //O
                0b01000100,
                0b10101010,
                0b10101010,
                0b10101010,
                0b01000100,
                //P
                0b11101110,
                0b10101010,
                0b11101110,
                0b10001000,
                0b10001000,
                //Q
                0b01000100,
                0b10101010,
                0b10101010,
                0b11101110,
                0b01100110,
                //R
                0b11101110,
                0b10101010,
                0b11001100,
                0b11101110,
                0b10101010,
                //S
                0b11101110,
                0b10001000,
                0b11101110,
                0b00100010,
                0b11101110,
                //T
                0b11101110,
                0b01000100,
                0b01000100,
                0b01000100,
                0b01000100,
                //U
                0b10101010,
                0b10101010,
                0b10101010,
                0b10101010,
                0b11101110,
                //V
                0b10101010,
                0b10101010,
                0b10101010,
                0b10101010,
                0b01000100,
                //W
                0b10101010,
                0b10101010,
                0b11101110,
                0b11101110,
                0b10101010,
                //X
                0b00000000,
                0b10101010,
                0b01000100,
                0b01000100,
                0b10101010,
                //Y
                0b10101010,
                0b10101010,
                0b01000100,
                0b01000100,
                0b01000100,
                //Z
                0b11101110,
                0b00100010,
                0b01000100,
                0b10001000,
                0b11101110
                };
               
//==================================
//This is the sync generator and raster generator. It MUST be entered from
//sleep mode to get accurate timing of the sync pulses
#pragma warn-
interrupt [TIM1_COMPA] void t1_cmpA(void)  //interrupt heading
begin
  //start the Horizontal sync pulse   
  PORTD = syncON;    
  //update the curent scanline number
  LineCount ++ ;  
  //begin inverted (Vertical) synch after line 247
  if (LineCount==248)
  begin
    syncON = 0b00100000;                   
    syncOFF = 0;                    
  end
  //back to regular sync after line 250
  if (LineCount==251)         
  begin
    syncON = 0;                     
    syncOFF = 0b00100000;
  end 
  //start new frame after line 262
  if (LineCount==263)
  begin
     LineCount = 1;
  end 
 
  delay_us(2); //adjust to make 5 us pulses
  //end sync pulse
  PORTD = syncOFF;  
 
  if (LineCount<ScreenBot && LineCount>=ScreenTop)
    begin
      
       //compute byte index for beginning of the next line
       //left-shift 4 would be individual lines
       // <<3 means line-double the pixels
       //The 0xfff8 truncates the odd line bit
       //i=(LineCount-ScreenTop)<<3 & 0xfff8; //
      
       #asm
       push r16
       lds   r12, _LineCount
       lds   r13, _Linecount+1
       ldi   r16, 30
       sub  r12, r16
       ldi  r16,0
       sbc  r13, r16
       lsl  r12
       rol  r13
       lsl  r12
       rol  r13
       lsl  r12   
       rol  r13
       mov  r16,r12
       andi r16,0xf0
       mov  r12,r16
       pop r16
       #endasm
       
       //load 16 registers with screen info
       #asm
       push r14
       push r15
       push r16
       push r17
       push r18
       push r19
       push r26
       push r27
      
       ldi  r26,low(_screen)   ;base address of screen
       ldi  r27,high(_screen)  
       add  r26,r12            ;offset into screen (add i)
       adc  r27,r13
       ld   r4,x+                ;load 16 registers and inc pointer
       ld   r5,x+
       ld   r6,x+ 
       ld   r7,x+
       ld   r8,x+
       ld   r9,x+
       ld   r10,x+ 
       ld   r11,x+
       ld   r12,x+
       ld   r13,x+
       ld   r14,x+ 
       ld   r15,x+
       ld   r16,x+  
       ld   r17,x+ 
       ld   r18,x+
       ld   r19,x
      
       pop  r27
       pop  r26
       #endasm 
 
       delay_us(4);  //adjust to center image on screen
      
       //blast 16 bytes to the screen  
       #asm
       ;but first a macro to make the code shorter 
       ;the macro takes a register number as a parameter
       ;and dumps its bits serially to portD.6  
       ;the nop can be eliminated to make the display narrower
       .macro videobits ;regnum
        BST  @0,7
                IN   R30,0x12
                BLD  R30,6
                nop
                OUT  0x12,R30 
               
                BST  @0,6
                IN   R30,0x12
                BLD  R30,6
                nop
                OUT  0x12,R30
               
                BST  @0,5
                IN   R30,0x12
                BLD  R30,6
                nop
                OUT  0x12,R30
               
                BST  @0,4
                IN   R30,0x12
                BLD  R30,6
                nop
                OUT  0x12,R30
               
                BST  @0,3
                IN   R30,0x12
                BLD  R30,6
                nop
                OUT  0x12,R30
               
                BST  @0,2
                IN   R30,0x12
                BLD  R30,6
                nop
                OUT  0x12,R30
               
                BST  @0,1
                IN   R30,0x12
                BLD  R30,6
                nop
                OUT  0x12,R30
               
                BST  @0,0
                IN   R30,0x12
                BLD  R30,6
                nop
                OUT  0x12,R30
       .endm    
       
                videobits r4 ;video line -- byte 1
        videobits r5 ;byte 2 
        videobits r6 ;byte 3
        videobits r7 ;byte 4
        videobits r8 ;byte 5
        videobits r9 ;byte 6
        videobits r10 ;byte 7
        videobits r11 ;byte 8
        videobits r12 ;byte 9
        videobits r13 ;byte 10 
        videobits r14 ;byte 11
        videobits r15 ;byte 12
        videobits r16 ;byte 13
        videobits r17 ;byte 14
        videobits r18 ;byte 15
        videobits r19 ;byte 16
                clt   ;clear video after the last pixel on the line
                IN   R30,0x12
                BLD  R30,6
                OUT  0x12,R30
                                                                                                                               
       pop r19
       pop r18
       pop r17
       pop r16 
       pop r15
       pop r14
       #endasm
             
    end        
end 
#pragma warn+
 
//==================================
//plot one point
//at x,y with color 1=white 0=black 2=invert
#pragma warn-
void video_pt(char x, char y, char c)
begin  
               
                #asm
                ;  i=(x>>3) + ((int)y<<4) ;   the byte with the pixel in it
 
                push r16
                ldd r30,y+2                            ;get x
                lsr r30
                lsr r30
                lsr r30                      ;divide x by 8
                ldd r12,y+1                            ;get y
                lsl r12                      ;mult y by 16
                clr r13
                lsl r12
                rol r13
                lsl r12
                rol r13
                lsl r12
                rol r13
                add r12, r30            ;add in x/8
               
                ;v2 = screen[i];   r5
        ;v3 = pos[x & 7];  r6
                ;v4 = c            r7
                ldi r30,low(_screen)
                ldi r31,high(_screen)
                add r30, r12
                adc r31, r13
                ld r5,Z                    ;get screen byte
                ldd r26,y+2                            ;get x
                ldi r27,0
                andi r26,0x07           ;form x & 7
                ldi r30,low(_pos*2) 
                ldi r31,high(_pos*2)
                add r30,r26
                adc r31,r27
                lpm r6,Z
                ld r16,y                    ;get c
      
       ;if (v4==1) screen[i] = v2 | v3 ;
       ;if (v4==0) screen[i] = v2 & ~v3;
       ;if (v4==2) screen[i] = v2 ^ v3 ;
      
       cpi r16,1
       brne tst0
       or  r5,r6
       tst0:
       cpi r16,0
       brne tst2
       com r6
       and r5,r6
       tst2:
       cpi r16,2
       brne writescrn
       eor r5,r6
       writescrn:
                ldi r30,low(_screen)
                ldi r31,high(_screen)
                add r30, r12
                adc r31, r13
                st Z, r5                    ;write the byte back to the screen
               
                pop r16
                #endasm
      
end
#pragma warn+
 
//==================================
// put a big character on the screen
// c is index into bitmap
void video_putchar(char x, char y, char c) 
begin
    v7 = x;
    for (v6=0;v6<7;v6++)
    begin
        v1 = bitmap[c][v6];
        v8 = y+v6;
        video_pt(v7,   v8, (v1 & 0x80)==0x80); 
        video_pt(v7+1, v8, (v1 & 0x40)==0x40);
        video_pt(v7+2, v8, (v1 & 0x20)==0x20);
        video_pt(v7+3, v8, (v1 & 0x10)==0x10);
        video_pt(v7+4, v8, (v1 & 0x08)==0x08);
    end
end
 
//==================================
// put a string of big characters on the screen
void video_puts(char x, char y, char *str)
begin
                char i ;
                for (i=0; str[i]!=0; i++)
                begin 
                                if (str[i]>=0x30 && str[i]<=0x3a)
                                                video_putchar(x,y,str[i]-0x30);
                                else video_putchar(x,y,str[i]-0x40+9);
                                x = x+6;
                end
end
     
//==================================
// put a small character on the screen
// x-cood must be on divisible by 4
// c is index into bitmap
void video_smallchar(char x, char y, char c) 
begin
                char mask;
                i=((int)x>>3) + ((int)y<<4) ;
                if (x == (x & 0xf8)) mask = 0x0f;     //f8
                else mask = 0xf0;
               
                screen[i] =    (screen[i] & mask) | (smallbitmap[c][0] & ~mask);
                screen[i+16] = (screen[i+16] & mask) | (smallbitmap[c][1] & ~mask);
        screen[i+32] = (screen[i+32] & mask) | (smallbitmap[c][2] & ~mask);
        screen[i+48] = (screen[i+48] & mask) | (smallbitmap[c][3] & ~mask);
                screen[i+64] = (screen[i+64] & mask) | (smallbitmap[c][4] & ~mask);
end 
 
//==================================
// put a string of small characters on the screen
// x-cood must be on divisible by 4
void video_putsmalls(char x, char y, char *str)
begin
                char i ;
                for (i=0; str[i]!=0; i++)
                begin 
                                if (str[i]>=0x30 && str[i]<=0x3a)
                                                video_smallchar(x,y,str[i]-0x30);
                                else video_smallchar(x,y,str[i]-0x40+12);
                                x = x+4;
                end
end
      
//==================================
//plot a line
//at x1,y1 to x2,y2 with color 1=white 0=black 2=invert
//NOTE: this function requires signed chars  
//Code is from David Rodgers,
//"Procedural Elements of Computer Graphics",1985
void video_line(char x1, char y1, char x2, char y2, char c)
begin  
                int e;
                signed char dx,dy,j, temp;
                signed char s1,s2, xchange;
        signed char x,y;
       
                x = x1;
                y = y1;
                dx = cabs(x2-x1);
                dy = cabs(y2-y1);
                s1 = csign(x2-x1);
                s2 = csign(y2-y1);
                xchange = 0;  
                if (dy>dx)
                begin
                                temp = dx;
                                dx = dy;
                                dy = temp;
                                xchange = 1;
                end
                e = ((int)dy<<1) - dx;  
                for (j=0; j<=dx; j++)
                begin
                                video_pt(x,y,c) ;
                                if (e>=0)
                                begin
                                                if (xchange==1) x = x + s1;
                                                else y = y + s2;
                                                e = e - ((int)dx<<1);
                                end
                                if (xchange==1) y = y + s2;
                                else x = x + s1;
                                e = e + ((int)dy<<1);
                end
end
 
//==================================
//return the value of one point
//at x,y with color 1=white 0=black 2=invert
char video_set(char x, char y)
begin
                //The following construction
                //detects exactly one bit at the x,y location
                i=((int)x>>3) + ((int)y<<4) ; 
    return ( screen[i] & 1<<(7-(x & 0x7)));           
end
 
//==================================
void drawnote(char x, char y)
begin
                //draws the note on the staff as an oval
                video_line(x-2,y-2,x+2,y-2,2);
                video_line(x-3,y-1,x+3,y-1,2);
                video_line(x-3,y,x+3,y,2);
                video_line(x-3,y+1,x+3,y+1,2);
                video_line(x-2,y+2,x+2,y+2,2);
end
//================================== 
void drawflat(char x, char y)
begin
                //draws the flat on the left of the note on the staff
                video_line(x-8,y-7,x-8,y+3,2);
                video_pt(x-7,y+2,2);
                video_pt(x-6,y+1,2);
                video_pt(x-5,y,2);
                video_line(x-4,y-2,x-4,y-1,2);
                video_line(x-6,y-3,x-5,y-3,2);
                video_pt(x-7,y-2,2);
end
//==================================
void drawsharp(char x, char y)
begin
                //draws the sharp to the left of the note on the staff
                video_line(x-5,y-5,x-5,y+3,2);
                video_line(x-8,y-3,x-8,y+5,2);
                video_line(x-7,y+2,x-6,y+2,2);
                video_line(x-7,y-2,x-6,y-2,2);    
                video_line(x-10,y-1,x-9,y-1,2);
                video_line(x-10,y+4,x-9,y+4,2);
                video_line(x-4,y-3,x-3,y-3,2);
                video_pt(x-4,y+1,2);
end  
//==================================
void drawstaff(void)//draws the staff
begin
               
    //music lines
  #define width 126
//treble stave
  video_line(0,15,width,15,1);
  video_line(0,21,width,21,1);
  video_line(0,27,width,27,1);
  video_line(0,33,width,33,1);
  video_line(0,39,width,39,1);
//middle C single stave
  video_line(45,45,85,45,1);
//bass stave
  video_line(0,58,width,58,1);
  video_line(0,64,width,64,1);
  video_line(0,70,width,70,1);
  video_line(0,76,width,76,1);
  video_line(0,82,width,82,1);
 
  //treble clef 
  video_line(15,21,15,45,1);
  video_line(12,46,15,46,1);
  video_pt(14,47,1);
  video_pt(13,47,1);
  video_pt(11,45,1);
  video_line(11,44,13,44,1);
  video_line(11,43,13,43,1);
 
  video_line(14,17,14,23,1);
  video_line(13,12,13,19,1);
  video_line(14,10,14,12,1);
  video_pt(15,10,1);
  video_pt(15,9,1);
  video_pt(16,10,1);
  video_pt(17,12,1);
  video_pt(17,11,1);
  video_line(18,12 ,18,18,1);
  video_line(19,13 ,19,16,1);
  video_line(17,17 ,17,20,1);
  video_pt(16,19,1);
  video_pt(16,20,1);
  video_pt(16,22,1);
 
  video_pt(13,23,1);
  video_pt(13,24,1); 
  video_pt(12,24,1);
  video_pt(12,25,1);
  video_pt(11,25,1);
  video_pt(11,26,1);
  video_pt(10,26,1);
  video_pt(10,28,1);
  video_pt(9,28,1);
  video_pt(9,29,1);
  video_pt(8,29,1);
  video_pt(9,30,1);
  video_pt(8,30,1); 
  video_pt(7,30,1);
  video_pt(7,31,1);
  video_pt(8,31,1);
  video_pt(7,32,1);
  video_pt(8,32,1);
  video_pt(8,34,1);
  video_pt(9,34,1);
  video_pt(9,35,1);
  video_pt(10,36,1);
  video_pt(10,37,1);
  video_pt(11,37,1);
  video_pt(12,38,1);
  video_pt(13,38,1);
 
  video_pt(17,38,1);
  video_pt(18,38,1);
  video_pt(19,37,1);
  video_line(20,30,20,36,1);
  video_line(21,31,21,35,1);
  video_line(19,29,19,32,1);
  video_line(18,29,18,31,1);
  video_pt(17,29,1);
  video_line(14,28,17,28,1);
  video_pt(14,29,1);
  video_pt(13,29,1);
  video_pt(13,30,1);
  video_line(12,30,12,32,1);
  video_pt(13,34,1);
  video_pt(14,35,1);  
 
 
  //bass clef
  video_line(12,63,15,63,1);
  video_line(12,65,15,65,1);
  video_line(11,66,15,66,1);
  video_pt(10,65,1);
  video_line(9,61,9,63,1);
  video_pt(10,60,1);
  video_pt(11,59,1);
  video_pt(16,59,1);
  video_pt(17,59,1);
  video_pt(18,60,1);
  video_line(19,61,19,67,1);
  video_line(20,62,20,65,1);
  video_line(18,67,18,69,1);
  video_pt(17,69,1);
  video_pt(17,71,1);
  video_pt(16,71,1);
  video_pt(16,72,1);
  video_pt(15,73,1);
  video_line(13,74,14,74,1);
  video_line(11,75,12,75,1);
  video_line(8,77,9,77,1);
 
  video_line(24,61,25,61,1);
  video_line(24,62,25,62,1);
  video_line(24,66,25,66,1);
  video_line(24,67,25,67,1);
 
 
end
 
//==================================   
void displaynotes(void)
begin
     //*********************************************  
     //toggle sharp mode or flat mode                
      if(PINA.0==0){ //if PINA.0 pushed
                                   if(flatcounter<20) flatcounter++; //increment if less than  20
                                   else{//toggle flat mode if counter is over 20
                                                flatmode=flatmode^1;
                                                flatcounter=0; 
                     }
                   }
                                //print MODE
                                video_putsmalls(108,2,text5);
                                                if(flatmode==1)    video_putsmalls(80,2,text3);   //print "FLATS"     
                                                else             video_putsmalls(80,2,text4);     //print "SHARP"
                                                               
      //erase the prev point
                                if(x1>0) drawnote(x1,y1);
                                if(x2>0) drawnote(x2,y2);
                                if(sharp1==1){
                                                 drawsharp(x1,y1);                 //draw sharp
                                                 sharp1=0;                              //change flag
                                } 
                                if(sharp2==1){
                                                 drawsharp(x2,y2);                 //draw sharp
                                                 sharp2=0;                              //change flag
                                }
                                if(flat1==1){
                                                 drawflat(x1,y1);                     //draw flat
                                                 flat1=0;                                  //change flag
                                }
                                if(flat2==1){
                                                 drawflat(x2,y2);                     //draw flat
                                                 flat2=0;                                  //change flag
                                }
                x1=0;   
                y1=0;
                x2=0;
                y2=0;
                                if(PINC.7==0){       // B3
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=27;                    //y1-position of note
                                                }
                                }     
               
                                if(PINC.6==0){                                    // B3 flat/A3 sharp               
                                                if(flatmode==1)                    //in flat mode
                                                begin
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=27;                    //y1-position of note
                                                                flat1=1;                   //note1 on x1, y1 has a flat
                                                 }      
                                                 else{                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=27;                   //y2-position of note
                                                                flat2=1;                   //note2 on x2, y2 has a flat
                                                }
                                                end
                                               
                                                else                                          //not in flat mode
                                                begin                              
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=30;                    //y1-position of note
                                                                sharp1=1;               //note1 on x1, y1 has a sharp
                                                }
                                                else{                                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=30;                    //y2-position of note
                                                                sharp2=1;               //note2 on x2, y2 has a sharp
                                                }
                                end
                                }   
                                 
                                if(PINC.5==0){  // A3   
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=30;                    //y1-position of note
                                                } 
                                                else{                                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=30;                    //y2-position of note
                                                }             
                                }  
                               
                                if(PINC.4==0){   // A3 flat/G2 sharp 
                                                if(flatmode==1)                    //in flat mode
                                                begin
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=30;                    //y1-position of note
                                                                flat1=1;                   //note on x1, y1 has a flat
                                                    }            
                                                    else{                                    //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=30;                    //y2-position of note
                                                                flat2=1;                   //note2 on x2, y2 has a flat
                                                    }
                                                end
                                               
                                                else                                          //not in flat mode
                                                begin   
                                                                if(x1==0){             //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=33;                    //y1-position of note
                                                                sharp1=1;               //note1 on x1, y1 has a sharp
                                                }
                                                else{                                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=33;                    //y2-position of note
                                                                sharp2=1;               //note2 on x2, y2 has a sharp
                                                }
                                end
                                } 
                                 
                                if(PINC.3==0){   // G2
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=33;                    //y1-position of note
                                                }           
                                                else{                                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=33;                    //y2-position of note
                                                }
                                }
                               
                                if(PINC.2==0){   // G2 flat/ F2 sharp     
                                                if(flatmode==1)                    //in flat mode
                                                begin
                                                   if(x1==0){                          //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=33;                    //y1-position of note
                                                                flat1=1;                   //note on x1, y1 has a flat
                                                    }            
                                                    else{                                    //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=33;                    //y2-position of note
                                                                flat2=1;                   //note2 on x2, y2 has a flat
                                                    }
                                                end
                                                else                                          //not in flat mode
                                                begin
                                                                if(x1==0){             //if no note allocated to x1, y1 yet
                                                                                x1=65;    //x1-position of note
                                                                                y1=36;   //y1-position of note
                                                                                sharp1=1; //note1 on x1, y1 has a sharp
                                                                }           
                                                                else{                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=36;                    //y2-position of note
                                                                sharp2=1;               //note2 on x2, y2 has a sharp
                                                                }
                                                end
                                }   
                               
                                if(PINC.1==0){   //F2    
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=36;                    //y1-position of note
                                                }           
                                                else{                                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=36;                    //y2-position of note
                                                }             
                                }             
                               
                                if(PINC.0==0){    // E2    
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=39;                    //y1-position of note
                                                }            
                                                else{                                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=39;                    //y2-position of note
                                                }
                                }    
                               
                                if(PINB.7==0){ // E2 flat/D2 sharp
                                                if(flatmode==1)                    //in flat mode
                                                begin
                                                   if(x1==0){                          //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=39;                    //y1-position of note
                                                                flat1=1;                   //note on x1, y1 has a flat
                                                    }            
                                                    else{                                    //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=39;                    //y2-position of note
                                                                flat2=1;                   //note2 on x2, y2 has a flat
                                                    }
                                                end  
                                               
                                                else                                          //not in flat mode
                                                begin
                                                                if(x1==0){             //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=41;                   //y1-position of note
                                                                sharp1=1;               //note1 on x1, y1 has a sharp
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=41;                    //y2-position of note
                                                                sharp2=1;               //note2 on x2, y2 has a sharp
                                                }
                                end         
                                }   
                               
                                if(PINB.6==0){    //D2
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=41;                    //y1-position of note
                                                }           
                                                else{                                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=41;                    //y2-position of note
                                                }
                                }   
                               
                                if(PINB.5==0){ //D2 flat/C2 sharp
                                                if(flatmode==1)                    //in flat mode
                                                begin
                                                   if(x1==0){                          //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=41;                    //y1-position of note
                                                                flat1=1;                   //note on x1, y1 has a flat
                                                    }            
                                                    else{                    //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=41;                    //y2-position of note
                                                                flat2=1;                   //note2 on x2, y2 has a flat
                                                    }
                                                end
                                               
                                                else                                          //not in flat mode
                                                begin
                                                                if(x1==0){             //if no note allocated to x1, y1 yet
                                                                                x1=65;    //x1-position of note
                                                                                y1=45;    //y1-position of note
                                                                                sharp1=1; //note1 on x1, y1 has a sharp
                                                                }           
                                                                else {                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                                x2=55;    //x2-position of note
                                                                                y2=45;    //y2-position of note
                                                                                sharp2=1; //note2 on x2, y2 has a sharp
                                                                }
                                end         
                                }   
                               
                                if(PINB.4==0){    //C2  
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=45;                    //y1-position of note
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=45;                    //y2-position of note
                                                }
                                }   
                               
                                if(PINB.3==0){    //B2
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=55;                    //y1-position of note
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=55;                    //y2-position of note
                                                }
                                }   
                               
                                if(PINB.2==0){    //B2 flat/A2 sharp          
                                                if(flatmode==1)                    //in flat mode
                                                begin
                                                   if(x1==0){                          //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=55;                    //y1-position of note
                                                                 flat1=1;                  //note on x1, y1 has a flat
                                                    }            
                                                    else{                                    //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=55;                    //y2-position of note
                                                                flat2=1;                   //note2 on x2, y2 has a flat
                                                    }
                                                end
                                                else                                          //not in flat mode
                                                begin
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=58;                    //y1-position of note
                                                                sharp1=1;               //note1 on x1, y1 has a sharp
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=58;                    //y2-position of note
                                                                sharp2=1;               //note2 on x2, y2 has a sharp
                                                }
                                end
                                }   
                               
                                if(PINB.1==0){    //A2
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=58;                    //y1-position of note
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=58;                    //y2-position of note
                                                }
                                }   
                               
                                if(PINB.0==0){    //A2 flat/ G1 sharp 
                                                if(flatmode==1)                    //in flat mode
                                                begin
                                                   if(x1==0){                          //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=58;                    //y1-position of note
                                                                flat1=1;                   //note on x1, y1 has a flat
                                                    }            
                                                    else{                                    //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=58;                    //y2-position of note
                                                                flat2=1;                   //note2 on x2, y2 has a flat
                                                    }
                                                end
                                               
                                                else                                          //not in flat mode
                                                begin
                                                                if(x1==0){             //if no note allocated to x1, y1 yet
                                                                                x1=65;    //x1-position of note
                                                                                y1=61;    //y1-position of note
                                                                                sharp1=1; //note1 on x1, y1 has a sharp
                                                                }           
                                                                else {                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                                x2=50;    //x2-position of note
                                                                                y2=61;    //y2-position of note
                                                                                sharp2=1; //note2 on x2, y2 has a sharp
                                                                }
                                                end         
                                                }             
                               
                                if(PINA.7==0){    //G1
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=61;                    //y1-position of note
                                                }            
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=61;                    //y2-position of note
                                                }
                                }    
                               
                                if(PINA.6==0){    //G1 flat/ F1 sharp
                                                if(flatmode==1)                    //in flat mode
                                                begin
                                                   if(x1==0){                          //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=61;                    //y1-position of note
                                                                flat1=1;                   //note on x1, y1 has a flat
                                                    }            
                                                    else{                                    //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=61;                    //y2-position of note
                                                                flat2=1;                   //note2 on x2, y2 has a flat
                                                    }
                                                end  
                                               
                                                else                                          //not in flat mode
                                                begin
                                                   if(x1==0){                          //if no note allocated to x1, y1 yet
                                                                x1=65;                    //x1-position of note
                                                                y1=64;                    //y1-position of note
                                                                sharp1=1;               //note1 on x1, y1 has a sharp
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=64;                    //y2-position of note
                                                                sharp2=1;               //note2 on x2, y2 has a sharp
                                                }
                                end
                                }  
                               
                                if(PINA.5==0){    //F1
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=64;                    //y1-position of note
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=64;                    //y2-position of note
                                                }
                                }               
                               
                                if(PINA.4==0){    //E1
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                 y1=67;                   //y1-position of note
                                                }           
                                                else{                                        //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=67;                    //y2-position of note
                                                }
                                }
                               
                                if(PINA.3==0){    //D1   
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=70;                    //y1-position of note
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                 y2=70;                   //y2-position of note
                                                }
                                }
                               
                                if(PINA.2==0){    // C1
                                                if(x1==0){                             //if no note allocated to x1, y1 yet
                                                                x1=60;                    //x1-position of note
                                                                y1=73;                    //y1-position of note
                                                }           
                                                else {                                       //if note already allocated to x1, y1, allocate to x2, y2
                                                                x2=50;                    //x2-position of note
                                                                y2=73;                    //y2-position of note
                                                }
                                }
                 
               
                                if(x1>0)  drawnote(x1,y1);                    //draw note if note allocated to x1, y1
                                if(x2>0) drawnote(x2,y2);   //draw note if note allocated to x2, y2
                                if(sharp1==1)        drawsharp(x1,y1); //draw sharp1 if sharp allocated to x1, y1
                                if(sharp2==1)   drawsharp(x2,y2);       //draw sharp2 if sharp allocated to x2, y2
                                if(flat1==1)            drawflat(x1,y1);     //draw flat1 if flat allocated to x1, y1
                                if(flat2==1)            drawflat(x2,y2);     //draw flat2 if flat allocated to x2, y2
               
      //**********************************************     
end
 
//================================== 
void gamePlay(void)
begin
                if(realtime>0)
                begin 
                                if(seconds<60) seconds++;
                                if(seconds==60){ 
                                                seconds=0;                             //reset “second” holder
                                                realtime--;                               //count down
                                                sprintf(rt,"%02d",realtime);  //print realtime
                                                video_putsmalls(56,90,rt);   //time in seconds
                                }
                                //print SCORE 
                                sprintf(sc,"%04d",score);
                                video_putsmalls(108,2,sc);
                                //erase the prev note and accidental
                                if(y1>0){                                                //if a note has been printed
                                                drawnote(60,y1);                    //erases note by toggling
                                                if(sharp1==1){                      //if sharp present
                                                                drawsharp(60,y1);  //erases sharp1 by toggling
                                                                sharp1=0;               //reset sharp1 flag
                                                } 
                                                if(flat1==1){                          //if flat 1 present
                                                                drawflat(60,y1);     //erases flat by toggling
                                                                flat1=0;                   //reset flat1 flag
                                                }
                                                y1=0;                                      //reset y1 to holding no note
                                   }
                                if(buttoncounter>0) buttoncounter--;               
                                else{
                                                if(PINA.0==0){                    //if PINA.0 is pushed
                                                                if(notetimer==119){            //set max value of timer to 59 if it is 119
                                                                                notetimer=59;
                                                                                sprintf(t,"%02d",2);  
                                                                }
                                                                else if(notetimer==59){       //set max value of timer to 119 if it is 49
                                                                                notetimer=49;
                                                                                sprintf(t,"%02d",3);  
                                                                }
                                                                else if(notetimer==49){       //set max value of timer to 119 if it is 49
                                                                                notetimer=119;
                                                                                sprintf(t,"%02d",1);  
                                                                }
                                                                video_putsmalls(112,90,t);  
                                                                buttoncounter=20;//variable holding counts between scanning PINA.0
                                                                score=0;                  //variable holding the score
                                                                realtime=MINUTE;//set the start time to 60 seconds
                                                }             
                                }
                                if(notecounter>0) notecounter--;
                                else {
                                                notecounter=notetimer;
                                                gamenote=1+TCNT0;   //random
                                                scored=0;
                                }
                                switch(gamenote){
                                                case 30:       // B3
                                                                y1=27;    //allocate y1 position for randomly chosen note
                                                                if(PINC.7==0){    //score if correct note is pressed for first time
                                                                                if(scored==0){
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;
                                                case 29:                    // B3 flat
                                                                y1=27;    //allocate y1 position for randomly chosen note
                                                                flat1=1;   
                                                                 if(PINC.6==0){//score if correct note is pressed for first time
                                                                                if(scored==0){
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;
                                                case 28:                    //A3 sharp
                                                                y1=30;    //allocate y1 position for randomly chosen note
                                                                sharp1=1;  
                                                                if(PINC.6==0){ //score if correct note is pressed for first time
                                                                                if(scored==0){
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;    
                                                case 27:  // A3   
                                                                y1=30;    //allocate y1 position for randomly chosen note
                                                                if(PINC.5==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }                             
                                                                break;
                                                case 26:      // A3 flat
                                                                y1=30;   //allocate y1 position for randomly chosen note                                                                                              flat1=1; 
                                                                if(PINC.4==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;
                                                case 25:      // G2 sharp
                                                                y1=33;    //allocate y1 for randomly chosen note                                                                                                              sharp1=1;
                                                                if(PINC.4==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;
                                                case 24:      // G2  
                                                                y1=33;    //allocate y1 position for randomly chosen note
                                                                if(PINC.3==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;
                                                case 23:     // G2 flat
                                                                y1=33;    //allocate y1 position for randomly chosen note
                                                                flat1=1; 
                                                                if(PINC.2==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break; 
                                                case 22:     // F2 sharp
                                                                y1=36;     //allocate y1 position for randomly chosen note
                                                                sharp1=1;
                                                                if(PINC.2==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;   
                                                case 21:    //F2   
                                                                y1=36;    //allocate y1 position for randomly chosen note
                                                                if(PINC.1==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;                     
                                                case 20:    // E2 
                                                                y1=39;    //allocate y1 position for randomly chosen note
                                                                if(PINC.0==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;     
                                                case 19: // E2 flat
                                                                y1=39;    //allocate y1 position for randomly chosen note
                                                                flat1=1; 
                                                                if(PINB.7==0){
                                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                                score++;  
                                                                                                scored=1;
                                                                                }
                                                                }
                                                                break;
                                case 18: //D2 sharp
                                                y1=41;     //allocate y1 position for randomly chosen note
                                                sharp1=1;
                                                if(PINB.7==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;     
                                case 17:    //D2      
                                                y1=41;    //allocate y1 position for randomly chosen note
                                                if(PINB.6==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;     
                                case 16: //D2 flat
                                                y1=41;    //allocate y1 position for randomly chosen note
                                                flat1=1; 
                                                if(PINB.5==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                 }
                                                break;     
                                case 15: // C2 sharp
                                                y1=45;    //allocate y1 position for randomly chosen note
                                                  sharp1=1;
                                                  if(PINB.5==0){
                                                                  if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                     }
                                                   }
                                                break;
                                case 14:    //C2 
                                                y1=45;    //allocate y1 position for randomly chosen note
                                                if(PINB.4==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                   }
                                                break;     
                                case 13:    //B2  
                                                y1=55;    //allocate y1 position for randomly chosen note
                                                if(PINB.3==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                  }
                                                break;     
                                case 12:    //B2 flat 
                                                y1=55;    //allocate y1 position for randomly chosen note
                                                flat1=1; 
                                                if(PINB.2==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                 }
                                                }
                                                break;
                                case 11:    //A2sharp
                                                y1=58;      //allocate y1 position for randomly chosen note
                                                sharp1=1;
                                                 if(PINB.2==0){
                                                                 if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;
                                case 10:    //A2     
                                                y1=58;    //allocate y1 position for randomly chosen note
                                                if(PINB.1==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;     
                                case 9:      //A2 flat
                                                y1=58;    //allocate y1 position for randomly chosen note
                                                flat1=1; 
                                                if(PINB.0==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;
                                case 8:      //G1 sharp
                                                y1=61;       //allocate y1 position for randomly chosen note
                                                sharp1=1;
                                                if(PINB.0==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;
                                case 7:      //G1      
                                                y1=61;    //allocate y1 position for randomly chosen note
                                                if(PINA.7==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;
                                case 6:      //G1 flat
                                                y1=61;    //allocate y1 position for randomly chosen note
                                                flat1=1; 
                                                if(PINA.6==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break; 
                                case 5:      //F1 sharp
                                                y1=64;    //allocate y1 position for randomly chosen note
                                                sharp1=1;
                                                if(PINA.6==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                                break;
                                case 4:      // F1
                                                y1=64;    //allocate y1 position for randomly chosen note
                                                if(PINA.5==0){
                                                                 if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;     
                                case 3:      // E1 
                                                y1=67;    //allocate y1 position for randomly chosen note
                                                if(PINA.4==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;
                                case 2:      // D1
                                                y1=70;    //allocate y1 position for randomly chosen note
                                                if(PINA.3==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;
                                case 1:      // C1
                                                y1= 73;   //allocate y1 position for randomly chosen note
                                                if(PINA.2==0){
                                                                if(scored==0){ //score if correct note is pressed for first time
                                                                                score++;  
                                                                                scored=1;
                                                                }
                                                }
                                                break;
                                default:
                                                y1=0;      //if no note is pushed                                        
                                                break;
                 }   
 
                 if(y1>0){
                                drawnote(60,y1);
                                if(sharp1==1)        drawsharp(60,y1);
                                else if(flat1==1)     drawflat(60,y1);    
                 }   
               
  end
  else{    
                                video_putsmalls(12,90,game); //"game"
                                video_putsmalls(32,90,over); //"over" 
                   if(buttoncounter>0) buttoncounter--;
                   else{
                       if(PINA.0==0){
                                 if(notetimer==119){
                                                notetimer=59;
                                                sprintf(t,"%02d",2);  
                                                }
                                else if(notetimer==59){
                                                notetimer=49;
                                                sprintf(t,"%02d",3);  
                                }
                                else if(notetimer==49){
                                                notetimer=119;
                                                sprintf(t,"%02d",1);  
                                }
                                video_putsmalls(112,90,t);  
                                buttoncounter=20;
                                score=0; 
                                realtime=MINUTE;
                                 video_putsmalls(12,90,time); //"time"
                                video_putsmalls(32,90,left); //"left"
                  }           
                 }
  }
end
 
//================================== 
void gamePlayChords(void) //the scheme is similar to the previous game
begin       
                if(realtime>0)        //while time is not yet up
                begin 
                                if(seconds<60) seconds++;
                                if(seconds==60){ 
                                                seconds=0;
                                                realtime--;
                                                sprintf(rt,"%02d",realtime);  //format number to string
                                                video_putsmalls(56,90,rt); //time in seconds
                                }
                               //print SCORE 
                              sprintf(sc,"%04d",score);                    //format number to string
                              video_putsmalls(108,2,sc);
                                //erase the prev point
                               if(y1>0){
                                                 drawnote(60,y1);
               
                                                  if(sharp1==1){
                                                               drawsharp(60,y1);    
                                                                sharp1=0;            //reset flag
                                                  } 
                                                  if(flat1==1){
                                                                drawflat(60,y1);    
                                                                 flat1=0;                //reset flag
                                                  }
                                                 y1=0;
                               }  
                               if(y2>0){
                                                drawnote(80,y2);
               
                                               if(sharp2==1){
                                                   drawsharp(80,y2);    
                                                   sharp2=0;                   //reset flag
                                                } 
                                               if(flat2==1){
                                                    drawflat(80,y2);    
                                                    flat2=0;                    //reset flag
                                                }
                                                y2=0;
                                   }
                 if(buttoncounter>0) buttoncounter--;
                 else{
                       if(PINA.0==0){
                            if(notetimer==119){
                                notetimer=59;
                                sprintf(t,"%02d",2);                  //format number to string
                    }
                    else if(notetimer==59){
                                notetimer=49;
                                sprintf(t,"%02d",3);                 //format number to string
                    }
                    else if(notetimer==49){
                                notetimer=119;
                                sprintf(t,"%02d",1);                  //format number to string
                    }
                       video_putsmalls(112,90,t);  
                      buttoncounter=20;
                      score=0;                                              //reset score
                        realtime=MINUTE;                        //reset flag
         }    
                 }
               
     if(notecounter>0) notecounter--;               
     else {
                 notecounter=notetimer;
                 gamenote=1+TCNT0;                                      //random
                 scored=0;                                                        //reset flag    
                 scored2=0;                                                     //reset flag
                 gamenote2=gamenote^0x5;
     }
    
    
     switch(gamenote){
    
                case 30:       // B3
                      y1=27;
                        if(PINC.7==0){                                    //correct key pressed
                             if(scored==0)   scored=1;             //first note scored
                        }
                       break;
                case 29:                    // B3 flat
                      y1=27;
                       flat1=1;   
                     if(PINC.6==0){                                    //correct key pressed
                                 if(scored==0)   scored=1;              //first note scored
                     }
                     break;
             case 28:                 //A3 sharp
                      y1=30;
                      sharp1=1;  
                       if(PINC.6==0){                                    //correct key pressed
                                 if(scored==0) scored=1;              //first note scored
                       }
                       break;    
            case 27:  // A3   
                     y1=30; 
                    if(PINC.5==0){                                    //correct key pressed
                                 if(scored==0)      scored=1;              //first note scored
                    }                          
                    break;
          case 26:      // A3 flat
                       y1=30;  
                      flat1=1; 
                      if(PINC.4==0){                                    //correct key pressed
                           if(scored==0)             scored=1;              //first note scored
                     }
                      break;
          case 25:      // G2 sharp
                     y1=33; 
                    sharp1=1;
                    if(PINC.4==0){                                    //correct key pressed
                           if(scored==0)             scored=1;              //first note scored
                    }
                    break;
           case 24:      // G2  
                   y1=33;
                    if(PINC.3==0){                                    //correct key pressed
                           if(scored==0)             scored=1;              //first note scored
                   }
                   break;
           case 23:     // G2 flat
                   y1=33;
                     flat1=1; 
                    if(PINC.2==0){                                    //correct key pressed
                            if(scored==0)            scored=1;              //first note scored
                     }
                   break; 
        case 22:     // F2 sharp
                  y1=36;    
                  sharp1=1;
                   if(PINC.2==0){                                    //correct key pressed
                            if(scored==0)            scored=1;              //first note scored
                    }
                  break;   
       case 21:    //F2   
                    y1=36;
                   if(PINC.1==0){                                    //correct key pressed
                            if(scored==0)            scored=1;              //first note scored
                    }
                     break;                     
      case 20:    // E2 
                   y1=39;
                   if(PINC.0==0){                                    //correct key pressed
                            if(scored==0)            scored=1;              //first note scored
                     }
                     break;     
  case 19: // E2 flat
                   y1=39; 
                    flat1=1; 
                  if(PINB.7==0){                                    //correct key pressed
                                 if(scored==0)      scored=1;              //first note scored
                   }
                  break;
 case 18: //D2 sharp
                y1=41;    
                sharp1=1;
                 if(PINB.7==0){                                    //correct key pressed
                              if(scored==0)      scored=1;              //first note scored
                  }
                break;     
case 17:    //D2      
                  y1=41;
                  if(PINB.6==0){                                    //correct key pressed
                         if(scored==0)      scored=1;              //first note scored
                  }
                  break;     
 case 16: //D2 flat
                 y1=41;    
                flat1=1; 
              if(PINB.5==0){                                    //correct key pressed
                         if(scored==0)   scored=1;              //first note scored
              }
              break;     
case 15: // C2 sharp
                y1=45;
               sharp1=1;
             if(PINB.5==0){                                    //correct key pressed
                          if(scored==0)   scored=1;              //first note scored
                 }
                  break;
 case 14:    //C2 
                   y1=45;
                  if(PINB.4==0){                                    //correct key pressed
                          if(scored==0)   scored=1;              //first note scored
                   }
                  break;     
case 13:    //B2  
                  y1=55;
                   if(PINB.3==0){                                    //correct key pressed
                        if(scored==0)   scored=1;              //first note scored
                    }
                 break;     
case 12:    //B2 flat 
                     y1=55;
                      flat1=1; 
                       if(PINB.2==0){                                    //correct key pressed
                                     if(scored==0)   scored=1;              //first note scored
                       }
                    break;
case 11:    //A2sharp
                     y1=58;     
                       sharp1=1;
                       if(PINB.2==0){                                    //correct key pressed
                              if(scored==0)   scored=1;              //first note scored
                       }
                   break;
case 10:    //A2     
                    y1=58;
                    if(PINB.1==0){                                    //correct key pressed
                                if(scored==0)        scored=1;              //first note scored
                     }
                    break;     
  case 9:      //A2 flat
                    y1=58;
                     flat1=1; 
                     if(PINB.0==0){                                    //correct key pressed
                        if(scored==0)   scored=1;              //first note scored
                   }
                   break;
 case 8:                //G1 sharp
                   y1=61;      
                   sharp1=1;
                     if(PINB.0==0){                                    //correct key pressed
                          if(scored==0)   scored=1;              //first note scored
                     }
                     break;
case 7:      //G1      
                  y1=61;
                    if(PINA.7==0){                                    //correct key pressed
                            if(scored==0)   scored=1;              //first note scored
                      }
                    break;
case 6:      //G1 flat
                  y1=61;
                  flat1=1; 
                  if(PINA.6==0){                                    //correct key pressed
                         if(scored==0)                scored=1;              //first note scored
                     }
                         break; 
case 5:      //F1 sharp
                  y1=64;
                   sharp1=1;
                   if(PINA.6==0){                                    //correct key pressed
                               if(scored==0)                scored=1;              //first note scored
                   }
                  break;
case 4:      // F1
                   y1=64;
                   if(PINA.5==0){                                    //correct key pressed
                                  if(scored==0)      scored=1;              //first note scored
                      }
                   break;     
case 3:      // E1 
                   y1=67;
                     if(PINA.4==0){                                    //correct key pressed
                               if(scored==0)   scored=1;              //first note scored
                     }
                    break;
case 2:      // D1
                     y1=70;   
                     if(PINA.3==0){                                    //correct key pressed
                               if(scored==0)      scored=1;              //first note scored
                     }
                   break;
 case 1:      // C1
                    y1= 73;
                    if(PINA.2==0){                                    //correct key pressed
                       if(scored==0)   scored=1;              //first note scored
                    }
                   break;
 default:
                     y1=0;
                 break;
 }               
                 
     switch(gamenote2){
    
                case 30:       // B3
                                      y2=27;
                                    if(PINC.7==0){                                    //correct key pressed
                                            if(scored2==0) scored2=1;              //second note scored
                                     }
                                     break;
                case 29:                    // B3 flat
                                    y2=27;
                                    flat2=1;   
                                   if(PINC.6==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                   break;
                   case 28:                 //A3 sharp
                                     y2=30;
                                     sharp2=1;  
                                   if(PINC.6==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                    break;    
                   case 27:  // A3   
                                    y2=30; 
                                     if(PINC.5==0){                                    //correct key pressed
                                             if(scored2==0)    scored2=1;               //second note scored
                                   }                          
                                    break;
                   case 26:      // A3 flat
                                        y2=30;  
                                     flat2=1; 
                                     if(PINC.4==0){                                    //correct key pressed
                                       if(scored2==0)           scored2=1;               //second note scored
                                   }
                                  break;
                   case 25:      // G2 sharp
                                     y2=33;  
                                      sharp2=1;
                                     if(PINC.4==0){                                    //correct key pressed
                                       if(scored2==0)           scored2=1;               //second note scored
                                   }
                                  break;
                     case 24:      // G2  
                                   y2=33;
                                 if(PINC.3==0){                                    //correct key pressed
                                       if(scored2==0)           scored2=1;               //second note scored
                                   }
                                  break;
                  case 23:     // G2 flat
                                      y2=33;
                                       flat2=1; 
                                     if(PINC.2==0){                                    //correct key pressed
                                                if(scored2==0)          scored2=1;               //second note scored
                                   }
                                   break; 
                   case 22:     // F2 sharp
                                      y2=36;    
                                      sharp2=1;
                                     if(PINC.2==0){                                    //correct key pressed
                                        if(scored2==0)          scored2=1;               //second note scored
                                   }
                                     break;   
                   case 21:    //F2   
                                      y2=36;
                                     if(PINC.1==0){                                    //correct key pressed
                                                  if(scored2==0)          scored2=1;               //second note scored
                                   }
                                    break;                     
                   case 20:    // E2 
                                    y2=39;
                                     if(PINC.0==0){                                    //correct key pressed
                                          if(scored2==0)          scored2=1;               //second note scored
                                    }
                                    break;     
                case 19: // E2 flat
                                  y2=39; 
                                  flat2=1; 
                                    if(PINB.7==0){                                    //correct key pressed
                                                  if(scored2==0)    scored2=1;               //second note scored
                                   }
                                    break;
                  case 18: //D2 sharp
                                     y2=41;    
                                    sharp2=1;
                                      if(PINB.7==0){                                    //correct key pressed
                                                  if(scored2==0)    scored2=1;               //second note scored
                                   }
                                  break;     
                  case 17:    //D2      
                                   y2=41;
                                   if(PINB.6==0){                                    //correct key pressed
                                                  if(scored2==0)    scored2=1;               //second note scored
                                   }
                                     break;     
              case 16: //D2 flat
                                    y2=41;    
                                   flat2=1; 
                                   if(PINB.5==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;              //second note scored
                                   }
                                   break;     
                case 15: // C2 sharp
                                     y2=45;
                                   sharp2=1;
                                   if(PINB.5==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                   break;
               case 14:    //C2 
                                   y2=45;
                                   if(PINB.4==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                   break;     
                case 13:    //B2  
                                 y2=55;
                                   if(PINB.3==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                    break;     
                case 12:    //B2 flat 
                                     y2=55;
                                     flat2=1; 
                                   if(PINB.2==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                   break;
                 case 11:    //A2sharp
                                  y2=58;     
                                  sharp2=1;
                                   if(PINB.2==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                   break;
                case 10:    //A2     
                                 y2=58;
                                 if(PINB.1==0){                                    //correct key pressed
                                       if(scored2==0)           scored2=1;               //second note scored
                                  }
                                   break;     
                  case 9:      //A2 flat
                                     y2=58;
                                    flat2=1; 
                                   if(PINB.0==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                   break;
                  case 8:                //G1 sharp
                                y2=61;      
                                sharp2=1;
                                   if(PINB.0==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                     break;
                case 7:      //G1      
                                     y2=61;
                                   if(PINA.7==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                    break;
                case 6:      //G1 flat
                                     y2=61;
                                     flat2=1; 
                                      if(PINA.6==0){                                    //correct key pressed
                                        if(scored2==0)              scored2=1;               //second note scored
                                   }
                                    break; 
                 case 5:      //F1 sharp
                                 y2=64;
                                sharp2=1;
                                    if(PINA.6==0){                                    //correct key pressed
                                        if(scored2==0)              scored2=1;               //second note scored
                                   }
                                   break;
                case 4:      // F1
                                    y2=64;
                                      if(PINA.5==0){                                    //correct key pressed
                                             if(scored2==0)    scored2=1;               //second note scored
                                   }
                               break;     
                case 3:      // E1 
                                   y2=67;
                                   if(PINA.4==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                   break;
                case 2:      // D1
                                   y2=70;   
                                     if(PINA.3==0){                                    //correct key pressed
                                             if(scored2==0)    scored2=1;               //second note scored
                                   }
                                    break;
                 case 1:      // C1
                                    y2= 73;
                                   if(PINA.2==0){                                    //correct key pressed
                                     if(scored2==0) scored2=1;               //second note scored
                                   }
                                    break;
                default:
                                   y2=0;
                                    break;
                 }     
                 
                 if(y1>0){
                                drawnote(60,y1);
                                if(sharp1==1)        drawsharp(60,y1);
                                else if(flat1==1)     drawflat(60,y1);    
                 }   
                 if(y2>0){
                                drawnote(80,y2);
                                if(sharp2==1)        drawsharp(80,y2);
                                else if(flat2==1)     drawflat(80,y2);    
                 }
     if(scored==1 && scored2==1){            //only if both notes are correctly played is the score incremented
                 score++;     
                 scored=2;
                 scored2=2;       
     } 
  end  
  else{    
                                video_putsmalls(12,90,game); //"game"
                                video_putsmalls(32,90,over); //"over" 
                   if(buttoncounter>0) buttoncounter--;
                   else{
                       if(PINA.0==0){        //allow to change mode and start again upon button push
            if(notetimer==119){         
                notetimer=59;
                sprintf(t,"%02d",2);   //format number to string
                }
                else if(notetimer==59){
                                notetimer=49;
                                sprintf(t,"%02d",3);    //format number to string
                }
                else if(notetimer==49){
                                notetimer=119;
                                sprintf(t,"%02d",1);    //format number to string
                }
                                                video_putsmalls(112,90,t);  
                                                buttoncounter=20;
                                                score=0; 
                                                realtime=MINUTE;
                                                video_putsmalls(12,90,time); //"time"
                                                video_putsmalls(32,90,left); //"left"
         }    
                 }
  }   
end
 
//==================================
 
 
 
// set up the ports and timers
void main(void)
begin
  //init timer 1 to generate sync
  TCCR1B = 9;                       //full speed; clear-on-match
  TCCR1A = 0x00;                //turn off pwm and oc lines
  TIMSK = 0x10;                   //enable interrupt T1 cmp
  OCR1A = lineTime;           //One NTSC line
 
  //init timer 0 to generate random numbers
  TCCR0=0b00001101;
  OCR0=29;
 
 
  //init ports
  DDRD = 0xf0;                    //video out and switches
  //D.5 is sync:1000 ohm + diode to 75 ohm resistor
  //D.6 is video:330 ohm + diode to 75 ohm resistor
 
  //set up the ports  
  DDRA=0x00;      // PORT A is an input with pullup
  PORTA=0xff;
  DDRB=0x00;      // PORT B is an input with pullup
  PORTB=0xff; 
  DDRC=0x00;      // PORT C is an input with pullup
  PORTC=0xff;            
 
 
 
  //initialize synch constants
  LineCount = 1;
  syncON = 0b00000000;
  syncOFF = 0b00100000; 
 
 
  sharp1=0;                //initialize flags
  sharp2=0;
  x1=0;
  x2=0;
  y1=0;
  y2=0;
  flatmode=0;   
  gamemode=0;
  flatcounter=0;  
  gamecounter=0;
  gamenote=0;
  gamenote2=0;
  notecounter=0;   
  score=0;
  scored=0;
  scored2=0;
  notetimer=0; 
  buttoncounter=20;
  realtime=0;
  seconds=0;
   drawstaff();
 
 
  video_puts(3,1,text1);                           //Print "MUSIC"
  //Print "NOTES"
  if(gamemode==1) video_puts(35,1,test1);  
  else if(gamemode==2) video_puts(35,1,test2);     
  else video_puts(35,1,text2);
  //print MODE
   video_putsmalls(108,2,text5);
                if(flatmode==1)    video_putsmalls(80,2,text3);       
                else             video_putsmalls(80,2,text4);    
  video_putsmalls(12,90,menu);             //"MENU"
  video_putsmalls(32,90,flat);             //"FLAT"
  video_putsmalls(56,90,xx);                     //"XX"
  video_putsmalls(112,90,yy);                       //"YY"
  video_putsmalls(88,90,games);                //"GAMES"
 
  //enable sleep mode
  MCUCR = 0b10000000;
  #asm ("sei");
 
  //The following loop executes once/video line during lines
  //1-230, then does all of the frame-end processing
  while(1)
  begin
 
    //stall here until next line starts
    //sleep enable; mode=idle 
    //use sleep to make entry into sync ISR uniform time 
    
    #asm ("sleep");
   
    //The following code executes during the vertical blanking
    //Code here can be as long as 
    //a total of 60 lines x 63.5 uSec/line x 8 cycles/uSec
   
    if (LineCount==231)
    begin                 
                                if(PINA.1==0){    // //if button pushed, change mode
                                                if(gamecounter<20) gamecounter++;       
                                                else{
                                                                if(gamemode==0){
                                                                                gamemode=1;            //"music Test 1"
                                                                                score=0;
                                                                                realtime=MINUTE; 
                                                                                sprintf(rt,"%02d",realtime);
                                                                                                video_putsmalls(56,90,rt); //time in seconds
                                                                                seconds=0;
                        notetimer=119;
                                                                                buttoncounter=0;
                                                                                sprintf(t,"%02d",1);   
                                                                                                video_putsmalls(112,90,t);
                                                                                                video_putsmalls(88,90,timertext);   
                                                                  
                                                                                  //Print "TEST1" and "SCORE"
                                                                                                video_puts(35,1,test1); 
                                                                                                video_putsmalls(80,2,scoretext);
                                                                                                video_putsmalls(12,90,time); //"time"
                                                                                                video_putsmalls(32,90,left); //"left"
                                                                }
                                                                else if(gamemode==1){   
                                                                                gamemode=2;         //"Music Test 2"
                                                                                //print SCORE 
                                                                                                sprintf(sc,"%04d",score);
                                                                                                video_putsmalls(108,2,sc);
                                                                                //erase the prev point
                                                                                                if(y1>0){
                                                                                                                drawnote(60,y1);
               
                                                                                                                if(sharp1==1){
                                                                                                                                drawsharp(60,y1);    
                                                                                                                                sharp1=0;
                                                                                                                } 
                                                                                                   else if(flat1==1){
                                                                                                                                 drawflat(60,y1);    
                                                                                                                                 flat1=0;
                                                                                                                }      
                                                                                                                y1=0;
                                                                                                }                                                                
                                                                                score=0;           
                                                                                realtime=MINUTE; 
                                                                                sprintf(rt,"%02d",realtime);
                                                                                                video_putsmalls(56,90,rt); //time in seconds     
                                                                                seconds=0;
                                                                                notetimer=119;
                                                                                buttoncounter=0;
                                                                                sprintf(t,"%02d",1);     
                                                                                                video_putsmalls(112,90,t);
                                                                                                video_putsmalls(88,90,timertext); 
                                                                  
                                                                                  //Print "TEST2" and "SCORE"
                                                                                video_puts(35,1,test2);   
                                                                                video_putsmalls(80,2,scoretext);    
                                                                                video_putsmalls(12,90,time); //"time"
                                                                                                video_putsmalls(32,90,left); //"left"
                                                                }
                                                                else{   
                                                                                //print SCORE 
                                                                                                sprintf(sc,"%04d",score);
                                                                                                video_putsmalls(108,2,sc);
                                                                                //erase the prev point
                                                                                                if(y1>0){
                                                                                                                drawnote(60,y1);
               
                                                                                                                if(sharp1==1){
                                                                                                                                drawsharp(60,y1);    
                                                                                                                                sharp1=0;
                                                                                                                } 
                                                                                                   else if(flat1==1){
                                                                                                                                 drawflat(60,y1);    
                                                                                                                                 flat1=0;
                                                                                                                }      
                                                                                                  y1=0;
                                                                                                }
                                                                                                if(y2>0){
                                                                                                                drawnote(80,y2);
               
                                                                                                                if(sharp2==1){
                                                                                                                                drawsharp(80,y2);    
                                                                                                                                sharp2=0;
                                                                                                                } 
                                                                                                                if(flat2==1){
                                                                                                                                 drawflat(80,y2);    
                                                                                                                                 flat2=0;
                                                                                                                }
                                                                                                                y2=0;
                                                                                                 }
                                                                                            gamemode=0;       //display mode
                                                                                                video_puts(35,1,text2);  //Print "NOTES"
                                                                                                video_putsmalls(12,90,menu); //"MENU"
                                                                                                video_putsmalls(32,90,flat); //"FLAT"
                                                                                                video_putsmalls(56,90,xx); //"XX"        
                                                                                                video_putsmalls(112,90,yy);   //"YY"
                                                                                                video_putsmalls(88,90,games);    //"GAMES"
                                                                                }
                                                                gamecounter=0;    // reset counter
                                                }
                   }//if
                   else{
                                                if(gamemode==1)   gamePlay();                      //music test1    
 
                                                else if(gamemode==2)  gamePlayChords();     //music test2   
                                               
                                                else  displaynotes(); //music display mode
          }
 
     end  //line 231
  end  //while
end  //main