Prog Thib Ju

Dependencies:   mbed

Committer:
JulienB1
Date:
Thu Apr 11 12:05:57 2019 +0000
Revision:
0:5535594ef331
J

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JulienB1 0:5535594ef331 1 #include "mbed.h"
JulienB1 0:5535594ef331 2
JulienB1 0:5535594ef331 3
JulienB1 0:5535594ef331 4 PwmOut moteur1(D10);
JulienB1 0:5535594ef331 5 PwmOut moteur2(D9);
JulienB1 0:5535594ef331 6 DigitalOut Trig(D12);
JulienB1 0:5535594ef331 7 InterruptIn reception(D8);
JulienB1 0:5535594ef331 8
JulienB1 0:5535594ef331 9 DigitalOut myled(LED1);
JulienB1 0:5535594ef331 10
JulienB1 0:5535594ef331 11 DigitalOut LED(LED1);
JulienB1 0:5535594ef331 12 Serial pc(USBTX, USBRX);
JulienB1 0:5535594ef331 13 Timer t;
JulienB1 0:5535594ef331 14 double distance ;
JulienB1 0:5535594ef331 15 double rc;
JulienB1 0:5535594ef331 16
JulienB1 0:5535594ef331 17 void front_montant()
JulienB1 0:5535594ef331 18 {
JulienB1 0:5535594ef331 19 t.start();
JulienB1 0:5535594ef331 20 }
JulienB1 0:5535594ef331 21
JulienB1 0:5535594ef331 22 void front_descendant()
JulienB1 0:5535594ef331 23 {
JulienB1 0:5535594ef331 24 t.stop();
JulienB1 0:5535594ef331 25 double distance = 330*t.read()/2*1.06;
JulienB1 0:5535594ef331 26 // pc.printf("Distance = %f \r\n",330*t.read()/2*1.06);
JulienB1 0:5535594ef331 27 pc.printf("Distance = %lf \r\n", distance);
JulienB1 0:5535594ef331 28 t.reset();
JulienB1 0:5535594ef331 29 myled =1;
JulienB1 0:5535594ef331 30 if (distance<=0.10){ rc = 0;}
JulienB1 0:5535594ef331 31 if (distance<=0.20 && distance>0.10){ rc = 0.5;}
JulienB1 0:5535594ef331 32 if (distance >=0.20) { rc = 1; }
JulienB1 0:5535594ef331 33 //if (distance<=0.40 && distance>0.20){ rc = 0.6;}
JulienB1 0:5535594ef331 34 //if (distance<=0.50 && distance>0.40){ rc = 0.8;}
JulienB1 0:5535594ef331 35 //pc.printf("rapport cyclique = \r \n");
JulienB1 0:5535594ef331 36 //pc.scanf("%lf" ,&rc);
JulienB1 0:5535594ef331 37 pc.printf("Nouvelle valeur du rapport cyclique = %lf \r \n", rc);
JulienB1 0:5535594ef331 38 moteur2.write(rc);
JulienB1 0:5535594ef331 39 }
JulienB1 0:5535594ef331 40
JulienB1 0:5535594ef331 41 int main()
JulienB1 0:5535594ef331 42 {
JulienB1 0:5535594ef331 43 t.reset();
JulienB1 0:5535594ef331 44 reception.rise(&front_montant);
JulienB1 0:5535594ef331 45 reception.fall(&front_descendant);
JulienB1 0:5535594ef331 46 moteur1.period_ms(10);
JulienB1 0:5535594ef331 47 moteur2.period_ms(10);
JulienB1 0:5535594ef331 48 moteur1.write(0);
JulienB1 0:5535594ef331 49 moteur2.write(0);
JulienB1 0:5535594ef331 50 pc.baud(9600);
JulienB1 0:5535594ef331 51
JulienB1 0:5535594ef331 52 while(1)
JulienB1 0:5535594ef331 53 {
JulienB1 0:5535594ef331 54 Trig = 1;
JulienB1 0:5535594ef331 55 wait(0.001);
JulienB1 0:5535594ef331 56 Trig =0 ;
JulienB1 0:5535594ef331 57 wait(0.001);
JulienB1 0:5535594ef331 58 /* Trig = 0;
JulienB1 0:5535594ef331 59 Trig = 1;
JulienB1 0:5535594ef331 60
JulienB1 0:5535594ef331 61 //pc.printf("rapport cyclique = \r \n");
JulienB1 0:5535594ef331 62 //pc.scanf("%lf" ,&rc);
JulienB1 0:5535594ef331 63 pc.printf("Nouvelle valeur du rapport cyclique = %lf \r \n", rc);
JulienB1 0:5535594ef331 64 moteur_cc_pwm.write(rc);
JulienB1 0:5535594ef331 65 */
JulienB1 0:5535594ef331 66 }
JulienB1 0:5535594ef331 67 }