void loop() {
if (millis() - lastTime >= TIME_STEP) {
// extensive computation that
// takes a few miliseconds
lastTime = millis();
}
}
displayNumber();
displayNumber(counter);
See https://www.ksi.mff.cuni.cz/teaching/nswi170-web/pages/labs/coding/5-encapsulation
Logical parts of the application must be grouped together (e.g., using structures). Communication between parts must be conducted solely through a well-defined interface (functions/methods).
void setup() {
incrementButton.setup();
display.setup();
}
void loop() {
incrementButton.handle();
display.handle();
if (incrementButton.wasPressed()) {
coutnter++;
display.updateValue(counter);
}
}
You don't know how button or display works. You don't care. There is an easy interface to check whether the button was pressed (whatever that means) and to display a specific number.
void setup() {
musicPlayer.setup();
counter.setup();
}
void loop() {
// Plays sound
musicPlayer.handle();
// Handles buttons and the counter
counter.handle();
}
musicPlayer and counter are completely independent of each other. You only choose what will be executed. counter.handle() handles the button and the display internally.
void setup() {
musicPlayer.setup();
counter.setup();
incrementButton.setup();
display.setup();
}
void loop() {
// Plays sound
musicPlayer.handle();
// Handles buttons and the counter
incrementButton.handle();
display.handle();
if (incrementButton.wasPressed()) {
counter.incrementValue();
display.updateValue(counter.getValue());
}
}
counter class only handles the counting, the rest is done externally