/*
--- Programme Arduino ---
Copyright Pascal ROMEYER
Date de création : 21/07/2016
Date de modification : 21/07/2016
Version 2.00

http://f4cvm.free.fr

  Code sous licence GNU GPL :
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License,
  or any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

--- Que fait ce programme ? ---
En fonction de la reception des valeur vitesse et direction
mixage de 2 variateur en avant ou arriere

--- Fonctionnalites utilisees ---
Afficheur LCD 1202 I2C - Juste 2 broches utilisees
pour affichage des données, ne sera pas utile par la suite

--- Circuit à realiser ---
Rubrique Modelisme... Les projets... Mixeur
Les servo a raccorder sur les sorties PWM D5 et D6
La radio raccordee sur A1 et A2



       \\\|///
      \\ - - //
       ( @ @ )
+---oOOo-(_)-oOOo------------------------------------+
Pascal ROMEYER
SAINT JUST MALMONT (43) JN25DI

Email perso  : pascal.romeyer@free.fr
Email radio  : f4cvm@free.fr
Site radio   : http://f4cvm.free.fr

     ooO  Ooo
+---(  )--(  )---------------------------------------+
    \  (  ) /
     \_) (_/

*/




/*
-----------------------------
 Chargement des biliotheques
-----------------------------
*/
// Bus I2C
  #include <Wire.h>

// Ecran LCD
  #include <LiquidCrystal_I2C.h>

// Servomoteur
  #include <Servo.h>




/*
------------------------------------------
Definition de l'ecran
Adresse LCD 0x27, 20 caracteres, 4 lignes
------------------------------------------
*/
  LiquidCrystal_I2C lcd(0x27, 20, 4);



/*
--------------------------
Declaration des variables
--------------------------
*/

Servo moteur1;  // creation de l objet servo pour le controler
Servo moteur2;  // creation de l objet servo pour le controler

unsigned long volant;       // Port analogique acquisition volant
unsigned long vitesse;      // Port analogique acquisition volant
int voie1;
int voie2;
int convert_voie1;
int convert_voie2;


/*
-------------------------------------
Declaration du SETUP
-------------------------------------
*/
void setup()
{
  lcd.init();         // On initialise le LCD
  lcd.backlight();    // On allume le retro eclairage
  moteur1.attach(5);  // attaches the servo on pin 9 to the servo object
  moteur2.attach(6);  // attaches the servo on pin 9 to the servo object
}



/*
-------------------------------------
Boucle principale du programme
-------------------------------------
*/
void loop() 
{ 

// Acquisition des voies du recepteur
  volant = pulseIn (4, HIGH, 25000);            // Lecture de la voie volant
//  volant = volant + 35;                  // Facteur de correction du zero servo
  vitesse = pulseIn (8, HIGH, 25000);           // Lecture de la voie accelerateur
//  vitesse = vitesse + 35;                // Facteur de correction du zero servo
  
// Affichage des valeurs brutes
  lcd.setCursor (0,0);
  lcd.print ("Volant:");
  lcd.setCursor (7,0);
  lcd.print ("    ");
  lcd.setCursor (7,0);
  lcd.print (volant);


  
  lcd.setCursor (0,1);
  lcd.print ("Speed:");
  lcd.setCursor (6,1);
  lcd.print ("    ");
  lcd.setCursor (6,1);
  lcd.print (vitesse);


// conversion des voies pour les servo  
  convert_voie1 = map(volant, 1000, 2000, 0, 180);      // scale it to use it with the servo (value between 0 and 180) 
  convert_voie2 = map(vitesse, 1000, 2000, 0, 180);     // scale it to use it with the servo (value between 0 and 180) 


// Affichage des valeur d angle des servos
  lcd.setCursor (0,2);
  lcd.print ("SERVO VOLANT:");
  lcd.setCursor (14,2);
  lcd.print ("    ");
  lcd.setCursor (14,2);
  lcd.print (convert_voie1);

  lcd.setCursor (0,3);
  lcd.print ("SERVO SPEED:");
  lcd.setCursor (14,3);
  lcd.print ("    ");
  lcd.setCursor (14,3);
  lcd.print (convert_voie2);


// Ecriture des valeur dans les servos
  moteur1.write(convert_voie1);        // Servo No 1 
//  delay(15);                           // waits for the servo to get there 
  moteur2.write(convert_voie2);        // Servo No 2
//  delay(15);                           // waits for the servo to get there 





}