why does this port readout goes wrong over/after a certain amount of time?

Hi Guy's,

I have a strange phenomenon in my small testing-what-does-what code.

I Read out the port 3 and print that value on the serial monitor.
At first, the readouts are correct. But then sometimes there are errors in the readout.
So I scan the port, while not pushing the connected button and the outcome should always be 1.
But for some strange reason, the readout changes suddenly to 0, it keeps that for about 210(?) sessions, and then it changes back to 1 again, also for about 210 loops...
Also... changing the state of the button has no longer effect on the course of the program.

I have a piece of code integrated to make the lines no longer than 100, to manage the view somewhat.

What makes the program that it is correct over the first about 210 loops, but afterward it suddenly gets unmanageable.

int i (0);

void setup() 
{
  Serial.begin (9600);

}

void loop() 
{
 
   Serial.print (digitalRead (3));
   delay (100);
   i++;
   if (i == 50) 
    {Serial.println ();
     i = 0;
    }  
 
}

How is the switch wired? If you do not have a pull-up or pull down resistor to force the open input to a known value the input "floats" and it's state in indeterminate.

Your switch wiring should be similar to one of these examples S1 S2 or S3:

FTMZ:
it suddenly gets unmanageable.

What exactly does that mean?

This looks strange, but I am not a C++ expert.

int i (0);

I would write it like this

int i = 0;

...R

OK, indeed floating seemed to be the issue.
But I wonder how that can happen after about 150-300 (Not exactly, of even nearby 210 :wink: )sequences.

Uncontrollable... With that, I meant that pushing the button did not do anything to change the status on the serial monitor.

You still have not explained how the button is wired.