trouble reading out two opto sensors at once

Hello, I’m having difficulties reading out two opto sensors, which only give a 0 and 1 as output.

My code

//for printf
#include <stdio.h>

const int S0Distance = 8; //pin attached to sensor that measures one complete turn 
const int S1Distance = 11; //pin attached to sensor that measures one complete turn 

//for printf
int my_putc( char c, FILE *t) {
  Serial.write( c );
} 

void setup()
{	
	pinMode(S0Distance,INPUT);
	pinMode(S1Distance,INPUT);
	       
        Serial.begin(9600);

        fdevopen( &my_putc, 0); 
        printf("test %i",10);
}

void loop()
{
    bool done = false; 
    while (!done) 
    {
                ....
          	if (digitalRead(S0Distance) == HIGH)
		{
		      printf("S0Distance high");
		}
 
                //a small delay didn't work out (1 to 10) 
	       //delay(1);
		
		if (digitalRead(S1Distance) == HIGH)
		{
			printf("S1Distance high");	
		}
    }
}

The difficulty is that I don’t get readings from both sensors at the same time. When I comment out one if statement the reading of the other sensor appear perfectly, but when using both if statements I only get readings from the first sensor (S0).

I’m sure I’m missing something here, since I don’t expect that reading out 2 bytes at the same time would be too much for a arduino. All help and suggestions are welcome.

Could your sensors have Open Collector outputs? That would require a pull-up resistor.

Try enabling the internal pull-ups:

void setup()
    {
	pinMode(S0Distance,INPUT);
	digitalWrite(S0Distance,HIGH);
	pinMode(S1Distance,INPUT);
	digitalWrite(S1Distance,HIGH);

How long it takes for “printf” execution inside if statement?
Is it too long, as you try to access external device?
I’d take a timestamp inside if, and use “printf” out of the while, at the end of code.

Thanks for your help both. It turned out that one of the sensors had a bad connection and removing the printf made it more responsive. It works now as expected.

Writing HIGH on the INPUT didn't make any difference by the way.

About the logging, is Serial.print faster then using printf?

About the logging, is Serial.print faster then using printf?

At 9600? I doubt you'd notice any difference.