Attempts to break a while loop while reading clock and data pins

Good day

I have been using a Latte Panda micro PC with an Arduino Lenoardo onboard to read data from measurement instruments.

The data from the instruments is read by watching a clock pin and reading the state of the data pin on the falling edge of the clock.

The code I have for this works fine but relies on the clock pulses to advanvce the code. If there is no device connected, or it is switched off, then the code gets stuck waiting for the clock.

I have attempted to use a Millis() timer to break the while loop but it does not work, the code gets stuck and crashes completely if I send any more serial commands.

I am still relatively new to Arduino and I have mostly used it for this purpose. I would not be surprised if it is just a

      for ( l = 0; l < 13; l++) {
         n = 0;

         for (m = 0; m < 4; m++) {

            timex = millis();

            while (digitalRead(clk) == LOW) {

                if ((millis() - timex > 3000) {
                   break;
                }

            while (digitalRead(clk) == HIGH) {
            }

         bitWrite (n, m, (digitalRead(dat) & 0x1));
         }

     mydata[l] = n;

     }

This is just a snippit of the project, I can post the whole thing if necessary.

Post the whole code not just a snippet. Or at least a complete and compilable example that illustrates the problem.

If nothing is connected then the line will be floating and the while high loop might also be stuck.

Hello

Ok here is the whole of the code.

This is a little different to the snippet, this time I tried something I found online but I still can’t get it to work. I have also moved ‘timex = millis()’ to outside the for loops so it doesn’t reset every time.

int req = 10; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 21; //mic Data line goes to pin 2
int clk = 12; //mic Clock line goes to pin 3
int inhibit = 13; //***Multiplex inhibit
int binA = 22; //*****Binary channel select
int binB = 23;

String Dec;
String Number;
String OutputStr;
String command;
unsigned long timex;

int e = 0;
int f = 0;
int g = 0;

int l = 0;
int m = 0;
int n = 0;
int signCh = 8;
int sign = 0;
int decimal;
float dpp;
byte mydata[14];
String value_str;
int value_int;
float value;


void setup() {
  
  pinMode(req, OUTPUT);
  pinMode(inhibit, OUTPUT);
  pinMode(binA, OUTPUT);
  pinMode(binB, OUTPUT);
  pinMode(clk, INPUT_PULLUP);
  pinMode(dat, INPUT_PULLUP);
 
  Serial.begin(9600);
  command = "";
}



void loop() {

  while (!Serial);
  delay(3);
//  e = 0;
  command = "";
  
  if (Serial.available() > 0) {
    command = Serial.readString();
    command.trim();
    e = command.toInt();
    delay(2);
  }
  
  if ((e > 0) && (e < 5)){
    readDevice();
  }
  

}


void readDevice() {

OutputStr == "";

    digitalWrite(req, LOW);
    digitalWrite(inhibit, HIGH);

    if (e == 4) {
      digitalWrite(binA, LOW);
      digitalWrite(binB, LOW);
//      OutputStr.concat("D: ");
    }
    
    if (e == 3) {
      digitalWrite(binA, HIGH);
      digitalWrite(binB, LOW);
//      OutputStr.concat("C: ");
    }
    
    if (e == 2) {
      digitalWrite(binA, LOW);
      digitalWrite(binB, HIGH);
//      OutputStr.concat("B: ");
    }
    
    if (e == 1) {
      digitalWrite(binA, HIGH);
      digitalWrite(binB, HIGH);
    }
    
    delay(2);
    digitalWrite(inhibit, LOW);
    delay(2);

    digitalWrite(req, HIGH);  // generate set request
    timex = millis();
    
      for ( l = 0; l < 13; l++ ) {
        n = 0;
        for (m = 0; m < 4; m++) {
        
        

          while (digitalRead(clk) == LOW || millis() - timex < 3000) {            
          } // hold until clock is high
          
          while ( digitalRead(clk) == HIGH) {
          } // hold until clock is low

        bitWrite(n, m, (digitalRead(dat) & 0x1));
        }
      mydata[l] = n;
      
      }
//           timex = millis();
//           if ((millis() - timex) > 30000);{
//            break; }


    sign = mydata[4];
    value_str = String(mydata[5]) + String(mydata[6]) + String(mydata[7]) + String(mydata[8] + String(mydata[9] + String(mydata[10]))) ;
    decimal = mydata[11];
    units = mydata[12];

    value_int = value_str.toInt();
    if (decimal == 0) dpp = 1.0;
    if (decimal == 1) dpp = 10.0;
    if (decimal == 2) dpp = 100.0;
    if (decimal == 3) dpp = 1000.0;
    if (decimal == 4) dpp = 10000.0;
    if (decimal == 5) dpp = 100000.0;

    value = value_int / dpp;

    Number = String(value, decimal);

    if (sign == 0) {
      OutputStr.concat(Number);
      OutputStr.concat(" ");
    }
    
    if (sign == 8) {
      OutputStr.concat("-");
      OutputStr.concat(Number);
      OutputStr.concat(" ");
    }

    digitalWrite(req, LOW);

Serial.println(OutputStr);
OutputStr = "";
}

I should point out that the original code came from here:

Thanks for replying Delta_G. I hope you can help.

Andy

Hello

Ok Delta G provided a huge clue as to the solution here.

Initially I placed Serial.print commands into the code to establish where it was waiting for the clock pin. It was sat waiting for the pin to go high. Delta_G commented that without a device connected the pin may be floating around.

I now have the timeout in both loops and it is working a treat.

Thank you