Strange serial output [rather :forgot "volatile"] [answered]

hello
here i have a machine that sends some data to a display thru a c-bus configuration (1 wire clock and 1 wire data). Each transmition is 22 or 23bits and must finish within 900usec, that is a period not more than about 40usec on clock. Iam trying to get the duration of the high and low, and finally have the following code :

boolean Rising, Falling = false;
byte x, y, z, CounterR, CounterF = 0;
byte pin2 = 2;
byte pin3 = 3;
unsigned long ArrayR[55], ArrayF[55];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("test clock, data using I/R");
  for (x = 0; x < 55; x++) {
    ArrayR[x], ArrayF[x] = 0;
  }

  attachInterrupt(digitalPinToInterrupt(pin2), ISRF, FALLING);
  attachInterrupt(digitalPinToInterrupt(pin3), ISRR, RISING);
}

void ISRF(void) {
  Falling = true;
}
void ISRR(void) {
  Rising = true;
}

void loop() {
  // put your main code here, to run repeatedly:
  CounterR = 1;
  CounterF = 1;
  Rising = false;
  Falling = false;
  ArrayF[0], ArrayR[0] = micros();

  while (1) {
    if  (Falling == true)  {
      ArrayF[CounterF] = micros();
      CounterF++;
      Falling = false;
    }
   
    if (Rising == true) {
      if (CounterF != 1) {
        ArrayR[CounterR] = micros();
        CounterR++;
      }
      Rising = false;
    }
    if (CounterF >= 52) {
      for (x = 1; x < 54; x++) {
        Serial.print(x);
        Serial.print("   PER: ");
        Serial.print(ArrayF[x] - ArrayF[x - 1]);
        Serial.print("   HIGH: ");
        Serial.print(ArrayF[x] - ArrayR[x - 1]);
        Serial.print("   LOW: ");
        Serial.println(ArrayR[x] - ArrayF[x]);
      }
      exit;
    }
    if (x > 50) exit;
  }
}

in brief, I use 2 arrays and store the micros() rising and falling edges for 53 periods so recording 2 cycles. There is inactive time between "sets" of 23 bits.
The problem is that on pulse 11 and 40, every recording gives a strange value 4294967292, the first number (4) suppose is correct, the rest? (dont mind for other "inaccuracies" ). Dont have access to the machine any more.
here is the printout

1   PER: 890584   HIGH: 890280   LOW: 4
2   PER: 28   HIGH: 24   LOW: 8
3   PER: 32   HIGH: 24   LOW: 4
4   PER: 24   HIGH: 20   LOW: 12
5   PER: 32   HIGH: 20   LOW: 12
6   PER: 32   HIGH: 20   LOW: 8
7   PER: 36   HIGH: 28   LOW: 4
8   PER: 32   HIGH: 28   LOW: 4
9   PER: 32   HIGH: 28   LOW: 4
10   PER: 28   HIGH: 24   LOW: 4
11   PER: 40   HIGH: 36   LOW: 4294967292
12   PER: 24   HIGH: 28   LOW: 4
13   PER: 32   HIGH: 28   LOW: 4
14   PER: 28   HIGH: 24   LOW: 8
15   PER: 28   HIGH: 20   LOW: 8
16   PER: 28   HIGH: 20   LOW: 12
17   PER: 32   HIGH: 20   LOW: 12
18   PER: 36   HIGH: 24   LOW: 8
19   PER: 32   HIGH: 24   LOW: 4
20   PER: 32   HIGH: 28   LOW: 4
21   PER: 32   HIGH: 28   LOW: 4
22   PER: 24   HIGH: 20   LOW: 8
23   PER: 1116   HIGH: 1108   LOW: 8
24   PER: 1128   HIGH: 1120   LOW: 4
25   PER: 24   HIGH: 20   LOW: 8
26   PER: 32   HIGH: 24   LOW: 8
27   PER: 28   HIGH: 20   LOW: 12
28   PER: 40   HIGH: 28   LOW: 4
29   PER: 28   HIGH: 24   LOW: 8
30   PER: 32   HIGH: 24   LOW: 4
31   PER: 32   HIGH: 28   LOW: 4
32   PER: 24   HIGH: 20   LOW: 12
33   PER: 32   HIGH: 20   LOW: 8
34   PER: 32   HIGH: 24   LOW: 8
35   PER: 36   HIGH: 28   LOW: 4
36   PER: 32   HIGH: 28   LOW: 4
37   PER: 28   HIGH: 24   LOW: 8
38   PER: 32   HIGH: 24   LOW: 4
39   PER: 24   HIGH: 20   LOW: 12
40   PER: 44   HIGH: 32   LOW: 4294967292
41   PER: 24   HIGH: 28   LOW: 8
42   PER: 32   HIGH: 24   LOW: 4
43   PER: 24   HIGH: 20   LOW: 12
44   PER: 32   HIGH: 20   LOW: 12
45   PER: 32   HIGH: 20   LOW: 8
46   PER: 1120   HIGH: 1112   LOW: 4
47   PER: 1321784   HIGH: 1321780   LOW: 12
48   PER: 32   HIGH: 20   LOW: 8
49   PER: 28   HIGH: 20   LOW: 12
50   PER: 40   HIGH: 28   LOW: 4
51   PER: 28   HIGH: 24   LOW: 8
52   PER: 153180   HIGH: 153172   LOW: 4
53   PER: 4292596968   HIGH: 4292596964   LOW: 0
1   PER: 890584   HIGH: 890280   LOW: 4
2   PER: 28   HIGH: 24   LOW: 8
3   PER: 32   HIGH: 24   LOW: 4
4   PER: 24   HIGH: 20   LOW: 12
5   PER: 32   HIGH: 20   LOW: 12
6   PER: 32   HIGH: 20   LOW: 8
7   PER: 36   HIGH: 28   LOW: 4
8   PER: 32   HIGH: 28   LOW: 4
9   PER: 32   HIGH: 28   LOW: 4
10   PER: 28   HIGH: 24   LOW: 4
11   PER: 40   HIGH: 36   LOW: 4294967292
12   PER: 24   HIGH: 28   LOW: 4
13   PER: 32   HIGH: 28   LOW: 4
14   PER: 28   HIGH: 24   LOW: 8
15   PER: 28   HIGH: 20   LOW: 8
16   PER: 28   HIGH: 20   LOW: 12
17   PER: 32   HIGH: 20   LOW: 12
18   PER: 36   HIGH: 24   LOW: 8
19   PER: 32   HIGH: 24   LOW: 4
20   PER: 32   HIGH: 28   LOW: 4
21   PER: 32   HIGH: 28   LOW: 4
22   PER: 24   HIGH: 20   LOW: 8
23   PER: 1116   HIGH: 1108   LOW: 8
24   PER: 1128   HIGH: 1120   LOW: 4
25   PER: 24   HIGH: 20   LOW: 8
26   PER: 32   HIGH: 24   LOW: 8
27   PER: 28   HIGH: 20   LOW: 12
28   PER: 40   HIGH: 28   LOW: 4
29   PER: 28   HIGH: 24   LOW: 8
30   PER: 32   HIGH: 24   LOW: 4
31   PER: 32   HIGH: 28   LOW: 4
32   PER: 24   HIGH: 20   LOW: 12
33   PER: 32   HIGH: 20   LOW: 8
34   PER: 32   HIGH: 24   LOW: 8
35   PER: 36   HIGH: 28   LOW: 4
36   PER: 32   HIGH: 28   LOW: 4
37   PER: 28   HIGH: 24   LOW: 8
38   PER: 32   HIGH: 24   LOW: 4
39   PER: 24   HIGH: 20   LOW: 12
40   PER: 44   HIGH: 32   LOW: 4294967292
41   PER: 24   HIGH: 28   LOW: 8

Should you turn interrupts off while setting the flags... or does this interfere with the loop?

Interrupts are turned off inside an ISR and are automatically turned on when the ISR exits, so the above is not necessary or desirable.

More importantly, variables shared with interrupt routines must be declared volatile:

volatile boolean Rising, Falling = false;

The following interesting result comes about if you subtract an unsigned long integer from a smaller unsigned long integer. If both longs were signed, that odd-looking LOW value would be a negative number.

40   PER: 44   HIGH: 32   LOW: 4294967292

Did you hear yhat strange "goup goup" sound? it was myhead knocking on the desk. Forgot volatile !!!
So, i thought the first digit (4) was the pulsewidth and the the rest was serial garbage!!! Now it is clear it is the full scale of an unsigned long. So, it seems that positions 11 and 40 on one array are never recorded keeping the initial "0".
But, why always 11 and 40 in all (about 11000 !) recorded loops . Even if one was recorded this would change the sequense (rather rhetorical question).
Thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.