// r.derderian pour f6krd (le 21/03/2017) 
//reception Orbitron/PstRotator interface protocole Easycom
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

//variable boutons manuels
int8_t btcw;
int8_t btccw;
int8_t btaux; 
int8_t aux1; 
int8_t aux2;

int8_t bcw=8;
int8_t bccw=7;
int8_t baux=6; 

int8_t m1=0;
int8_t m2=0;

int8_t cw=3;   //in2
int8_t ccw=4;  //in3
int8_t brake=2;//in1
int8_t aux=5;  //in4
int8_t pcap;

int pazim=A0;
int paz1=0;
int Dir=0;
int w;//valeur pot azim
int x;//valeur azim pour comparaison

void setup() {
    Wire.begin();  //I2C bus
    lcd.begin();     
    Serial.begin(9600);
    lcd.clear();   
  //**********************************
lcd.backlight();
pinMode(cw, OUTPUT);    //relai cw 3
pinMode(ccw, OUTPUT);   //relai ccw 4
pinMode(brake, OUTPUT); //relai frein 2
pinMode(aux, OUTPUT);   //relai aux 5

pinMode(bcw, INPUT);    //bt cw
digitalWrite(bcw,HIGH);
pinMode(bccw, INPUT);   //bt ccw
digitalWrite(bccw,HIGH);
pinMode(baux, INPUT);   //bt aux
digitalWrite(baux,HIGH);
pinMode(9, INPUT); //manuel/logiciel
digitalWrite(9,HIGH); 

lcd.setCursor(15, 1);lcd.print("B");

digitalWrite(brake,HIGH);delay(10);//bloque le frein
digitalWrite(cw,HIGH);delay(10);
digitalWrite(ccw,HIGH);delay(10);
digitalWrite(aux,HIGH);

lcd.setCursor(3, 0);  lcd.print ("F6KRD HAMIV");delay(3000);lcd.setCursor(2, 0);  lcd.print ("------#------");
}

void loop() {
     pcap=digitalRead(9);//Serial.print(pcap);
     //position antenne
     if(pcap==HIGH){
      manuel();     
     }
     if(pcap==LOW){
      automat(); 
     }
     potazim();
}


void potazim(){
      paz1=analogRead(pazim);
      Dir=paz1;//Serial.println(paz1);
      positionP();
}


void positionP(){   //potentiometre 500 ohms

   if (Dir >=361){
   lcd.setCursor(5, 1);lcd.print ("S ");//00-360
   lcd.setCursor(2, 0);lcd.print ("S");
   lcd.setCursor(3, 0);lcd.print ("-");   
   } else
   if (Dir <= 0){
   lcd.setCursor(5, 1);lcd.print ("S ");//00-360
   lcd.setCursor(13, 0);lcd.print ("-");
   lcd.setCursor(14, 0);lcd.print ("S");   
   } else
   
//------------------ 
   if ((Dir >=4)and (Dir <= 44)){
   lcd.setCursor(5, 1); lcd.print ("SE");//38
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("-");
   lcd.setCursor(13, 0);lcd.print ("I");   
   lcd.setCursor(12, 0);lcd.print ("-"); 
   }else
   if ((Dir >=45)and (Dir <= 74)){
   lcd.setCursor(5, 1); lcd.print ("SE");//60
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(13, 0);lcd.print ("-");  
   lcd.setCursor(12, 0);lcd.print ("I"); 
   lcd.setCursor(11, 0);lcd.print ("-");   
   }else
//------------------    
   if ((Dir >=75) and (Dir <= 104)){
   lcd.setCursor(5, 1); lcd.print ("E ");//90  
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(12, 0);lcd.print ("-");
   lcd.setCursor(11, 0);lcd.print ("E"); 
   lcd.setCursor(10, 0);lcd.print ("-");
   }else
//------------------    
   if ((Dir >=105)and (Dir <= 134)){ 
   lcd.setCursor(5, 1); lcd.print ("NE"); //120
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(11, 0);lcd.print ("-");
   lcd.setCursor(10, 0);lcd.print ("I");
   lcd.setCursor(9, 0);lcd.print ("-"); 
   }else
   if ((Dir >=135)and (Dir <= 164)){ 
   lcd.setCursor(5, 1); lcd.print ("NE"); //150
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(10, 0);lcd.print ("-");
   lcd.setCursor(9, 0);lcd.print ("I");
   lcd.setCursor(8, 0);lcd.print ("#");
   }else
//------------------    
   if ((Dir >=165)and (Dir <= 194)){
   lcd.setCursor(5, 1); lcd.print ("N ");//180
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(9, 0);lcd.print ("-");
   lcd.setCursor(8, 0);lcd.print ("N");
   lcd.setCursor(7, 0);lcd.print ("-");
   }else
//------------------   
   if ((Dir >=195)and (Dir <= 224)){
   lcd.setCursor(5, 1);  lcd.print ("NO"); //210
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(8, 0);lcd.print ("-");
   lcd.setCursor(7, 0);lcd.print ("I"); 
   lcd.setCursor(6, 0);lcd.print ("-");
   }else
   if ((Dir >=225)and (Dir <= 254)){
   lcd.setCursor(5, 1);  lcd.print ("NO"); //240
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(7, 0);lcd.print ("-");
   lcd.setCursor(6, 0);lcd.print ("I"); 
   lcd.setCursor(5, 0);lcd.print ("-");
   }else
//------------------        
   if ((Dir >=255) and (Dir <= 284)){
   lcd.setCursor(5, 1); lcd.print ("O "); //270
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(6, 0);lcd.print ("-");
   lcd.setCursor(5, 0);lcd.print ("O"); 
   lcd.setCursor(4, 0);lcd.print ("-");
   }else
//------------------     
   if ((Dir >=285)and (Dir <= 314)){
   lcd.setCursor(5, 1); lcd.print ("SO"); //300
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(5, 0);lcd.print ("-"); 
   lcd.setCursor(4, 0);lcd.print ("I");
   lcd.setCursor(3, 0);lcd.print ("-");
   }else
   if ((Dir >=315)and (Dir <= 345)){
   lcd.setCursor(5, 1); lcd.print ("SO"); //330
   lcd.setCursor(2, 0);lcd.print ("*");
   lcd.setCursor(14, 0);lcd.print ("*");
   lcd.setCursor(4, 0);lcd.print ("-");
   lcd.setCursor(3, 0);lcd.print ("I");
   }
//------------------    
   lcd.setCursor(8, 1);lcd.print("   ");lcd.setCursor(8, 1); lcd.print(paz1);   

}

void manuel(){// cde manuelle
     lcd.setCursor(0, 1);lcd.print("MANU");
     btcw=digitalRead(bcw);//cw
     btccw=digitalRead(bccw);//ccw
//-----------------------------  
     if((btcw==LOW)&&(m1==0)){
     digitalWrite(brake,LOW);
     delay(500);   //debloque frein  
     lcd.setCursor(15, 1);lcd.print(" ");
     m1=1;    
     digitalWrite(cw,LOW);
     lcd.setCursor(15, 1);lcd.print("<");
     potazim();
     }
     if((btcw==HIGH)&&(m1==1)){
     digitalWrite(cw,HIGH);
     lcd.setCursor(15, 1);lcd.print(" ");
     delay(500);
     digitalWrite(brake,HIGH);//bloque le frein
     lcd.setCursor(15, 1);lcd.print("B");        
     m1=0;
     }
//-----------------------------
     if((btccw==LOW)&&(m2==0)){
     digitalWrite(brake,LOW);
     delay(500);  //debloque le frein
     m2=1;      
     digitalWrite(ccw,LOW);
     lcd.setCursor(15, 1);lcd.print(">");
     potazim();          
     }
     if((btccw==HIGH)&&(m2==1)){
     digitalWrite(ccw,HIGH);      
     lcd.setCursor(15, 1);lcd.print(" ");
     delay(500);
     digitalWrite(brake,HIGH);//bloque le frein
     lcd.setCursor(15, 1);lcd.print("B");   
     m2=0;
     }
//-----------------------------     
    
      btaux=digitalRead(baux); //relai aux         
     if ((btaux == LOW)&&(aux1==0)){
      digitalWrite(aux,LOW);   
      delay(50);      
      aux1=1;
      delay(50);
      aux2=1;
      }
     if ((btaux == HIGH)&& (aux1==1)) {
      aux2=0;
      delay(50);
      }
     if ((btaux == LOW)&&(aux2==0)){
      delay(50);
      digitalWrite(aux,HIGH);   
      delay(50); 
      aux2=1;       
      aux1=0;
      delay(1000);
      }
//-----------------------------        

}

void automat(){
    //----------------reception Orbitron/PstRotator interface protocole GS-232/winrotator-funkbox
     lcd.setCursor(0, 1);lcd.print("AUTO"); 
     potazim(); 
     lcd.setCursor(12, 1);lcd.print(Dir);
     delay(50);
     lcd.setCursor(12, 1);lcd.print("    ");
 

     if (Serial.available()>0 ){
      //Si un message a été reçu  faire ceci
     String a = (Serial.readString());  // Lit le message reçu et l'affiche sur le moniteur
   
     String  az= a.substring(2, 5);// extrait az
     
     int w = az.toInt();//transforme la chaine texte en numerique
      x=w;
     lcd.setCursor(12, 1);lcd.print("   ");
     if (x<paz1){
     digitalWrite(brake,LOW); //  debloque le frein   
     lcd.setCursor(15, 1);lcd.print(" ");     
     delay(500);      
     digitalWrite(cw,HIGH);
     delay(100);      
     digitalWrite(ccw,LOW);      
     lcd.setCursor(15, 1);lcd.print("<"); 
     }
     if (x>paz1){
     digitalWrite(brake,LOW);     //debloque le frein
     lcd.setCursor(15, 1);lcd.print(" ");     
     delay(500);    
     digitalWrite(ccw,HIGH);      
     delay(100); 
     digitalWrite(cw,LOW); 
     lcd.setCursor(15, 1);lcd.print(">");
     }
     lcd.setCursor(12, 1);
     lcd.print(az);
     }

     if (x==paz1){
     digitalWrite(ccw,HIGH);      
     delay(50); 
     digitalWrite(cw,HIGH);    
     delay(3000);
     digitalWrite(brake,HIGH);lcd.setCursor(15, 1);lcd.print("B"); //bloque le frein     
     lcd.setCursor(0, 1);lcd.print("STOP"); 
     }

}
