Hi,
As an assigment for school, I have to make a buzzer react to the amount of light that falls on an LDR.
There must also be a possibility for calibration. I have one button connected to a interrupt pin which triggers(upon LOW) the function 'calibrate'.
It works perfectly, but the messages on the Serial Monitor get chopped off.
When I press the button, the function is executed, and the first message should be entirely displayed.
Instead, the monitor only displays 'Block LDR from l'. Only to resume the rest of the first message upon the next button press, followed with only the partial the second message: 'Expose LDR to l'.
Upon the third press, the rest of the message is printed.
The function itself works, but it annoys me(and probably the teacher who'll grade me, too) that the messages aren't displayed properly.
For the sake of readability and simplicity, I have isolated the concerning function that causes the problem, but I'll provide the entire code if desired.
void calibrate(){
cli(); // Temporary interrupt disabling to prevent restarting calibration during calibration
Serial.println("Block LDR from light sources to set minimum value(0) by presssing the button.");
while(!digitalRead(calibPin))
; // wait for user to relieve button
while(digitalRead(calibPin))
; // wait for user input
calibMinValue = analogRead(ldrPin); // Save minimum value, ideally just above 0
Serial.println("Expose LDR to light source to set maximum value(100) by pressing the button.");
while(!digitalRead(calibPin))
; // wait for user to relieve button
while(digitalRead(calibPin))
; // wait for user input
calibMaxValue = analogRead(ldrPin); // Save maximum value, ideally just under 1024
ldrResolution = calibMaxValue - calibMinValue;
while(!digitalRead(calibPin))
; // Wait for user to relieve input
sei(); // Re-enable interrupts
}
I have properly debounced my button with an RC construction.(100nF+330Ohm, 10kOhm)
The button pulls the pin to the ground upon press.
My way of handling the button presses with WHILE waiting loops may not be the most professional or efficient way, but it should work following my logic.
So now for my question; Why are the messages on the Serial Monitor not finished before more code gets executed?
I'm curious whether other people experience the same problem with this code, so feel free to try out.
My bets are on the WHILE waiting loop constructions, or else the something with disabling the interrupts.
Thanks for the help champs.