#include "EEPROM.h"
#include <NmraDcc.h>
NmraDcc  Dcc ;
DCC_MSG  Packet ;

int numag1=1;
int numag2=2;



int octet;
int octet0=10;
int posag;
int posag1=1;
int posag2=1;


int nag;
int nag0=0;
int a;
int b;
int c=0;

long t=micros();
long dt=1500;
long t0=20000;
long dt0=1500;
long dt1max=1900;
long dt1min=1300;
long dt2max=1900;
long dt2min=1300;

int n=0;
int nmax=20; // vitesse de déplacement de l'aiguille entre 1 et 20
int x;
int y;
int servo=1;



// This function is called whenever a normal DCC Turnout Packet is received
void notifyDccAccState( uint16_t Addr, uint16_t BoardAddr, uint8_t OutputAddr, uint8_t State)
{
 
nag=Addr;  

 if (Addr==numag1) {
    a=Addr-4*int((Addr-1)/4);
    b=2*(a-1);
    posag1= OutputAddr -b;
   }  
   
   if (Addr==numag2) {
    a=Addr-4*int((Addr-1)/4);
    b=2*(a-1);
    posag2= OutputAddr -b;
   }  

}









void setup() {
  

  
  Serial.begin(9600);
 
  pinMode(13, OUTPUT); 
 
  
  digitalWrite(13, LOW); 
numag1=EEPROM.read(2);
numag2=EEPROM.read(6);
x=EEPROM.read(3);
dt1max=1000+x*100;
y=EEPROM.read(4);
dt1min=1000+y*100;
nmax=EEPROM.read(5);
x=EEPROM.read(7);
dt2max=1000+x*100;
y=EEPROM.read(8);
dt2min=1000+y*100;



Serial.println("NMRA DCC Example 1");
  // Call the main DCC Init function to enable the DCC Receiver
  Dcc.init( MAN_ID_DIY, 10, FLAGS_OUTPUT_ADDRESS_MODE | FLAGS_DCC_ACCESSORY_DECODER | FLAGS_ENABLE_INT0_PULL_UP, 0 );
  Serial.println("Init Done");
}

void loop()
{
 Dcc.process(); 

if (Serial.available() > 0) {
    octet = Serial.read();
    Serial.write(octet);
     if ( octet==49) {
      servo=1;
     }
      if ( octet==50) {
      servo=2;
     }
    if ( servo==1 and octet>0 and octet<17) {
      numag1=octet;
      EEPROM.write(2, numag1); 
      if (numag1==1) digitalWrite(13, HIGH); 
      
    }
     if ( servo==2 and octet>0 and octet<17) {
      numag2=octet;
      EEPROM.write(6, numag2); 
      }
    if (servo==1 and octet==17) {
      dt=1600;
      posag=3;
       EEPROM.write(3, 6); 
      }
    if (servo==1 and octet==18) {
      dt=1700;
      posag=3;
       EEPROM.write(3, 7); 
      }
     if (servo==1 and octet==19) {
      dt=1800;
      posag=3;
       EEPROM.write(3, 8); 
      }
      
     if (servo==1 and octet==20) {
      dt=1900;
      posag=3;
       EEPROM.write(3, 9); 
      }
      
       if (servo==1 and octet==21) {
      dt=2000;
      posag=3;
       EEPROM.write(3, 10); 
      }
      
        if (servo==1 and octet==22) {
      dt=1400;
      posag=3;
      EEPROM.write(4, 4); 
      }
      
         if (servo==1 and octet==23) {
      dt=1300;
      posag=3;
      EEPROM.write(4, 3); 
      }
      
         if (servo==1 and octet==24) {
      dt=1200;
      posag=3;
      EEPROM.write(4, 2); 
      }
      if (servo==1 and octet==25) {
      dt=1100;
      posag=3;
      EEPROM.write(4, 1); 
      }
        if (servo==1 and octet==26) {
      dt=1000;
      posag=3;
      EEPROM.write(4, 0); 
      }
        if (octet==27) {
      nmax=20;
      posag=3;
      EEPROM.write(5, nmax); 
      }
      
         if (octet==28) {
      nmax=15;
      posag=3;
      EEPROM.write(5, nmax); 
      }
           if (octet==29) {
      nmax=10;
      posag=3;
      EEPROM.write(5, nmax); 
      }
              if (octet==30) {
      nmax=5;
      posag=3;
      EEPROM.write(5, nmax); 
      }
      
      if (servo==2 and octet==17) {
      dt=1600;
      posag=3;
       EEPROM.write(7, 6); 
      }
    if (servo==2 and octet==18) {
      dt=1700;
      posag=3;
       EEPROM.write(7, 7); 
      }
     if (servo==2 and octet==19) {
      dt=1800;
      posag=3;
       EEPROM.write(7, 8); 
      }
      
     if (servo==2 and octet==20) {
      dt=1900;
      posag=3;
       EEPROM.write(7, 9); 
      }
      
       if (servo==2 and octet==21) {
      dt=2000;
      posag=3;
       EEPROM.write(7, 10); 
      }
      
        if (servo==2 and octet==22) {
      dt=1400;
      posag=3;
      EEPROM.write(8, 4); 
      }
      
         if (servo==2 and octet==23) {
      dt=1300;
      posag=3;
      EEPROM.write(8, 3); 
      }
      
         if (servo==2 and octet==24) {
      dt=1200;
      posag=3;
      EEPROM.write(8, 2); 
      }
      if (servo==2 and octet==25) {
      dt=1100;
      posag=3;
      EEPROM.write(8, 1); 
      }
        if (servo==2 and octet==26) {
      dt=1000;
      posag=3;
      EEPROM.write(8, 0); 
      }
               
                 
            } 

if (nag==numag1 && posag1==1) {
digitalWrite(13,HIGH);
if (micros()<t+dt && dt<dt1max) {
    digitalWrite(9,HIGH);
    n=n+1;
    if (n==nmax) {
        dt=dt+1;
        n=0;
    }

    
}
if (micros()>t+dt && micros()<t+t0) digitalWrite(9,LOW);
if (micros()>t+t0) t=micros();


}

if (nag==numag1 && posag1==0) {
digitalWrite(13,LOW);
if (micros()<t+dt && dt>dt1min) {
    digitalWrite(9,HIGH);
    n=n+1;
    if (n==nmax) {
        dt=dt-1;
        n=0;
    }

}
if (micros()>t+dt && micros()<t+t0) digitalWrite(9,LOW);
if (micros()>t+t0) t=micros();
  
}

if (nag==numag2 && posag2==1) {
if (micros()<t+dt && dt<dt2max) {
    digitalWrite(8,HIGH);
    n=n+1;
    if (n==nmax) {
        dt=dt+1;
        n=0;
    }

    
}
if (micros()>t+dt && micros()<t+t0) digitalWrite(8,LOW);
if (micros()>t+t0) t=micros();


}

if (nag==numag2 && posag2==0) {
if (micros()<t+dt && dt>dt2min) {
    digitalWrite(8,HIGH);
    n=n+1;
    if (n==nmax) {
        dt=dt-1;
        n=0;
    }

}
if (micros()>t+dt && micros()<t+t0) digitalWrite(8,LOW);
if (micros()>t+t0) t=micros();
  
}




if (posag==3) {

if (micros()<t+dt ) {
    digitalWrite(9,HIGH);
   
    }

    

if (micros()>t+dt && micros()<t+t0) digitalWrite(9,LOW);
if (micros()>t+t0) t=micros();


}

}
