NSWI170, 2025, Labs 03
Jáchym Bártík
loop trvat více milisekund
millis() vrací: 1, 3, 4, 6, 9, 11, ...constexpr int PERIOD = 10;
void loop() {
int currentTime = millis();
if (currentTime - lastTime == PERIOD) { // Don't do this!
lastTime = currentTime;
doSomething();
}
} if (currentTime - lastTime >= PERIOD) { // Still not perfect
lastTime = currentTime;constexpr int PERIOD = 10;
void loop() {
int currentTime = millis();
if (currentTime - lastTime >= PERIOD) { // Do this instead
lastTime += PERIOD;
doSomething();
}
}& (and), | (or), ~ (negation)<<, >> (posun doleva, doprava)7 & 2 // 0111 & 0010 -> 0010 = 2
7 | 8 // 0111 | 1000 -> 1111 = 15
2 << 1 // 0010 -> 0100 = 4
3 << 2 // 0011 -> 1100 = 12void setup() {
pinMode(button1_pin, INPUT);
}ON, tlačítko je zapnutévoid loop() {
bool isPressed = digitalRead(button1_pin) == ON;
}#include <funshield.h>
void setup() {
pinMode(led1_pin, OUTPUT);
pinMode(button1_pin, INPUT);
}
void loop() {
digitalWrite(led1_pin, digitalRead(button1_pin));
}loop bylo tlačítko stisknuto, tj. kdy digitalRead poprvé vrátila false
x = (x + 15) % 16struct Button {
int pin;
bool isPressed;
};
void processButton(Button &button) {
if (digitalRead(button.pin) == ON && !button.isPressed)
... // Handle the click event
}
Button buttons[3];
processButton(buttons[0]);public vs. private
public
private
class Button { // This class does exactly the same as the previous structure
public:
int pin;
bool isPressed;
};