Tu mourras moins bete / Mbed 2 deprecated Vitesse_HandSpinner

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 
00004 DigitalOut myled(LED2);     // Pas encore utilisé
00005 AnalogIn   phototo(A1);    // Renvoie un float entre [1.0 ; 0.0]
00006                    // Light => no resistance
00007                    // Dark => high resistance
00008 
00009 Serial     pc(USBTX, USBRX);
00010 
00011 const int changements_par_tour = 3*2;   // HandSpinner avec 3 branches
00012 const int tours_par_mesure = 2;
00013 const int timeout_ms = 4 * 1000;    // 4 secondes
00014 const int TAILLE_RECORD = 1000;
00015 
00016 struct Seuil {
00017     float haut;
00018     float bas;
00019 };
00020 
00021 Timer t;        // https://developer.mbed.org/handbook/Timer
00022                     // https://developer.mbed.org/questions/61002/Equivalent-to-Arduino-millis/
00023                     
00024 Seuil calibration() {
00025     float  v, v_max, v_min, delta;
00026     Seuil s;
00027     
00028     v = phototo.read();
00029     v_max = v;
00030     v_min = v;
00031     
00032     t.reset();
00033     t.start();
00034     for (int i=0; i<200 ; i++) {
00035             v = phototo.read();         // float entre [1.0 ; 0.0]
00036             //phototo.read_u16();       // @TODO  renvoie un entier [65535 ; 0]  (plus rapide)
00037             if      (v > v_max)  v_max = v;
00038             else if (v < v_min)  v_min = v;
00039             wait_ms(5); 
00040     }
00041     t.stop();
00042     delta = (v_max - v_min) / 3.0;
00043     s.haut = v_max - delta;
00044     s.bas  = v_min + delta;
00045     pc.printf("Calibration [%.3f ; %.3f] [%.3f; %.3f] sur %d ms.\t", v_max, s.haut, s.bas, v_min, t.read_ms());
00046     return s;
00047 }
00048 
00049 
00050 void affiche_mesures(float mesures[], int nb_mesures)  {
00051     if ( nb_mesures > TAILLE_RECORD )  nb_mesures = TAILLE_RECORD;
00052     pc.printf("\r\n---------------------------------\r\n");
00053     for (int i=0; i < nb_mesures ; i++)  pc.printf("%f;", mesures[i]);
00054     pc.printf("\r\n---------------------------------\r\n");
00055 }
00056     
00057 
00058 int main() {
00059     pc.baud( 115200 ); // 9600 14400 57600 115200
00060     //pc.format(8, SerialBase::None, 1);  // Optional (default)
00061     
00062     pc.printf("Demarrage ! \r\n");
00063     
00064     bool jour_ou_nuit, precedent;
00065     int changements, mesures;
00066     int temps_par_tour_ms;
00067     Seuil seuil;      
00068     float value, valpre, moy;
00069     float record[TAILLE_RECORD];
00070     
00071     
00072     while(true) {
00073 
00074         seuil = calibration();
00075         valpre = seuil.haut;     // initialise la valeur "precedente"
00076         
00077         changements = 0; mesures = 0;
00078         t.reset();                
00079         t.start();
00080         while ( ( changements < ( changements_par_tour * tours_par_mesure ) )
00081                 && ( t.read_ms() < timeout_ms ) )  {
00082             
00083             value = phototo.read();
00084             //printf("v %f\r\n", value);  // debug, mais bien trop lent!
00085             if (mesures < TAILLE_RECORD)  record[mesures] = value;
00086             mesures++;  // Attention de ne pas depasser!
00087     
00088             moy = (value + valpre) / 2.0;       // on lisse 
00089             valpre = value;
00090             if      (moy > seuil.haut)  jour_ou_nuit = true;
00091             else if (moy < seuil.bas)   jour_ou_nuit = false;
00092             else continue;
00093             
00094              if ( jour_ou_nuit != precedent )  {
00095                 precedent = jour_ou_nuit;
00096                 changements = changements + 1;
00097             }
00098          }
00099          t.stop();
00100          
00101         pc.printf(" %d mesures et %d changements \t", mesures, changements);
00102         
00103         if ( t.read_ms() >= timeout_ms ) {
00104             pc.printf("Timeout  ... \r\n");
00105             continue;
00106         }
00107             
00108         temps_par_tour_ms = t.read_ms() / tours_par_mesure;
00109         if (temps_par_tour_ms != 0)
00110             pc.printf("Un tour en %d ms ;\t %d tours/seconde \r\n",  temps_par_tour_ms, 1000/temps_par_tour_ms); 
00111         else
00112             pc.printf("Etrange ! le temps mesure est nul !?\r\n");
00113         affiche_mesures(record, mesures);
00114         wait_ms(100);       
00115     }
00116 }
00117 
00118