/////////////////////SEMAPHORE///////////////////// #include constexpr int semaphoreled[] {led1_pin, led2_pin}; constexpr int semaphoreled_count = sizeof(semaphoreled) / sizeof(semaphoreled[0]); constexpr unsigned long delay_param = 1000; unsigned long last_time; bool sviti; void initLeds(const int leds[], int count) { for (int i = 0; i < count; ++i) { pinMode(leds[i], OUTPUT); digitalWrite(leds[i], OFF); } } void switchLedsState(const int leds[], int count) { sviti = !sviti; for (int i = 0; i < count; ++i) { digitalWrite(leds[i], sviti ? ON : OFF); } } void setup() { // put your setup code here, to run once: initLeds(semaphoreled, semaphoreled_count); last_time = 0; sviti = false; } void loop() { // put your main code here, to run repeatedly: unsigned long current_time = millis(); if (current_time >= last_time + delay_param) { last_time = current_time; switchLedsState(semaphoreled, semaphoreled_count); } } /////////////////////PWM-example///////////////////// #include void setup() { // put your setup code here, to run once: pinMode(led1_pin, OUTPUT); pinMode(led2_pin, OUTPUT); } //intensity je desatinne cislo (0,1), ktore urcuje kolko percent z 2000 MIKROSEKUND (nie MILISEKUND) //ma byt led zasvietene. zvysok je zhasnute void lightWithIntensity(int led, float intensity) { auto current_time = micros(); const int freq = 2000; current_time %= freq; if (current_time < freq * intensity) { digitalWrite(led, ON); } else if (current_time >= freq * intensity) { digitalWrite(led, OFF); } } float intensity = 0; bool increasing = true; void update_intensity() { if (increasing) { intensity += 0.01; if (intensity >= 1) increasing = false; } else { intensity -= 0.01; if (intensity <= 0) increasing = true; } } //kazdych 10ms menim intenzitu unsigned long last_time = 0; void do_delay(int delay_time) { unsigned long current_time = millis(); if (current_time >= last_time + delay_time) { last_time = current_time; update_intensity(); } } void loop() { // put your main code here, to run repeatedly: do_delay(10); lightWithIntensity(led2_pin, intensity); }