Skectch working but I think it should not?

Hi firstly I know this should be in another category, but every other category said I'm not allowed to post in this category, even one I posted in before.

I'm working on a sketch to record the on/off status of several opto switches, essentially creating a multi channel oscilloscope (I'm doing the plotting outside of the Arduino environment). Thing is, it's working well so far, but then on examining my code and output, I believe the sketch should be stuck in an infinite loop, but it's not, so hopefully someone can explain what I'm missing.

As I said I have several opto switches, two of which are looking at the same moving part. I want to start logging when a (start) limit switch is triggered and stop when I have 150 transitions on the other switch (there is a comb breaking the opto and 150 transitions = 75 comb teeth or about 120mm movement).

I'm looping until this happens, but I realized I'm not getting that number of transitions, yet the sketch stops and prints out the results.

Here is a cut down debug version:



// i/o pins
int C_position = 6;
int C_limit = 7;


// flags / counts
int thisSample = 0;
int logOptos = 0;
int C_pos_count = 0;
int save_C_pos_state = 0;
int stopLogging = 0;


// output array and keys
int outputC_pos = 1;
int outputC_pos_count = 2;
int outputArray[2000][2]; // [SAMPLE][SENSOR OUTPUT]


void setup() {
 pinMode(C_position, INPUT);
  pinMode(C_limit, INPUT);
  Serial.begin(115200);
}

// opto switch output:
// HIGH - Light passes
// LOW  - Light blocked
// putty does not work if the Serial Monitor is open!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// https://stackoverflow.com/questions/73988173/how-can-i-make-the-loop-execute-exactly-once-in-arduino


// just run the program once
bool firstLoop = true;
void loop() {
  if (firstLoop) {
    serialWait();
    logOptosMain();
    printOutput();
    firstLoop = false;
  }
}











void logOptosMain() {
  do {
    int C_limit_in = digitalRead(C_limit);

    if (C_limit_in == 0) // start logging when limit sensor is first triggered (when bar blocks light)
      logOptos = 1;  

    if (logOptos == 1) {
       int C_position_in = digitalRead(C_position);

      // count transitions in position sensor (this is not the sample rate!! )
      if (C_position_in != save_C_pos_state) {
        C_pos_count++;
        save_C_pos_state = C_position_in;
      }
    
      outputArray[thisSample][outputC_pos] =C_position_in;
      outputArray[thisSample][outputC_pos_count] =C_pos_count;

       // stop logging after 150 transitions / 75 waves past limit trigger 
      if (C_pos_count > 150) { stopLogging = 1; }
      delayMicroseconds(90);
      thisSample++;
    }
  }while (stopLogging == 0); 

  Serial.println("");
  Serial.println("_____________LOGGING COMPLETE __________________________");

 
}











// output the data to  PuTTY
void printOutput() {
  // headings
  Serial.print("Tick");
  Serial.print("\t");
  Serial.print("CTransitions");
  Serial.print("\t");
  Serial.println("CPos");

  int samples=sizeof(outputArray) / sizeof(outputArray[0]) ;
  for(int i = 0; i < samples; i++)
  {
    Serial.print(i);
    Serial.print("\t");

    Serial.print(outputArray[i][outputC_pos]);  
    Serial.print("\t");

    Serial.println(outputArray[i][outputC_pos_count]);  
 
    
  }
  Serial.println("");
  Serial.println("_____________ END DATA TO SERIAL __________________________");  
}




// output dots to serial for 10sec giving time to open PuTTY
void serialWait()
{
  for (int i = 0; i <= 100; i++) {
  Serial.print(".");
  delay(100);
  }    

Serial.println("");
Serial.println("______ READY _________");
}

And here is my output which shows ~110 transitions:

This condition should never be true, thus the loop should never stop?

if (C_pos_count > 150) { stopLogging = 1; }

It's a UNO R4. Thanks!



Tick    CTransitions    CPos
0       1       1
1       1       1
2       1       1
3       1       1
4       1       1
5       1       1
6       1       1
7       1       1
8       1       1
9       1       1
10      1       1
11      1       1
12      1       1
13      1       1
14      1       1
15      1       1
16      1       1
17      1       1
18      1       1
19      1       1
20      1       1
21      1       1
22      1       1
23      1       1
24      1       1
25      1       1
26      1       1
27      1       1
28      1       1
29      1       1
30      1       1
31      1       1
32      1       1
33      1       1
34      1       1
35      1       1
36      1       1
37      1       1
38      1       1
39      1       1
40      1       1
41      1       1
42      1       1
43      0       2
44      0       2
45      0       2
46      0       2
47      0       2
48      0       2
49      0       2
50      0       2
51      0       2
52      0       2
53      0       2
54      0       2
55      0       2
56      0       2
57      0       2
58      0       2
59      0       2
60      0       2
61      0       2
62      1       3
63      1       3
64      1       3
65      1       3
66      1       3
67      1       3
68      1       3
69      1       3
70      1       3
71      1       3
72      1       3
73      1       3
74      1       3
75      1       3
76      1       3
77      1       3
78      1       3
79      1       3
80      1       3
81      1       3
82      1       3
83      1       3
84      1       3
85      1       3
86      1       3
87      1       3
88      1       3
89      1       3
90      1       3
91      1       3
92      1       3
93      1       3
94      1       3
95      1       3
96      1       3
97      1       3
98      1       3
99      1       3
100     1       3
101     1       3
102     1       3
103     1       3
104     1       3
105     1       3
106     1       3
107     1       3
108     1       3
109     1       3
110     0       4
111     0       4
112     0       4
113     0       4
114     0       4
115     0       4
116     0       4
117     0       4
118     0       4
119     0       4
120     0       4
121     0       4
122     0       4
123     0       4
124     0       4
125     0       4
126     0       4
127     0       4
128     1       5
129     1       5
130     1       5
131     1       5
132     1       5
133     1       5
134     1       5
135     1       5
136     1       5
137     1       5
138     1       5
139     1       5
140     1       5
141     1       5
142     1       5
143     1       5
144     1       5
145     1       5
146     1       5
147     1       5
148     1       5
149     1       5
150     1       5
151     1       5
152     1       5
153     1       5
154     1       5
155     1       5
156     1       5
157     1       5
158     1       5
159     1       5
160     1       5
161     1       5
162     1       5
163     1       5
164     1       5
165     1       5
166     1       5
167     1       5
168     1       5
169     1       5
170     1       5
171     1       5
172     1       5
173     1       5
174     0       6
175     0       6
176     0       6
177     0       6
178     0       6
179     0       6
180     0       6
181     0       6
182     0       6
183     0       6
184     0       6
185     0       6
186     0       6
187     0       6
188     0       6
189     0       6
190     0       6
191     1       7
192     1       7
193     1       7
194     1       7
195     1       7
196     1       7
197     1       7
198     1       7
199     1       7
200     1       7
201     1       7
202     1       7
203     1       7
204     1       7
205     1       7
206     1       7
207     1       7
208     1       7
209     1       7
210     1       7
211     1       7
212     1       7
213     1       7
214     1       7
215     1       7
216     1       7
217     1       7
218     1       7
219     1       7
220     1       7
221     1       7
222     1       7
223     1       7
224     1       7
225     1       7
226     1       7
227     1       7
228     1       7
229     1       7
230     1       7
231     1       7
232     1       7
233     1       7
234     0       8
235     0       8
236     0       8
237     0       8
238     0       8
239     0       8
240     0       8
241     0       8
242     0       8
243     0       8
244     0       8
245     0       8
246     0       8
247     0       8
248     0       8
249     0       8
250     0       8
251     1       9
252     1       9
253     1       9
254     1       9
255     1       9
256     1       9
257     1       9
258     1       9
259     1       9
260     1       9
261     1       9
262     1       9
263     1       9
264     1       9
265     1       9
266     1       9
267     1       9
268     1       9
269     1       9
270     1       9
271     1       9
272     1       9
273     1       9
274     1       9
275     1       9
276     1       9
277     1       9
278     1       9
279     1       9
280     1       9
281     1       9
282     1       9
283     1       9
284     1       9
285     1       9
286     1       9
287     1       9
288     1       9
289     1       9
290     1       9
291     0       10
292     0       10
293     0       10
294     0       10
295     0       10
296     0       10
297     0       10
298     0       10
299     0       10
300     0       10
301     0       10
302     0       10
303     0       10
304     0       10
305     0       10
306     0       10
307     1       11
308     1       11
309     1       11
310     1       11
311     1       11
312     1       11
313     1       11
314     1       11
315     1       11
316     1       11
317     1       11
318     1       11
319     1       11
320     1       11
321     1       11
322     1       11
323     1       11
324     1       11
325     1       11
326     1       11
327     1       11
328     1       11
329     1       11
330     1       11
331     1       11
332     1       11
333     1       11
334     1       11
335     1       11
336     1       11
337     1       11
338     1       11
339     1       11
340     1       11
341     1       11
342     1       11
343     1       11
344     1       11
345     0       12
346     0       12
347     0       12
348     0       12
349     0       12
350     0       12
351     0       12
352     0       12
353     0       12
354     0       12
355     0       12
356     0       12
357     0       12
358     0       12
359     0       12
360     0       12
361     1       13
362     1       13
363     1       13
364     1       13
365     1       13
366     1       13
367     1       13
368     1       13
369     1       13
370     1       13
371     1       13
372     1       13
373     1       13
374     1       13
375     1       13
376     1       13
377     1       13
378     1       13
379     1       13
380     1       13
381     1       13
382     1       13
383     1       13
384     1       13
385     1       13
386     1       13
387     1       13
388     1       13
389     1       13
390     1       13
391     1       13
392     1       13
393     1       13
394     1       13
395     1       13
396     0       14
397     0       14
398     0       14
399     0       14
400     0       14
401     0       14
402     0       14
403     0       14
404     0       14
405     0       14
406     0       14
407     0       14
408     0       14
409     0       14
410     1       15
411     1       15
412     1       15
413     1       15
414     1       15
415     1       15
416     1       15
417     1       15
418     1       15
419     1       15
420     1       15
421     1       15
422     1       15
423     1       15
424     1       15
425     1       15
426     1       15
427     1       15
428     1       15
429     1       15
430     1       15
431     1       15
432     1       15
433     1       15
434     1       15
435     1       15
436     1       15
437     1       15
438     1       15
439     1       15
440     1       15
441     1       15
442     1       15
443     1       15
444     1       15
445     1       15
446     0       16
447     0       16
448     0       16
449     0       16
450     0       16
451     0       16
452     0       16
453     0       16
454     0       16
455     0       16
456     0       16
457     0       16
458     0       16
459     1       17
460     1       17
461     1       17
462     1       17
463     1       17
464     1       17
465     1       17
466     1       17
467     1       17
468     1       17
469     1       17
470     1       17
471     1       17
472     1       17
473     1       17
474     1       17
475     1       17
476     1       17
477     1       17
478     1       17
479     1       17
480     1       17
481     1       17
482     1       17
483     1       17
484     1       17
485     1       17
486     1       17
487     1       17
488     1       17
489     1       17
490     1       17
491     1       17
492     1       17
493     1       17
494     1       17
495     1       17
496     1       17
497     1       17
498     0       18
499     0       18
500     0       18
501     0       18
502     0       18
503     0       18
504     0       18
505     0       18
506     0       18
507     0       18
508     0       18
509     0       18
510     1       19
511     1       19
512     1       19
513     1       19
514     1       19
515     1       19
516     1       19
517     1       19
518     1       19
519     1       19
520     1       19
521     1       19
522     1       19
523     1       19
524     1       19
525     1       19
526     1       19
527     1       19
528     1       19
529     1       19
530     1       19
531     1       19
532     1       19
533     1       19
534     1       19
535     1       19
536     1       19
537     1       19
538     1       19
539     1       19
540     1       19
541     1       19
542     1       19
543     1       19
544     1       19
545     1       19
546     1       19
547     1       19
548     1       19
549     1       19
550     1       19
551     0       20
552     0       20
553     0       20
554     0       20
555     0       20
556     0       20
557     0       20
558     0       20
559     0       20
560     0       20
561     0       20
562     0       20
563     1       21
564     1       21
565     1       21
566     1       21
567     1       21
568     1       21
569     1       21
570     1       21
571     1       21
572     1       21
573     1       21
574     1       21
575     1       21
576     1       21
577     1       21
578     1       21
579     1       21
580     1       21
581     1       21
582     1       21
583     1       21
584     1       21
585     1       21
586     1       21
587     1       21
588     1       21
589     1       21
590     1       21
591     1       21
592     1       21
593     1       21
594     1       21
595     1       21
596     1       21
597     1       21
598     1       21
599     1       21
600     1       21
601     0       22
602     0       22
603     0       22
604     0       22
605     0       22
606     0       22
607     0       22
608     0       22
609     0       22
610     0       22
611     0       22
612     0       22
613     0       22
614     1       23
615     1       23
616     1       23
617     1       23
618     1       23
619     1       23
620     1       23
621     1       23
622     1       23
623     1       23
624     1       23
625     1       23
626     1       23
627     1       23
628     1       23
629     1       23
630     1       23
631     1       23
632     1       23
633     1       23
634     1       23
635     1       23
636     1       23
637     1       23
638     1       23
639     1       23
640     1       23
641     1       23
642     1       23
643     1       23
644     1       23
645     0       24
646     0       24
647     0       24
648     0       24
649     0       24
650     0       24
651     0       24
652     0       24
653     0       24
654     0       24
655     0       24
656     0       24
657     0       24
658     1       25
659     1       25
660     1       25
661     1       25
662     1       25
663     1       25
664     1       25
665     1       25
666     1       25
667     1       25
668     1       25
669     1       25
670     1       25
671     1       25
672     1       25
673     1       25
674     1       25
675     1       25
676     1       25
677     1       25
678     1       25
679     1       25
680     1       25
681     1       25
682     1       25
683     1       25
684     0       26
685     0       26
686     0       26
687     0       26
688     0       26
689     0       26
690     0       26
691     0       26
692     0       26
693     0       26
694     0       26
695     1       27
696     1       27
697     1       27
698     1       27
699     1       27
700     1       27
701     1       27
702     1       27
703     1       27
704     1       27
705     1       27
706     1       27
707     1       27
708     1       27
709     1       27
710     1       27
711     1       27
712     1       27
713     1       27
714     1       27
715     1       27
716     1       27
717     1       27
718     1       27
719     1       27
720     1       27
721     1       27
722     1       27
723     1       27
724     1       27
725     1       27
726     1       27
727     1       27
728     1       27
729     1       27
730     1       27
731     1       27
732     1       27
733     1       27
734     0       28
735     0       28
736     0       28
737     0       28
738     0       28
739     0       28
740     0       28
741     0       28
742     0       28
743     0       28
744     0       28
745     0       28
746     1       29
747     1       29
748     1       29
749     1       29
750     1       29
751     1       29
752     1       29
753     1       29
754     1       29
755     1       29
756     1       29
757     1       29
758     1       29
759     1       29
760     1       29
761     1       29
762     1       29
763     1       29
764     1       29
765     1       29
766     1       29
767     1       29
768     1       29
769     1       29
770     1       29
771     1       29
772     1       29
773     1       29
774     1       29
775     1       29
776     1       29
777     1       29
778     1       29
779     1       29
780     1       29
781     1       29
782     1       29
783     1       29
784     0       30
785     0       30
786     0       30
787     0       30
788     0       30
789     0       30
790     0       30
791     0       30
792     0       30
793     0       30
794     0       30
795     0       30
796     0       30
797     1       31
798     1       31
799     1       31
800     1       31
801     1       31
802     1       31
803     1       31
804     1       31
805     1       31
806     1       31
807     1       31
808     1       31
809     1       31
810     1       31
811     1       31
812     1       31
813     1       31
814     1       31
815     1       31
816     1       31
817     1       31
818     1       31
819     1       31
820     1       31
821     1       31
822     1       31
823     1       31
824     1       31
825     1       31
826     1       31
827     1       31
828     1       31
829     1       31
830     1       31
831     1       31
832     1       31
833     1       31
834     1       31
835     1       31
836     1       31
837     0       32
838     0       32
839     0       32
840     0       32
841     0       32
842     0       32
843     0       32
844     0       32
845     0       32
846     0       32
847     0       32
848     0       32
849     1       33
850     1       33
851     1       33
852     1       33
853     1       33
854     1       33
855     1       33
856     1       33
857     1       33
858     1       33
859     1       33
860     1       33
861     1       33
862     1       33
863     1       33
864     1       33
865     1       33
866     1       33
867     1       33
868     1       33
869     1       33
870     1       33
871     1       33
872     1       33
873     1       33
874     1       33
875     1       33
876     1       33
877     1       33
878     1       33
879     1       33
880     1       33
881     1       33
882     1       33
883     1       33
884     0       34
885     0       34
886     0       34
887     0       34
888     0       34
889     0       34
890     0       34
891     0       34
892     0       34
893     0       34
894     0       34
895     1       35
896     1       35
897     1       35
898     1       35
899     1       35
900     1       35
901     1       35
902     1       35
903     1       35
904     1       35
905     1       35
906     1       35
907     1       35
908     1       35
909     1       35
910     1       35
911     1       35
912     1       35
913     1       35
914     1       35
915     1       35
916     1       35
917     1       35
918     1       35
919     1       35
920     1       35
921     1       35
922     1       35
923     1       35
924     1       35
925     1       35
926     1       35
927     1       35
928     1       35
929     0       36
930     0       36
931     0       36
932     0       36
933     0       36
934     0       36
935     0       36
936     0       36
937     0       36
938     0       36
939     0       36
940     1       37
941     1       37
942     1       37
943     1       37
944     1       37
945     1       37
946     1       37
947     1       37
948     1       37
949     1       37
950     1       37
951     1       37
952     1       37
953     1       37
954     1       37
955     1       37
956     1       37
957     1       37
958     1       37
959     1       37
960     1       37
961     1       37
962     1       37
963     1       37
964     1       37
965     1       37
966     1       37
967     1       37
968     1       37
969     1       37
970     1       37
971     0       38
972     0       38
973     0       38
974     0       38
975     0       38
976     0       38
977     0       38
978     0       38
979     0       38
980     0       38
981     0       38
982     1       39
983     1       39
984     1       39
985     1       39
986     1       39
987     1       39
988     1       39
989     1       39
990     1       39
991     1       39
992     1       39
993     1       39
994     1       39
995     1       39
996     1       39
997     1       39
998     1       39
999     1       39
1000    1       39
1001    1       39
1002    1       39
1003    1       39
1004    1       39
1005    1       39
1006    1       39
1007    1       39
1008    1       39
1009    1       39
1010    1       39
1011    1       39
1012    1       39
1013    0       40
1014    0       40
1015    0       40
1016    0       40
1017    0       40
1018    0       40
1019    0       40
1020    0       40
1021    0       40
1022    0       40
1023    1       41
1024    1       41
1025    1       41
1026    1       41
1027    1       41
1028    1       41
1029    1       41
1030    1       41
1031    1       41
1032    1       41
1033    1       41
1034    1       41
1035    1       41
1036    1       41
1037    1       41
1038    1       41
1039    1       41
1040    1       41
1041    1       41
1042    1       41
1043    1       41
1044    1       41
1045    1       41
1046    1       41
1047    1       41
1048    1       41
1049    1       41
1050    1       41
1051    1       41
1052    1       41
1053    1       41
1054    0       42
1055    0       42
1056    0       42
1057    0       42
1058    0       42
1059    0       42
1060    0       42
1061    0       42
1062    0       42
1063    1       43
1064    1       43
1065    1       43
1066    1       43
1067    1       43
1068    1       43
1069    1       43
1070    1       43
1071    1       43
1072    1       43
1073    1       43
1074    1       43
1075    1       43
1076    1       43
1077    1       43
1078    1       43
1079    1       43
1080    1       43
1081    1       43
1082    1       43
1083    1       43
1084    1       43
1085    1       43
1086    1       43
1087    1       43
1088    1       43
1089    1       43
1090    1       43
1091    0       44
1092    0       44
1093    0       44
1094    0       44
1095    0       44
1096    0       44
1097    0       44
1098    0       44
1099    0       44
1100    1       45
1101    1       45
1102    1       45
1103    1       45
1104    1       45
1105    1       45
1106    1       45
1107    1       45
1108    1       45
1109    1       45
1110    1       45
1111    1       45
1112    1       45
1113    1       45
1114    1       45
1115    1       45
1116    1       45
1117    1       45
1118    1       45
1119    1       45
1120    1       45
1121    1       45
1122    1       45
1123    1       45
1124    1       45
1125    1       45
1126    1       45
1127    0       46
1128    0       46
1129    0       46
1130    0       46
1131    0       46
1132    0       46
1133    0       46
1134    0       46
1135    1       47
1136    1       47
1137    1       47
1138    1       47
1139    1       47
1140    1       47
1141    1       47
1142    1       47
1143    1       47
1144    1       47
1145    1       47
1146    1       47
1147    1       47
1148    1       47
1149    1       47
1150    1       47
1151    1       47
1152    1       47
1153    1       47
1154    1       47
1155    1       47
1156    1       47
1157    1       47
1158    1       47
1159    1       47
1160    1       47
1161    0       48
1162    0       48
1163    0       48
1164    0       48
1165    0       48
1166    0       48
1167    0       48
1168    0       48
1169    1       49
1170    1       49
1171    1       49
1172    1       49
1173    1       49
1174    1       49
1175    1       49
1176    1       49
1177    1       49
1178    1       49
1179    1       49
1180    1       49
1181    1       49
1182    1       49
1183    1       49
1184    1       49
1185    1       49
1186    1       49
1187    1       49
1188    1       49
1189    1       49
1190    1       49
1191    1       49
1192    1       49
1193    1       49
1194    1       49
1195    1       49
1196    0       50
1197    0       50
1198    0       50
1199    0       50
1200    0       50
1201    0       50
1202    0       50
1203    1       51
1204    1       51
1205    1       51
1206    1       51
1207    1       51
1208    1       51
1209    1       51
1210    1       51
1211    1       51
1212    1       51
1213    1       51
1214    1       51
1215    1       51
1216    1       51
1217    1       51
1218    1       51
1219    1       51
1220    1       51
1221    1       51
1222    1       51
1223    1       51
1224    1       51
1225    1       51
1226    1       51
1227    1       51
1228    1       51
1229    1       51
1230    0       52
1231    0       52
1232    0       52
1233    0       52
1234    0       52
1235    0       52
1236    0       52
1237    1       53
1238    1       53
1239    1       53
1240    1       53
1241    1       53
1242    1       53
1243    1       53
1244    1       53
1245    1       53
1246    1       53
1247    1       53
1248    1       53
1249    1       53
1250    1       53
1251    1       53
1252    1       53
1253    1       53
1254    1       53
1255    1       53
1256    1       53
1257    1       53
1258    1       53
1259    1       53
1260    1       53
1261    1       53
1262    0       54
1263    0       54
1264    0       54
1265    0       54
1266    0       54
1267    0       54
1268    1       55
1269    1       55
1270    1       55
1271    1       55
1272    1       55
1273    1       55
1274    1       55
1275    1       55
1276    1       55
1277    1       55
1278    1       55
1279    1       55
1280    1       55
1281    1       55
1282    1       55
1283    1       55
1284    1       55
1285    1       55
1286    1       55
1287    1       55
1288    1       55
1289    1       55
1290    1       55
1291    1       55
1292    1       55
1293    1       55
1294    0       56
1295    0       56
1296    0       56
1297    0       56
1298    0       56
1299    0       56
1300    0       56
1301    1       57
1302    1       57
1303    1       57
1304    1       57
1305    1       57
1306    1       57
1307    1       57
1308    1       57
1309    1       57
1310    1       57
1311    1       57
1312    1       57
1313    1       57
1314    1       57
1315    1       57
1316    1       57
1317    1       57
1318    1       57
1319    1       57
1320    1       57
1321    1       57
1322    1       57
1323    1       57
1324    0       58
1325    0       58
1326    0       58
1327    0       58
1328    0       58
1329    0       58
1330    1       59
1331    1       59
1332    1       59
1333    1       59
1334    1       59
1335    1       59
1336    1       59
1337    1       59
1338    1       59
1339    1       59
1340    1       59
1341    1       59
1342    1       59
1343    1       59
1344    1       59
1345    1       59
1346    1       59
1347    1       59
1348    1       59
1349    1       59
1350    1       59
1351    1       59
1352    1       59
1353    1       59
1354    1       59
1355    1       59
1356    0       60
1357    0       60
1358    1       61
1359    1       61
1360    1       61
1361    1       61
1362    1       61
1363    1       61
1364    1       61
1365    1       61
1366    1       61
1367    1       61
1368    1       61
1369    1       61
1370    1       61
1371    1       61
1372    1       61
1373    1       61
1374    1       61
1375    1       61
1376    1       61
1377    1       61
1378    1       61
1379    1       61
1380    1       61
1381    1       61
1382    1       61
1383    0       62
1384    0       62
1385    0       62
1386    0       62
1387    0       62
1388    0       62
1389    1       63
1390    1       63
1391    1       63
1392    1       63
1393    1       63
1394    1       63
1395    1       63
1396    1       63
1397    1       63
1398    1       63
1399    1       63
1400    1       63
1401    1       63
1402    1       63
1403    1       63
1404    1       63
1405    1       63
1406    1       63
1407    0       64
1408    0       64
1409    0       64
1410    0       64
1411    0       64
1412    0       64
1413    1       65
1414    1       65
1415    1       65
1416    1       65
1417    1       65
1418    1       65
1419    1       65
1420    1       65
1421    1       65
1422    1       65
1423    1       65
1424    1       65
1425    1       65
1426    1       65
1427    1       65
1428    1       65
1429    1       65
1430    1       65
1431    1       65
1432    1       65
1433    0       66
1434    0       66
1435    0       66
1436    0       66
1437    0       66
1438    1       67
1439    1       67
1440    1       67
1441    1       67
1442    1       67
1443    1       67
1444    1       67
1445    1       67
1446    1       67
1447    1       67
1448    1       67
1449    1       67
1450    1       67
1451    1       67
1452    1       67
1453    1       67
1454    1       67
1455    1       67
1456    0       68
1457    0       68
1458    0       68
1459    0       68
1460    1       69
1461    1       69
1462    1       69
1463    1       69
1464    1       69
1465    1       69
1466    1       69
1467    1       69
1468    1       69
1469    1       69
1470    1       69
1471    1       69
1472    1       69
1473    1       69
1474    1       69
1475    1       69
1476    1       69
1477    1       69
1478    1       69
1479    0       70
1480    0       70
1481    0       70
1482    0       70
1483    0       70
1484    0       70
1485    1       71
1486    1       71
1487    1       71
1488    1       71
1489    1       71
1490    1       71
1491    1       71
1492    1       71
1493    1       71
1494    1       71
1495    1       71
1496    1       71
1497    1       71
1498    1       71
1499    1       71
1500    1       71
1501    1       71
1502    1       71
1503    1       71
1504    1       71
1505    1       71
1506    1       71
1507    0       72
1508    0       72
1509    0       72
1510    0       72
1511    0       72
1512    0       72
1513    1       73
1514    1       73
1515    1       73
1516    1       73
1517    1       73
1518    1       73
1519    1       73
1520    1       73
1521    1       73
1522    1       73
1523    1       73
1524    1       73
1525    1       73
1526    1       73
1527    1       73
1528    1       73
1529    1       73
1530    1       73
1531    1       73
1532    1       73
1533    0       74
1534    0       74
1535    0       74
1536    0       74
1537    0       74
1538    1       75
1539    1       75
1540    1       75
1541    1       75
1542    1       75
1543    1       75
1544    1       75
1545    1       75
1546    1       75
1547    1       75
1548    1       75
1549    1       75
1550    1       75
1551    1       75
1552    1       75
1553    1       75
1554    1       75
1555    1       75
1556    1       75
1557    0       76
1558    0       76
1559    0       76
1560    0       76
1561    0       76
1562    1       77
1563    1       77
1564    1       77
1565    1       77
1566    1       77
1567    1       77
1568    1       77
1569    1       77
1570    1       77
1571    1       77
1572    1       77
1573    1       77
1574    1       77
1575    1       77
1576    1       77
1577    1       77
1578    0       78
1579    0       78
1580    0       78
1581    0       78
1582    0       78
1583    1       79
1584    1       79
1585    1       79
1586    1       79
1587    1       79
1588    1       79
1589    1       79
1590    1       79
1591    1       79
1592    1       79
1593    1       79
1594    1       79
1595    1       79
1596    1       79
1597    1       79
1598    1       79
1599    0       80
1600    0       80
1601    0       80
1602    0       80
1603    0       80
1604    1       81
1605    1       81
1606    1       81
1607    1       81
1608    1       81
1609    1       81
1610    1       81
1611    1       81
1612    1       81
1613    1       81
1614    1       81
1615    1       81
1616    1       81
1617    1       81
1618    1       81
1619    0       82
1620    0       82
1621    0       82
1622    0       82
1623    1       83
1624    1       83
1625    1       83
1626    1       83
1627    1       83
1628    1       83
1629    1       83
1630    1       83
1631    1       83
1632    1       83
1633    1       83
1634    1       83
1635    1       83
1636    1       83
1637    1       83
1638    1       83
1639    1       83
1640    1       83
1641    0       84
1642    0       84
1643    0       84
1644    0       84
1645    0       84
1646    1       85
1647    1       85
1648    1       85
1649    1       85
1650    1       85
1651    1       85
1652    1       85
1653    1       85
1654    1       85
1655    1       85
1656    1       85
1657    1       85
1658    1       85
1659    1       85
1660    1       85
1661    1       85
1662    0       86
1663    0       86
1664    0       86
1665    0       86
1666    0       86
1667    1       87
1668    1       87
1669    1       87
1670    1       87
1671    1       87
1672    1       87
1673    1       87
1674    1       87
1675    1       87
1676    1       87
1677    1       87
1678    1       87
1679    1       87
1680    1       87
1681    1       87
1682    1       87
1683    1       87
1684    1       87
1685    0       88
1686    0       88
1687    0       88
1688    0       88
1689    1       89
1690    1       89
1691    1       89
1692    1       89
1693    1       89
1694    1       89
1695    1       89
1696    1       89
1697    1       89
1698    1       89
1699    1       89
1700    1       89
1701    1       89
1702    1       89
1703    1       89
1704    1       89
1705    1       89
1706    1       89
1707    0       90
1708    0       90
1709    0       90
1710    0       90
1711    0       90
1712    1       91
1713    1       91
1714    1       91
1715    1       91
1716    1       91
1717    1       91
1718    1       91
1719    1       91
1720    1       91
1721    1       91
1722    1       91
1723    1       91
1724    1       91
1725    1       91
1726    1       91
1727    1       91
1728    1       91
1729    1       91
1730    1       91
1731    0       92
1732    0       92
1733    0       92
1734    0       92
1735    1       93
1736    1       93
1737    1       93
1738    1       93
1739    1       93
1740    1       93
1741    1       93
1742    1       93
1743    1       93
1744    1       93
1745    1       93
1746    1       93
1747    1       93
1748    1       93
1749    1       93
1750    1       93
1751    1       93
1752    1       93
1753    0       94
1754    0       94
1755    0       94
1756    0       94
1757    0       94
1758    1       95
1759    1       95
1760    1       95
1761    1       95
1762    1       95
1763    1       95
1764    1       95
1765    1       95
1766    1       95
1767    1       95
1768    1       95
1769    1       95
1770    1       95
1771    1       95
1772    1       95
1773    1       95
1774    1       95
1775    1       95
1776    1       95
1777    0       96
1778    0       96
1779    0       96
1780    0       96
1781    0       96
1782    1       97
1783    1       97
1784    1       97
1785    1       97
1786    1       97
1787    1       97
1788    1       97
1789    1       97
1790    1       97
1791    1       97
1792    1       97
1793    1       97
1794    1       97
1795    1       97
1796    1       97
1797    1       97
1798    1       97
1799    1       97
1800    1       97
1801    1       97
1802    1       97
1803    0       98
1804    0       98
1805    0       98
1806    0       98
1807    1       99
1808    1       99
1809    1       99
1810    1       99
1811    1       99
1812    1       99
1813    1       99
1814    1       99
1815    1       99
1816    1       99
1817    1       99
1818    1       99
1819    1       99
1820    1       99
1821    1       99
1822    1       99
1823    1       99
1824    1       99
1825    1       99
1826    1       99
1827    1       99
1828    0       100
1829    0       100
1830    0       100
1831    0       100
1832    1       101
1833    1       101
1834    1       101
1835    1       101
1836    1       101
1837    1       101
1838    1       101
1839    1       101
1840    1       101
1841    1       101
1842    1       101
1843    1       101
1844    1       101
1845    1       101
1846    1       101
1847    1       101
1848    1       101
1849    1       101
1850    1       101
1851    1       101
1852    1       101
1853    1       101
1854    0       102
1855    0       102
1856    0       102
1857    0       102
1858    0       102
1859    1       103
1860    1       103
1861    1       103
1862    1       103
1863    1       103
1864    1       103
1865    1       103
1866    1       103
1867    1       103
1868    1       103
1869    1       103
1870    1       103
1871    1       103
1872    1       103
1873    1       103
1874    1       103
1875    1       103
1876    1       103
1877    1       103
1878    1       103
1879    1       103
1880    1       103
1881    1       103
1882    1       103
1883    0       104
1884    0       104
1885    0       104
1886    0       104
1887    1       105
1888    1       105
1889    1       105
1890    1       105
1891    1       105
1892    1       105
1893    1       105
1894    1       105
1895    1       105
1896    1       105
1897    1       105
1898    1       105
1899    1       105
1900    1       105
1901    1       105
1902    1       105
1903    1       105
1904    1       105
1905    1       105
1906    1       105
1907    1       105
1908    1       105
1909    1       105
1910    1       105
1911    1       105
1912    1       105
1913    0       106
1914    0       106
1915    0       106
1916    0       106
1917    0       106
1918    1       107
1919    1       107
1920    1       107
1921    1       107
1922    1       107
1923    1       107
1924    1       107
1925    1       107
1926    1       107
1927    1       107
1928    1       107
1929    1       107
1930    1       107
1931    1       107
1932    1       107
1933    1       107
1934    1       107
1935    1       107
1936    1       107
1937    1       107
1938    1       107
1939    1       107
1940    1       107
1941    1       107
1942    1       107
1943    1       107
1944    1       107
1945    1       107
1946    0       108
1947    0       108
1948    0       108
1949    0       108
1950    0       108
1951    0       108
1952    1       109
1953    1       109
1954    1       109
1955    1       109
1956    1       109
1957    1       109
1958    1       109
1959    1       109
1960    1       109
1961    1       109
1962    1       109
1963    1       109
1964    1       109
1965    1       109
1966    1       109
1967    1       109
1968    1       109
1969    1       109
1970    1       109
1971    1       109
1972    1       109
1973    1       109
1974    1       109
1975    1       109
1976    1       109
1977    1       109
1978    1       109
1979    1       109
1980    1       109
1981    1       109
1982    1       109
1983    0       110
1984    0       110
1985    0       110
1986    0       110
1987    0       110
1988    0       110
1989    0       110
1990    1       111
1991    1       111
1992    1       111
1993    1       111
1994    1       111
1995    1       111
1996    1       111
1997    1       111
1998    1       111
1999    1       1

_____________ END DATA TO SERIAL __________________________

It is because you only declare an array of 2000 logs:
outputArray[2000][2]
and when it tried to log the 2001th time, it crashed the whole program.
I don't know why you want to log duplicate state when it has not changed.
quick fix is to put the log inside the "if" condition when it confirmed state has changed

if (C_position_in != save_C_pos_state) {
        outputArray[C_pos_count][outputC_pos] =C_position_in;
        outputArray[C_pos_count][outputC_pos_count] =C_pos_count;
        C_pos_count++;
        save_C_pos_state = C_position_in;
      }

But if you want to keep the "tick" printout, then just increase the array to 3000 or something

1 Like

And I have to admit, your topic title made me click. Good job!

1 Like

Put your 2000 lines of output in a code block.

  1. Open your post for editing
  2. CUT all the numbers.
  3. Click the 5. < CODE > button in the post/edit window
  4. Where you see ```type or paste code here``` paste your cut numbers
  5. Click SUBMIT
1 Like

Thanks, I tried that twice, third time lucky. Maybe because I was rushing, late for something!

Glad you liked my title :slight_smile:

I appreciate your suggestion, but I do need to record the ticks (in the complete sketch, this is just a demo to show my problem).

I did think that it was due to reaching the end of the declared array, but it goes on to execute the printOutput() function, so has not crashed? Unless it just crashed the logOptosMain() function and carried on?

PS I'm marginal on memory so can't increase the array, but in any case that would not answer why it's working when the end loop condition is not met. Unless as per my last sentence above.

Thank you.

1 Like

Moved to programming questions.

I don't understand why you couldn't start your topic there.

1 Like
  • Well, when you access a memory location out of your declared array size, the system will work abnormally and you can't find persistent, logic answers for an abnormal system.
  • I have tested with an esp32 board and with a quick sketch and found out.
  • If I made a 1-dimentional array. It will crash immediately when I try to access a memory location out of the array region. This makes sense.
  • Now when I tried 2-dimentional array, I can access beyond the array region (and how far I can access before it stop running was random). And when I think about it, 2-dimentional array in C++ is actually a 1-dimentional array of pointes, each points to a real 1-dimentional array. And with a pointer, the compiler cannot predict what memory location it points to so you won't get any warning or error until you access a memory location that is "protected/prohibited" and your system will crash.

So in your case, the system might not have been crashed, because the memory beyond 2000 isn't protected or prohibited.
You can try this by accessing beyond 2000 in your print fuction and you should still get the correct results.
HOWEVER, as I said, working with an abnormal system won't guarantee persistent results. You should try another approach instead of letting the function accessing memory location beyond your declared array.

1 Like

Hi sorry for the delay in getting back, and thanks for your time looking into this.

I tried your suggestion of accessing past the array end, I defined my array as 1,000 and hard coded my outout loop to 3,000, here's a sample of the output:

1205    0       0
1206    0       0
1207    0       0
1208    0       0
1209    1074118656      536880928
1210    1380009027      0
1211    0       32832
1212    36665   0
1213    0       131328
1214    -539033600      3084
1215    50331648        -539033600
1216    3084    36665
1217    0       536880916

So it seems what you say about the array is true.

However, I don't believe this answers my question as to why there is not an infinite loop in the logOptosMain function when the exit condition is not met.

To test the logic, I reduced if (C_pos_count > 150) { stopLogging = 1; } from 150 to 70, and it stopped as expected. I then commented out this line so there is no means for the loop to stop, and got the results I posted before!

I'm going to rethink my code to avoid this situation, so my thread has now become an academic one. So, by all means either leave it at that or you are welcome to post more!

Thanks :slight_smile:

Not when I think about it. Review multidimensional arrays in C/C++.

a7

Sorry my bad.
Do you have an explanation for him? I'm curious too.

Yes. Except it may work fine, for some time, until you change some things that change something, and the improper access starts making trouble. Almost anything can make the issues start to manifest.

It is fun but makes no sense to learn why or just how, in some particular case, one can or might not get away with doing.

These can be very hard errors to find, as they can litecause anything to happen anywhere.

a7

1 Like

Ok, so it looks to me like you said to do
all that stuff once before checking the while (stopLogging == 0); condition which will be false after 150 transitions, no?

and of course, now that that condition doesn't match, it looks like you block out any looping possibility by toggling the firstLoop flag after a single run through of your core functions.

So, do{}...while() seems to be your issue if you want the loop to go on forever inside the do instruction set, if I'm understanding your dilemma correctly.

The do…​while loop works in the same manner as the while loop, with the exception that the condition is tested at the end of the loop, so the do loop will always run at least once.

source:

1 Like

Hi @hallowed31 I'm not fully following your post, but it seems you might not be following mine either (and I don't mean that in a sarcastic way!).

Starting with the second bit of my code you quoted, I just want logOptosMain() and associated functions to run once per power on of the board, this is working as expected.

As for the do while inside of logOptosMain() I do not want it to run indefinitely. My question is why it appears to stop when I never reach 150 transitions.

Hi @username_not_available not a clue! As I said before, my only wild guess is that trying to add to the array after 2,000 iterations is stopping the loop, but not causing a fatal crash and allowing the rest of the code to continue. I'm going to do a rewrite to simplify things.

Thanks @alto777 Yes I think I am in the territory of almost random outcomes, which happened to fall in my favor, for now...

Yeah, without the gear in front of me this is difficult to visualize.
But coffee is kicking in and I had some shower thoughts:

  • the stuff that's only supposed to only happen once, would it do well to move it to the end of void setup()?
  • I don't love do...while, almost never need it. Why not regular while()?
  • in using while() (or any function you might break down into smaller tasks than you have), when you iterate things, did you consider using a for(int i = 0; i < n; i++) or similar loop?
  • the free running void loop() is awesome but sometimes we need more control. Sounds like you might benefit from encompassing all your "run once" or "run occasionally" or "always check and use, though" sketch flow in a finite state machine. The switch/case/break control structure is awesome for this.

I've just done a rewrite using much simpler control logic for the loop and it's working perfectly and I now have confidence in it.

Thanks everyone for your help. I'm going to try reading the i/o pins via the registers so I can get a faster sample rate, so no doubt I'll be back again!!

1 Like

So, this has been bugging me and I tried to find out for myself.
Here's the example:

So the 1-dimension in that 2-dimentional array is infact an array of pointers.

If what I said was incorrect, could you let me know what that was?

Thanks
una

1 Like