Bonjour,

J’ai développé un logiciel en VB6 qui gère des courses de « petites autos sur une piste plastique (ou slot) »

J’ai connecté sur un Arduino UNO 2 x IR (pour la détection du passage des autos) et deux relais pour couper l’alimentation des pistes. L’Arduino est connecté au PC via le port USB.

Le protocole de dialogue est simplissime :
PC  Arduino
- Mise en service du système de détection (à chaque passage une de mes leds s’allume et un message est envoyé au PC)
- En fermant le programme VB6, je désactive ce système de détection.
- Déclenchement d’un ou deux relais
Arduino  PC
- Chaque passage d’une auto envoie un message au PC

Le système fonctionne parfaitement en détection. Lorsqu’on utilise les relais, à un moment (aléatoire) l’Arduino se perd : mes Led ne s’allument plus, VB ne reçoit plus de messages; ce qui ne peut normalement arriver que lorsque le programme VB se ferme (ce qui, bien sûr, n’est pas le cas).
J’en conclus que c’est le programme de l’Arduino qui ne boucle plus correctement.

J’ai essayé d’alimenter la carte relai indépendamment, le résultat est identique.
J’ai essayé d’alimenter l’arduino indépendamment; je lance le programme VB. Si je débranche le câble USB, mes LEDs continuent à s’allumer au passage, j’en conclus que le problème se situe au niveau de l’Arduino et de lui seul.

La seule piste que j’ai pu découvrir : en accélérant la boucle de l’arduino, les anomalies sont plus fréquentes ! Une pause d’1/10ème limite les pannes… mais la précision des calculs des chronomètres des autos devient insuffisante.

Comment faire un diagnostic fiable et trouver la cause de cette anomalie ?

Merci de votre aide

JF

PS en PJ mon script.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
//                    Pgm pour STZ Base V 2 2018
//
//  Format Message PC ==> Arduino .... [APOnnnn]
//                A = Code Action
//                P = Numero Piste
//                0 = 0 ou 1 (on ou off)
//                nnnnn = valeur
//  Format Message  Arduino ==> PC .... [APnnnn]
//                A = Code Action
//                P = Numero Piste
//                nnnnn = valeur
//
//  ====================  constantes  =======================
//const float ecart = 0.1 ;
const char debutcrochet = '[' ;
const char fincrochet = ']' ;
const char msgvolt = 'V' ;
const char msgmessage = 'M' ;
const char msgerreur = 'E' ;
const char msgamp = 'A' ;
const char msgbouton = 'B' ;
const char msgpiste = 'P' ;
const char msglux = 'L' ;
const char msgrelai = 'R' ;
const char msgmoteur = 'U' ;
const char msgtest = 'T' ;
const char msgfuel = 'F' ;
const char msgstand = 'S' ;
const long delailed = 1000 ;            // Durée allumage LED en ms
const int comptgal =  1  ;            // Combien de signaux successifs
const int wascii = 48 ;
const int maxvolt = 200 ;
const int maxampe = 100 ;
const int delailoop = 10 ;
 
 
//  ====================  variables  =======================
int wpiste ;
char waction ;
int wvaleur ;
int onoff ;
int indice ;
int jndice ;
long tpsled[] = {0, 0, 0 } ;
int comptcell [] = {0, 0, 0} ;
int autopasse [] = {0, 0, 0} ;
float temp ;
int value = 0 ;
int amperezero [] = {0, 0} ;
int oldvolt[] = {0 , 999 , 999} ;
int oldamp[] = {0 , 999 , 999} ;
int oldstand [] = {0 , 0 , 0} ;
int oldpiste [] = {0 , 0 , 0} ;
int oldbouton [] = {0 , 0 , 0} ;
int wvalue[4] ;
int comptvolt = maxvolt ;
int comptampe [] = {0 , 0 , 0 } ;
int moyenne [] = {0 , 0 , 0 } ;
char TabJelis[21];
String jelis;
int wdiz ;
 
boolean flagrelai = false ;
boolean flagpiste = false ;
boolean flagbouton = false ;
boolean flagvolt = false ;
boolean flagamp [] = {0 , 0 , 0 } ;
boolean flagfuel [] = {0 , 0 , 0 } ;
boolean flagstand [] = {0 , 0 , 0 } ;
boolean etatstand [] = {0 , 0 , 0 } ;
 
//   ================  Entrées Analogiques  =====================
int volt[] = {0, 0 , 1} ;
int amp[] = {0, 2 , 3} ;
int stand[] = {0 , 4, 5} ;
// int photo = 4 ;
 
//   ================  Sorties  Digitales  =====================
int relai[] = {0 , 7 , 8 } ;
int moteur[] = {0 , 9, 10 , 6} ;
int ledpasse[] = {0 , 11, 12} ;
int ledtest = 13 ;
 
//   ================  Entrées Digitales  =====================
//int wserie[] = {0 , 0 , 1} ;      //  ===  RESERVE pour Com ===
int bouton[] = {0 , 2 , 3} ;
int cell[] = {0 , 4 , 5} ;
 
void setup()
{
  //pinMode(wserie[1], OUTPUT);      //  ===  RESERVE pour Com ===
  //pinMode(wserie[2], OUTPUT);      //  ===  RESERVE pour Com ===
  pinMode(bouton[1], INPUT);         // D2            Utilisé
  pinMode(bouton[2], INPUT);         // D3    pwm     Utilisé
  pinMode(cell[1], INPUT);           // D4            Utilisé
  pinMode(cell[2], INPUT);           // D5    pwm     Utilisé
  pinMode(moteur[3], OUTPUT);        // D6    pwm     pas Utilisé
  pinMode (relai[1], OUTPUT);        // D7            Utilisé
  pinMode (relai[2], OUTPUT);        // D8            Utilisé
  pinMode (moteur[1], OUTPUT);       // D9    pwm     Utilisé
  pinMode (moteur[2], OUTPUT);       // D10   pwm     Utilisé
  pinMode (ledpasse[1], OUTPUT);     // D11   pwm     Utilisé
  pinMode (ledpasse[2], OUTPUT);     // D12           Utilisé
  pinMode (ledtest, OUTPUT);         // D13           Utilisé
  pinMode(volt[1], INPUT);           // A0            pas encore Utilisé
  pinMode(volt[2], INPUT);           // A1            pas encore Utilisé
  pinMode(amp[1], INPUT);            // A2            Utilisé
  pinMode(amp[2], INPUT);            // A3            Utilisé
  pinMode(stand[1], INPUT);          // A4            pas encore Utilisé
  pinMode(stand[2], INPUT);          // A5            pas encore Utilisé
 
  Serial.begin(9600);
  digitalWrite(relai[1], HIGH);
  digitalWrite(relai[2], HIGH);
}
 
void loop()
{
  //  =======================  Lecture port série  ================
  if (Serial.available() > 0)
  {
    String b;
    b = Serial.readStringUntil('[');
    jelis = Serial.readStringUntil(']');
    if ( jelis.length() > 0 )
    {
      wvaleur = 0 ;
      wdiz = pow (10 , jelis.length() - 4) + 1 ;
      jelis.toCharArray(TabJelis, 10);
      waction = TabJelis [0] ;
      wpiste  = TabJelis [1] - wascii ;
      onoff = TabJelis [2] - wascii ;
      wvaleur = 0 ;
      for (indice = 3 ; indice < jelis.length() ; indice ++)
      {
        wvaleur = wvaleur + (TabJelis [indice] - wascii) * wdiz ;
        wdiz = wdiz / 10 ;
      }
 
      // [U10250]
 
      switch (waction)
      {
        case  msgmoteur:                   //  ====  Pilotage alimentation pistes  ===
          analogWrite ( moteur [ wpiste ] , wvaleur ) ;
 
Serial.print (moteur [wpiste] );
Serial.print (" Moteur ==> ");
Serial.println (wvaleur) ;
 
          break;
 
        case msglux:                    //  ====  Pilotage alimentation Lumière  ===
          for (indice = 0 ; indice <= wvaleur ; indice++)
          {
            analogWrite(moteur[3], indice);
          }
          break;
 
        case msgrelai:                  //  ====  Gestion des relais  ===
            if ( wpiste < 3 ) {
              digitalWrite( relai [ wpiste ] , 1 - onoff) ;
 
//Serial.print ("==> ");
//Serial.println ( digitalWrite ( relai [ 1 ] ) ) ;
 
            }
            else
            {
              digitalWrite ( relai[ 1 ] , 1 - onoff ) ;
              digitalWrite ( relai[ 2 ] , 1 - onoff ) ;
            }
 
          break;
 
        case msgtest:                   //  ====  Test de démarrage  ===
          Serial.print (debutcrochet) ;
          Serial.print ("T");
          Serial.println (fincrochet) ;
          digitalWrite(ledtest, HIGH) ;
          digitalWrite(ledpasse[1] , HIGH) ;
          digitalWrite(ledpasse[2] , HIGH) ;
          delay ( delailed ) ;
          digitalWrite(ledtest, LOW) ;
          digitalWrite(ledpasse[1] , LOW) ;
          digitalWrite(ledpasse[2] , LOW) ;
          break;
 
        case msgbouton:                 //  ====  Gestion ou non des boutons  ===
          if ( onoff == 1 )
          {
            flagbouton = true ;
//            digitalWrite(ledpasse[2], HIGH);
//            delay(delailed / 10) ;
//            digitalWrite(ledpasse[2], LOW);
          }
          else
          {
            flagbouton = false ;
          }
 
          break;
 
        case msgpiste:                  //  ====  Gestion ou non des cellules  ===
          if ( onoff == 1 )
          {
            flagpiste = true ;
          }
          else
          {
            flagpiste = false ;
          }
 
          break;
 
//        case msgvolt:                   //  ====  Gestion ou non Lecture Voltage  ===
//          if ( wpiste == 1 )
//          {
//            flagvolt = true ;
//          }
//          else
//          {
//            flagvolt = false ;
//          }
//
//          break;
 
        case msgamp:                    //  ====  Gestion ou non Lecture Ampérage  ===
          if ( wpiste == 3 )
          {
            flagamp [ 1 ] = onoff ;
            flagamp [ 2 ] = onoff ;
          }
          else
          {
            flagamp [ wpiste ] = onoff ;
          }
 //         if  (flagamp [1]+ flagamp [2] == 0) { digitalWrite(ledtest, LOW); }
 //         else                                { digitalWrite(ledtest, HIGH); }
 
          break;
 
        case msgstand:                    //  ====  Gestion ou non Arrêt stand  ===         
          if ( wpiste == 3 )
          {
            flagstand [ 1 ] = onoff ;
            flagstand [ 2 ] = onoff ;
            oldstand  [ 1 ] = 0 ;
            oldstand  [ 2 ] = 0 ;
          }
          else
          {
            flagstand [ wpiste ] = onoff ;
            oldstand  [ wpiste ] = onoff ;
          }
 
          break;
 
        default:
          Serial.print (debutcrochet) ;
          Serial.print (msgerreur) ;    //  ====  Envoi Ereur au PC  ===
          Serial.println (fincrochet) ;
          break;
      }
    }
  }
 
  // ======================   Détection cellules 1 & 2 ====================
  if ( flagpiste == 1 )
  {
    for (jndice = 1 ; jndice <= 3 ; jndice++)
    {
      if (digitalRead(cell[jndice]) == LOW )
      {
        comptcell[jndice] ++ ;
        if (oldpiste [jndice] == 0 )
        {
          oldpiste [jndice] = 1 ;
          Serial.print (debutcrochet) ;
          Serial.print (msgpiste) ;
          Serial.print(jndice);
          Serial.println (fincrochet) ;
          digitalWrite(ledpasse[jndice], HIGH);   // high
          tpsled[jndice] = millis();
          autopasse [jndice ] = 1 ;
        }
      }
      else
      {
         oldpiste [jndice] = 0 ;
        if (tpsled[jndice] + delailed < millis())
        {
          digitalWrite(ledpasse[jndice], LOW);
        }
      }
    }
  }
  else
  {
    if (digitalRead (ledpasse[1]) == HIGH) {
      digitalWrite(ledpasse[1], LOW);
    }
    if (digitalRead (ledpasse[2]) == HIGH) {
      digitalWrite(ledpasse[2], LOW);
    }
  }
 
// ======================   Détection Boutons 1 & 2 ====================
  if ( flagbouton == 1 )
  {
    for (jndice = 1 ; jndice <= 3  ; jndice++)
    { 
      if (digitalRead(bouton[jndice]) == HIGH )
      {
        if (oldbouton [jndice] == 0 )
        {
          oldbouton [jndice] = 1 ;
          Serial.print (debutcrochet) ;
          Serial.print (msgbouton) ;
          Serial.print(jndice);
          Serial.println (fincrochet) ;
        }
      }
      else
      {
        oldbouton [jndice] = 0 ;
      }
    }
  }
 
//                      =================== Mesure des Ampères  =================
  for (indice = 1 ; indice < 3 ; indice ++ )
  {
    if ( flagamp [ indice ] == true )
   {
      value = analogRead ( amp [ indice ] ) ;
      if (comptampe [indice ] == 100 )
      {
        moyenne [ indice ] = moyenne  [ indice ] / comptampe  [ indice ]  ;    
        if (moyenne [ indice ] > 0 )
        {
          Serial.print ( debutcrochet ) ;
          Serial.print ( msgamp ) ;
//          Serial.print ( "X" ) ;          
           Serial.print ( indice ) ;
          Serial.print ( moyenne [ indice ] ) ;
          Serial.println ( fincrochet ) ;
        }
        comptampe  [ indice ] = 0 ;
        moyenne  [ indice ] = 0 ;
        autopasse [indice ] = 0 ;
      }
      else
      {
      comptampe [ indice ] ++ ;
      moyenne [ indice ] = moyenne  [ indice ] + value ;
      }
    }
  }
 
//                      =================== Test des Stands  =================
  for (indice = 1 ; indice < 3 ; indice ++ )
  {
    if ( flagstand [ indice ] == true )
    {
      value = analogRead ( stand [ indice ] );
      if (value > 511 ) {etatstand [indice ] = 0 ; }
      else              {etatstand [indice ] = 1 ; } 
 
      if ( oldstand [indice ] != etatstand [indice ] )
      {         
       Serial.print ( debutcrochet ) ;
       Serial.print ( msgstand ) ;
       Serial.print ( indice ) ;
       Serial.print ( etatstand [ indice ] ) ;
       Serial.println ( fincrochet ) ;
       oldstand [ indice ] = etatstand [ indice ] ;
      }
    }
  } 
  delay ( delailoop );
}