Four If statements in loop(), last one not executing - SOLVED !

Not new to Arduino but this has me stumped !
The last if statement (if (SCALE_Z_ENABLED == 1) { in the loop() will not execute.

When I move the code to be the 1st, 2nd or 3rd If statement then it executes, but then the 4th one will not.
if (SCALE_Z_ENABLED == 1) is true so I cannot understand what is going on, its a relativly simple loop().
Sorry for posting the whole code but thought it would make it easier to understand.

[code]
/*
    Name:       TouchDROInterface.ino
    Created:	  17-06-2023
    Author:     Mike Welling
    Revisions:
    19-06-2023  Added detection of valid axis Data to AutoEnable individual axis

  ###################################################################################################################
  #
  # Chinese Capacitive Scales Protocol:
  # 2540 Count per Inch, 24 bit word (6 x 4bit), last nibble only used for sign (bit 21)
  # X, Y, Z & W Axis:
  # X is Left-Right movement of Lathe Tool  X
  # Y is Cross-Slide movement               Y
  # Z is Compound movement                  Xc
  # W is TailStock movement                 Ts
  # The Serial Protocol used by TouchDRO uses plain ASCII (char).
  # The format is Axis + Absolute position in Counts Per Inch(float) and ended with a terminating character ";"
  # Example:  X1234567;Y2345678;Z3456789;
  # various bits of code pinched from other sources - thanks to all of you
  ###################################################################################################################
*/

// Scale Enabled function set automaticaly on detection of valid Data pulses
int SCALE_X_ENABLED = 0 ;           // all axis set to Disabled initially
int SCALE_Y_ENABLED = 0 ;
int SCALE_Z_ENABLED = 0 ;
int SCALE_W_ENABLED = 0 ;

#define SCALE_X_CLK_PIN 2         // X Axis Clock pin
#define SCALE_X_DATA_PIN 3        // X Axis Data pin
#define SCALE_Y_CLK_PIN	5			    // Y Axis Clock pin
#define SCALE_Y_DATA_PIN 4		    // Y Axis Data pin
#define SCALE_Z_CLK_PIN	6			    // Z Axis Clock pin
#define SCALE_Z_DATA_PIN 7		    // Z Axis Data pin
#define SCALE_W_CLK_PIN	8			    // W Axis Clock pin
#define SCALE_W_DATA_PIN 9		    // W Axis Data pin

#define ENABLEDX_LED 10           // LED indicating X Axis Enabled, scale plugged in & outputting clock pulses
#define ENABLEDY_LED 11           // LED indicating Y Axis Enabled, scale plugged in & outputting clock pulses
#define ENABLEDZ_LED 12           // LED indicating Z Axis Enabled, scale plugged in & outputting clock pulses
#define ENABLEDW_LED 13           // LED indicating W Axis Enabled, scale plugged in & outputting clock pulses

float X_Readout = 40.0;           // test data only, overwritten later in code
float Y_Readout = 30.0;
float Z_Readout = 20.0;
float W_Readout = 10.0;

int bit_arrayX[24];           // For storing the X Axis data - bit_array[1] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB)
int bit_arrayY[24];           // For storing the Y Axis data - bit_array[1] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB)
int bit_arrayZ[24];           // For storing the Z Axis data - bit_array[1] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB)
int bit_arrayW[24];           // For storing the W Axis data - bit_array[1] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB)
unsigned long time_now;       // For storing the time when the clock signal is changed from HIGH to LOW (falling edge trigger of data output).

void setup() {
  Serial.begin(115200);                 // set 115200 baud rate to HC-06 BT module
  pinMode(SCALE_X_CLK_PIN, INPUT);
  pinMode(SCALE_X_DATA_PIN, INPUT);
  pinMode(SCALE_Y_CLK_PIN, INPUT);
  pinMode(SCALE_Y_DATA_PIN, INPUT);
  pinMode(SCALE_Z_CLK_PIN, INPUT);
  pinMode(SCALE_Z_DATA_PIN, INPUT);
  pinMode(SCALE_W_CLK_PIN, INPUT);
  pinMode(SCALE_W_DATA_PIN, INPUT);

  pinMode(ENABLEDX_LED, OUTPUT);
  pinMode(ENABLEDY_LED, OUTPUT);
  pinMode(ENABLEDZ_LED, OUTPUT);
  pinMode(ENABLEDW_LED, OUTPUT);

  digitalWrite(ENABLEDX_LED, LOW);      // Turn OFF ALL Enabled LED Indications
  digitalWrite(ENABLEDY_LED, LOW);
  digitalWrite(ENABLEDZ_LED, LOW);
  digitalWrite(ENABLEDW_LED, LOW);

  // Auto Detect which Axis are plugged in & have a valid Clock signal, to enable each Axis individually, turn on Axis LED when Enabled.
  delay(500);
  long pulseDurationX = pulseIn(SCALE_X_CLK_PIN, LOW);
  long pulseDurationY = pulseIn(SCALE_Y_CLK_PIN, LOW);
  long pulseDurationZ = pulseIn(SCALE_Z_CLK_PIN, LOW);
  long pulseDurationW = pulseIn(SCALE_W_CLK_PIN, LOW);

  if (pulseDurationX > 50)SCALE_X_ENABLED = 1, digitalWrite(ENABLEDX_LED, HIGH);
  if (pulseDurationY > 50)SCALE_Y_ENABLED = 1, digitalWrite(ENABLEDY_LED, HIGH);
  if (pulseDurationZ > 50)SCALE_Z_ENABLED = 1, digitalWrite(ENABLEDZ_LED, HIGH);
  if (pulseDurationW > 50)SCALE_W_ENABLED = 1, digitalWrite(ENABLEDW_LED, HIGH);

  /*
    // Test data whilst establishing Auto Detect of Axis Enable
    Serial.print("X Pulse Duration: ");
    Serial.println(pulseDurationX);
    Serial.print("Y Pulse Duration: ");
    Serial.println(pulseDurationY);
    Serial.print("Z Pulse Duration: ");
    Serial.println(pulseDurationZ);
    Serial.print("W Pulse Duration: ");
    Serial.println(pulseDurationW);

    Serial.println("Scales Enabled: ");
    Serial.print("X = ");
    Serial.println(SCALE_X_ENABLED);
    Serial.print("Y = ");
    Serial.println(SCALE_Y_ENABLED);
    Serial.print("Z = ");
    Serial.println(SCALE_Z_ENABLED);
    Serial.print("W = ");
    Serial.println(SCALE_W_ENABLED);
    Serial.println();
  */

}


void loop() {

  if (SCALE_X_ENABLED == 1) {
    while (digitalRead(SCALE_X_CLK_PIN) == LOW) {}      // If clock is LOW wait until it turns to HIGH
    time_now = micros();                                // When input goes HIGH set time_now
    while (digitalRead(SCALE_X_CLK_PIN) == HIGH) {}     // Wait for the end of the HIGH pulse
    if ((micros() - time_now) > 1000) {                 // If the HIGH pulse was longer than 1000 micros we are at the start of a new bit sequence
      decodeX();                                        // decode the X Axis bit sequence
    }
  }

  if (SCALE_Y_ENABLED == 1) {
    while (digitalRead(SCALE_Y_CLK_PIN) == LOW) {}      // If clock is LOW wait until it turns to HIGH
    time_now = micros();
    while (digitalRead(SCALE_Y_CLK_PIN) == HIGH) {}     // Wait for the end of the HIGH pulse
    if ((micros() - time_now) > 1000) {                 // If the HIGH pulse was longer than 1000 micros we are at the start of a new bit sequence
      decodeY();                                        // decode the Y Axis bit sequence
    }
  }

  if (SCALE_W_ENABLED == 1) {
    while (digitalRead(SCALE_W_CLK_PIN) == LOW) {}      // If clock is LOW wait until it turns to HIGH
    time_now = micros();
    while (digitalRead(SCALE_W_CLK_PIN) == HIGH) {}     // Wait for the end of the HIGH pulse
    if ((micros() - time_now) > 1000) {                 // If the HIGH pulse was longer than 1000 micros we are at the start of a new bit sequence
      decodeW();                                        // decode the W Axis bit sequence
    }
  }

  if (SCALE_Z_ENABLED == 1) {
    while (digitalRead(SCALE_Z_CLK_PIN) == LOW) {}      // If clock is LOW wait until it turns to HIGH
    time_now = micros();
    while (digitalRead(SCALE_Z_CLK_PIN) == HIGH) {}     // Wait for the end of the HIGH pulse
    if ((micros() - time_now) > 1000) {                 // If the HIGH pulse was longer than 1000 micros we are at the start of a new bit sequence
      decodeZ();                                        // decode the Z Axis bit sequence
    }
  }

  Serial.println("End of Loop");                          // Test only !!
  delay(5);
}


void decodeX() {
  // reset variables
  int sign = 1;
  int i = 0;
  float result = 0.0;

  bit_arrayX[i] = digitalRead(SCALE_X_DATA_PIN);          // Store the 1st bit (start bit) which is always 1.
  while (digitalRead(SCALE_X_CLK_PIN) == HIGH) {};
  for (i = 1; i <= 24; i++) {
    while (digitalRead(SCALE_X_CLK_PIN) == LOW) {}        // Wait until clock returns to HIGH
    bit_arrayX[i] = digitalRead(SCALE_X_DATA_PIN);
    while (digitalRead(SCALE_X_CLK_PIN) == HIGH) {}       // Wait until clock returns to LOW
  }
  for (i = 1; i <= 20; i++) {                             // Turning the result in the bit array from binary to decimal, i=1 skip LSB
    result = result + (pow(2, i - 1) * bit_arrayX[i]);
  }
  if (bit_arrayX[21] == 0) sign = -1;                     // Bit 21 is the sign bit. 0 = -ve, 1 = +ve - *** CHANGE THIS TO SUIT ORIENTATION OF SCALE ON LATHE ***
  X_Readout = (result * sign) / 100.00;
  Serial.println("X" + String(X_Readout) + ";");          // Output X Axis data to TouchDRO
  /*
    Serial.println("X Axis Data Stream");
    for (i = 0; i <= 24; i++) {                             // Show the content of the bit array. This is for verification only.
      Serial.print(bit_arrayX[i]);
      Serial.print(" ");
    }
    Serial.println();
    Serial.print("X Axis Distance = ");
    Serial.print(X_Readout, 2);                             // Print result with 2 decimals
    Serial.print(" mm, ");
    Serial.print((X_Readout / 25.4), 3);                    // display result in Inches to 3 decimal places
    Serial.println(" \"");
  */
}

void decodeY() {
  // reset variables
  int sign = 1;
  int i = 0;
  float result = 0.0;

  bit_arrayY[i] = digitalRead(SCALE_Y_DATA_PIN);          // Store the 1st bit (start bit) which is always 1.
  while (digitalRead(SCALE_Y_CLK_PIN) == HIGH) {};
  for (i = 1; i <= 24; i++) {
    while (digitalRead(SCALE_Y_CLK_PIN) == LOW) {}        // Wait until clock returns to HIGH
    bit_arrayY[i] = digitalRead(SCALE_Y_DATA_PIN);
    while (digitalRead(SCALE_Y_CLK_PIN) == HIGH) {}       // Wait until clock returns to LOW
  }
  for (i = 1; i <= 20; i++) {                             // Turning the result in the bit array from binary to decimal, i=1 skip LSB
    result = result + (pow(2, i - 1) * bit_arrayY[i]);
  }
  if (bit_arrayY[21] == 0) sign = -1;                     // Bit 21 is the sign bit. 0 = -ve, 1 = +ve - *** CHANGE THIS TO SUIT ORIENTATION OF SCALE ON LATHE ***
  Y_Readout = (result * sign) / 100.00;
  Serial.println("Y" + String(Y_Readout) + ";");          // Output Y Axis data to TouchDRO
  /*
    Serial.println("Y Axis Data Stream");
    for (i = 0; i <= 24; i++) {                             // Show the content of the bit array. This is for verification only.
      Serial.print(bit_arrayY[i]);
      Serial.print(" ");
    }
    Serial.println();
    Serial.print("Y Axis Distance = ");
    Serial.print(Y_Readout, 2);                             // Print result with 2 decimals
    Serial.print(" mm, ");
    Serial.print((Y_Readout / 25.4), 3);                    // display result in Inches to 3 decimal places
    Serial.println(" \"");
  */
}

void decodeZ() {
  // reset variables
  int sign = 1;
  int i = 0;
  float result = 0.0;

  bit_arrayZ[i] = digitalRead(SCALE_Z_DATA_PIN);          // Store the 1st bit (start bit) which is always 1.
  while (digitalRead(SCALE_Z_CLK_PIN) == HIGH) {};
  for (i = 1; i <= 24; i++) {
    while (digitalRead(SCALE_Z_CLK_PIN) == LOW) {}        // Wait until clock returns to HIGH
    bit_arrayZ[i] = digitalRead(SCALE_Z_DATA_PIN);
    while (digitalRead(SCALE_Z_CLK_PIN) == HIGH) {}       // Wait until clock returns to LOW
  }
  for (i = 1; i <= 20; i++) {                             // Turning the result in the bit array from binary to decimal, i=1 skip LSB
    result = result + (pow(2, i - 1) * bit_arrayZ[i]);
  }
  if (bit_arrayZ[21] == 0) sign = -1;                     // Bit 21 is the sign bit. 0 = -ve, 1 = +ve - *** CHANGE THIS TO SUIT ORIENTATION OF SCALE ON LATHE ***
  Z_Readout = (result * sign) / 100.00;
  Serial.println("Z" + String(Z_Readout) + ";");          // Output Z Axis data to TouchDRO
  /*
    Serial.println("Z Axis Data Stream");
    for (i = 0; i <= 24; i++) {                             // Show the content of the bit array. This is for verification only.
      Serial.print(bit_arrayZ[i]);
      Serial.print(" ");
    }
    Serial.println();
    Serial.print("Z Axis Distance = ");
    Serial.print(Z_Readout, 2);                             // Print result with 2 decimals
    Serial.print(" mm, ");
    Serial.print((Z_Readout / 25.4), 3);                    // display result in Inches to 3 decimal places
    Serial.println(" \"");
  */
}

void decodeW() {
  // reset variables
  int sign = 1;
  int i = 0;
  float result = 0.0;

  bit_arrayW[i] = digitalRead(SCALE_W_DATA_PIN);          // Store the 1st bit (start bit) which is always 1.
  while (digitalRead(SCALE_W_CLK_PIN) == HIGH) {};
  for (i = 1; i <= 24; i++) {
    while (digitalRead(SCALE_W_CLK_PIN) == LOW) {}        // Wait until clock returns to HIGH
    bit_arrayW[i] = digitalRead(SCALE_W_DATA_PIN);
    while (digitalRead(SCALE_W_CLK_PIN) == HIGH) {}       // Wait until clock returns to LOW
  }
  for (i = 1; i <= 20; i++) {                             // Turning the result in the bit array from binary to decimal, i=1 skip LSB
    result = result + (pow(2, i - 1) * bit_arrayW[i]);
  }
  if (bit_arrayW[21] == 0) sign = -1;                     // Bit 21 is the sign bit. 0 = -ve, 1 = +ve - *** CHANGE THIS TO SUIT ORIENTATION OF SCALE ON LATHE ***
  W_Readout = (result * sign) / 100.00;
  Serial.println("W" + String(W_Readout) + ";");          // Output W Axis data to TouchDRO
  /*
    Serial.println("W Axis Data Stream");
    for (i = 0; i <= 24; i++) {                             // Show the content of the bit array. This is for verification only.
      Serial.print(bit_arrayW[i]);
      Serial.print(" ");
    }
    Serial.println();
    Serial.print("W Axis Distance = ");
    Serial.print(W_Readout, 2);                             // Print result with 2 decimals
    Serial.print(" mm, ");
    Serial.print((W_Readout / 25.4), 3);                    // display result in Inches to 3 decimal places
    Serial.println(" \"");
  */
}
[/code]

that's fine - allways post full compileable code.

To your problem:

add more Serial prints so you can see how far your sketch is getting processed and where it stops (for example: it could be stocked within a while...)

1 Like

Welcome

You have buffer overflows in your code. Arrays are zero-indexed, so the valid indexes of an array of size 24 are 0-23, not 1-24

guix

That solved the issue , many thanks !
Still don't understand why it only affected the 4th If statement but I've changed them all to 1-23 now.

Mike

Delta_G

Will try that, many thanks.

Mike

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