Strange serial.Print behavior

Hello to all of you,

I am totally new to the Arduino and trying to play a little bit around with the setup discribed in this tutorial:

All the Code Examples are working and so I added a Reed-Switch with a pulldown and started to write some code by myself.

The Switch should mesure the rotationspeed and the LEDs should light up for the quarter of one cycle.

As long as I leave the serial.Print Command at its place, it kind of works, but when I comment it out, nothing works anymore.

I whould be happy, if someone can explain it. Here is the Code:

// Constants

// Pin connected to Reedswitch
const int buttonPin = 7;
// Pin connected to single LED
const int ledPin = 13;
//Pin connected to ST_CP of 74HC595
const int latchPin = 8;
//Pin connected to SH_CP of 74HC595
const int clockPin = 12;
////Pin connected to DS of 74HC595
const int dataPin = 11;

// Variables

int buttonState = 0;
// Time for one Cycle
unsigned long cycleTime;
// Time at the Switch
unsigned long timeAtSwitch = 0;
// Age of the cycle
unsigned long age = 0;

// Byte for Shiftregister
int numberToDisplay = 0;


boolean nextRound;

void setup() {
  pinMode (ledPin, OUTPUT);
  pinMode (buttonPin, INPUT);
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  // enable serial Port
  Serial.begin(9600);
}

void loop() {
  
  
  // take the latchPin low
  digitalWrite(latchPin, LOW);
  // shift out the bits
  shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
  //take the latch pin high so the LEDs will light up
  digitalWrite(latchPin, HIGH);
  
  Serial.println(numberToDisplay);
  numberToDisplay = 0;

  
  // read the Reed Switch
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH && nextRound == true) {
    digitalWrite(ledPin, HIGH);
    cycleTime = millis() - timeAtSwitch;
    timeAtSwitch = millis();
    // Serial.println(cycleTime);
    nextRound = false;
  }
  if (buttonState == LOW) {
    digitalWrite(ledPin, LOW);
    nextRound = true;
  }
  
  // age of cycle
  age = millis() - timeAtSwitch;
  if (age < cycleTime/4) {
    Serial.println(age);
    // all LEDs on
    numberToDisplay = 255;
  }
}

What I suspect is happening is that the Serial.println call is introducing a delay into your program, and your program doesn't work without that delay. What happens if you substitute a delay(N) call for the Serial.println, for some smallish value of N e.g. 1 to 10?

There seems to be no attempt to debounce the switch in your code. This means that a single push of the button will probably appear as a series of very fast pushes, and cycleTime will end up being set to the interval between the last two contact bounces, not the last two pushes.

BTW the more usual way to wire a switch is between ground and a digital pin, rather than between Vcc and a digital pin. This has the advantage that you can dispense with the external pulldown resistor and turn on the internal pullup instead, saving a resistor.

Thanks a lot!

By debouncing the button, there was no need for the delay anymore.