-
Notifications
You must be signed in to change notification settings - Fork 0
/
codearduino.rtf
161 lines (160 loc) · 4.05 KB
/
codearduino.rtf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{\rtf1\ansi\ansicpg1252\cocoartf2513
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\*\expandedcolortbl;;}
\paperw11900\paperh16840\margl1440\margr1440\vieww25400\viewh16000\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
\f0\fs24 \cf0 // Constants moteurs\
const int enableBridge = 2;\
const int MotorForward = 3;\
const int MotorReverse = 5;\
\
// Variables pour HC-SR04\
const int Echo_in_pin = 2; \
const int Trig_out_pin = 3;\
float Dist_mm=0.0;\
\
// Param\'e8tres led\
const int LED_r=5; \
const int LED_g=6; \
const int LED_b=7; \
\
// Vitesse du son (mm/us) \
const double Speed_sound = 340.0e-3;\
\
// Variables\
int Power = 80; //Motor velocity between 0 and 255\
\
void setup()\{\
// Initialisation HC-SR04\
pinMode(Trig_out_pin, OUTPUT);\
pinMode(Echo_in_pin, INPUT);\
digitalWrite(Trig_out_pin, LOW);\
/////////////////////\
// Init interface s\'e9rie \
Serial.begin(115200); \
//moteur\
dcBegin();\
\
// LEDs \
pinMode(LED_r,OUTPUT);\
pinMode(LED_g,OUTPUT);\
pinMode(LED_b,OUTPUT);\
digitalWrite(LED_r, LOW);\
digitalWrite(LED_g, LOW);\
digitalWrite(LED_b, LOW);\
//////////////////////\
\
\
\}\
\
void loop()\{\
/*+++++++++++++++++++++++++++++++++++++*/\
\
\
/* Mesure de la distance */ \
Dist_mm=GetDisCM( Echo_in_pin, Trig_out_pin, Speed_sound); // Valeur brute \
//Dist_mm=GetMean(8, Echo_in_pin, Trig_out_pin, Speed_sound); // Valeur moyenne \
\
/* Affichage de la distance */\
Serial.println(Dist_mm); \
\
/* Activation des LEDs */\
SetLED(Dist_mm, LED_r, LED_g, LED_b);\
\
if (Dist_mm > 500)\{\
dcForward(Power);\
delay(2000);\
\}else\{\
dcReverse(Power);\
delay(3000);\
dcStop();\
delay(1000);\
\}\
\
\
\}\
\
\
/*++++++++++++++++++++++++++++++++++++++*/\
\
\
// Calcul de la distance en mm \
float GetDisCM(int Echo_in_pinn, int Trig_out_pinn, double Speed_soundd)\
\{\
float Dist_milm=0.0;\
long HC_val=0; \
\
// D\'e9clanchement mesure avec l'envoie d'une impulsion de 10\'b5s\
digitalWrite(Trig_out_pin, HIGH);\
delayMicroseconds(10);\
digitalWrite(Trig_out_pin, LOW);\
\
// Lecture du temps aller/retour de l'onde \
HC_val = pulseIn(Echo_in_pin, HIGH, 30000);\
\
// Calcul de distance d(mm), V=d/t==> v*t \
// Le coefficnet 2 pour la distance aller\
Dist_milm = (HC_val/2.0) * Speed_sound; \
\
return Dist_milm;\
\} \
\
// Valeur Moyenne \
float GetMean(long Taille, int Echo_in_pinn, int Trig_out_pinn, double Speed_soundd)\
\{\
long i;\
float Distance_tmp, Mean_val=0.0,Somme=0.0; \
for(i=0; i<Taille; i++)\
\{\
Distance_tmp=GetDisCM( Echo_in_pinn, Trig_out_pinn, Speed_soundd);\
Somme=Somme+Distance_tmp; \
\}\
Mean_val=Somme/Taille; \
return Mean_val;\
\}\
\
void SetLED(float Dist_mmm, int LED_rr, int LED_gg, int LED_bb)\
\{\
if((Dist_mmm < 500) && (Dist_mmm != 0.0))\
\{\
digitalWrite(LED_rr, HIGH);\
digitalWrite(LED_gg, HIGH);\
digitalWrite(LED_bb, HIGH);\
delay(floor(Dist_mmm));\
digitalWrite(LED_rr, LOW);\
digitalWrite(LED_gg, LOW);\
digitalWrite(LED_bb, LOW);\
delay(floor(Dist_mmm));\
\} \
\}\
\
\
\
void dcBegin()\{\
// Initialise les pines utilis\'e9es pour le moteur\
pinMode(MotorForward,OUTPUT);\
pinMode(MotorReverse,OUTPUT);\
pinMode(enableBridge,OUTPUT);\
\}\
void dcForward(int P)\{\
digitalWrite(enableBridge,HIGH); // Active pont en H\
// Tourne dans le sens Forward \'e0 la vitesse sp\'e9cifi\'e9e par P\
analogWrite(MotorForward,P);\
analogWrite(MotorReverse,0);\
\}\
\
void dcReverse(int P)\{\
digitalWrite(enableBridge,HIGH); // Active pont en H\
// Tourne dans le sens Reverse \'e0 la vitesse sp\'e9cifi\'e9e par P\
analogWrite(MotorForward,0);\
analogWrite(MotorReverse,P);\
\}\
\
void dcStop()\{\
// Arr\'eate le moteur et d\'e9sactive le pont en H\
analogWrite(MotorForward,0);\
analogWrite(MotorReverse,0);\
digitalWrite(enableBridge,LOW); \
\}\
}