Guillaume GARBAY / Mbed 2 deprecated HTU21D_HELLOWORLD

Dependencies:   HTU21D mbed MD25 TextLCD

Fork of HTU21D-F_Nucleo_F411RE by Guillaume GARBAY

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Librairies
00002 #include "mbed.h"               // Standard
00003 #include "MD25.h"               // Moteurs
00004 #include "TextLCD.h"            // Ecran LCD
00005 #include "HTU21D.h"             // Capteur Temperature / Humidite
00006 // #include "VCNL40x0.h"        // Capteur Luminosite / Distance
00007 
00008 
00009 // Declarations E/S //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00010 
00011 DigitalIn bouton (USER_BUTTON);                                     // Bouton d'arret total du robot
00012 DigitalIn bouton_poussoir(D9);                                      // Pin PC7 Bouton poussoir demarrage
00013 DigitalIn cap_arret(PA_9);                                          // Pin PA9 capteur d'arret (Devant)
00014  
00015 Serial pc(SERIAL_TX, SERIAL_RX);                                    // Port Serie avec le PC
00016 
00017 TextLCD lcd(D2, D3, D4, D5, D6, D7,TextLCD::LCD20x4);               // Ecran LDC (rs, e, d4-d7)
00018 MD25 i2c(I2C_SDA, I2C_SCL);                                         // Moteurs  :   100kHz clock, addr B0, reg 0=speedl, 1=speedr (0 (Full Reverse) 128 (Stop) 255 (Full Forward). 15=mode (set to 0 default)
00019 HTU21D temphumid(D14, D15);//HTU21D temp(I2C_SDA, I2C_SCL);                                    // Capteur Temperature / Humidite 
00020 //VCNL40x0 VCNL40x0_Device (I2C_SDA, I2C_SCL, VCNL40x0_ADDRESS);      // Capteur Luminosite / Distance Define SDA, SCL pin and I2C address 
00021 
00022 
00023 // Fonctions pour les Moteurs ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00024 // Prototypes ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00025     void init_motors (void);                            // Initialisation des Moteurs
00026     void setSpeed (int speedMotor1, int speedMotor2);   // Definir Vitesse moteurs
00027 
00028 // Definition ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00029     void init_motors(void) {
00030         i2c.setMode(0);         // MODE 0, 0=marche arriere 128=stop 255=marche arriere vmax
00031         i2c.setCommand(32);     // 0x20 reset encoders
00032         i2c.setCommand(50);     // 0X32 Disable time out
00033         //i2c.setCommand(32);   // 0x20 reset encoders
00034         //i2c.setCommand(48);   // 0X32 Disable speed regulation
00035     }
00036      
00037     void setSpeed (int vitMotor1, int vitMotor2) {
00038         i2c.setSpeedRegisters(vitMotor1,vitMotor2); 
00039     }
00040 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00041 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00042 
00043 
00044 // Fonction d'Initialisation //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00045 void initialisations () { 
00046     lcd.printf("Initialisation\n");   
00047     pc.printf("Initialisation\n\r");
00048     bouton.mode (PullUp);                       // Bouton Utilisateur 
00049     bouton_poussoir.mode (PullUp);              // Bouton Poussoir
00050     init_motors();                              // Initialise Moteur
00051 
00052 
00053     
00054     pc.printf("Fin Initialisation\n\r");
00055     lcd.printf("Fin Initialisation\n");
00056     wait(1);
00057 }
00058 
00059 // Fonction Principale //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00060 int main() { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00061     pc.printf("\n\n\n\n\n\n\n\n Majord'home: Hello! Programme\n\r"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00062     lcd.printf("Majord'home: Hello!\n"); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00063     wait(1); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00064     initialisations (); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00065     
00066 // Declaration Variables Locales
00067     float Ftemp, Ctemp, Ktemp, Humid;       // Temperature et humidite
00068     
00069     
00070 // Init des Temperatures et Humidite
00071     Ftemp = temphumid.sample_ftemp();       Ctemp = temphumid.sample_ctemp();       Ktemp = temphumid.sample_ktemp();       Humid = temphumid.sample_humid();
00072     
00073     
00074 // Boucle Infinie //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00075     lcd.cls();  lcd.locate(0,0);
00076     lcd.printf("%.2f C  %.2f %% Hum", Ctemp, Humid); lcd.locate(0,1);
00077     lcd.printf("%.2f F  %.2f K", Ftemp, Ktemp); lcd.locate(0,2);
00078     lcd.printf("Debut While(1)");
00079     pc.printf("Debut While(1)\n\r");
00080     wait(3); lcd.cls();  lcd.locate(0,0);
00081     
00082     while(1) { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
00083 
00084         lcd.locate(0,0);
00085         Ctemp = temphumid.sample_ctemp();   Humid = temphumid.sample_humid();
00086         lcd.printf("%.2f C  %.2f %% Hum", Ctemp, Humid);
00087         
00088                
00089         
00090         lcd.locate(0,2);
00091         cap_arret.read();
00092         if (cap_arret==0) {
00093             lcd.printf("A=0");
00094             setSpeed(128,128);
00095             }
00096         else if (cap_arret==1) {
00097             lcd.printf("A=1");
00098             //setSpeed(60,60);
00099             }
00100             
00101         
00102     }
00103 }