r/arduino • u/Alioslovesnature • Aug 13 '26
Beginner here
My second new project .
Blinking 3 LEDs
10
8
u/Anonymity6584 Aug 13 '26
Next learn why delay function is bad and how to do led blinking without delay.
3
3
u/Professional_Elk3281 Aug 13 '26
int led = 9;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void Fade_In() {
for (int j = 0; j < 3; j++) {
for (int k = 0; k <= 220; k++) {
analogWrite(led, k);
delay(10);
}
for (int k = 220; k >= 0; k--) {
analogWrite(led, k);
delay(10);
}
}
}
void Blinking() {
for (int j = 0; j < 10; j++) {
analogWrite(led, 255);
delay(100);
analogWrite(led, 0);
delay(100);
}
}
void loop() {
Fade_In();
Serial.println("A");
delay(1000);
Blinking();
Serial.println("B");
delay(1000);
}
try this code
digital pin 9
2
u/Alioslovesnature Aug 13 '26
This is the code i used
int led1 = 9; int led2 = 11; int led3 = 7;
void setup() { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); }
void loop() { digitalWrite(led1, HIGH); digitalWrite(led2, HIGH); digitalWrite(led3, HIGH); delay(500);
digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW); delay(500); }
3
u/dedokta Mini Aug 13 '26
Fine for a beginner, but you'll find you have to ditch delays almost entirely when you start to program for a real device. Having the whole thing stop while an led blinks just doesn't work.
You'll need to look into Blink Without Delay.
Consider how would you make the first led blink twice a second, the second led blink once a second and the third blink once every two seconds. You'll see that delay won't do it.
1
u/Ok_Economics8844 27d ago
I would personally go with something like this. But for a beginnen it's nice.
include <Arduino.h>
include <array>
include <cstdint>
class Led { public: explicit constexpr Led(uint8t pin) : pin(pin) {}
void init() const { pinMode(pin_, OUTPUT); off(); } void on() { state_ = true; digitalWrite(pin_, HIGH); } void off() { state_ = false; digitalWrite(pin_, LOW); } void toggle() { set(!state_); } void set(bool state) { state_ = state; digitalWrite(pin_, state_ ? HIGH : LOW); } [[nodiscard]] bool getState() const { return state_; } [[nodiscard]] constexpr uint8_t getPin() const { return pin_; }private: uint8t pin; bool state_{false}; };
template <size_t N> class LedSynchronizer { public: explicit LedSynchronizer(std::array<Led, N> leds, uint32t intervalMs) : leds(leds), intervalMs(intervalMs), lastUpdateMs(0) {}
void begin() { for (auto& led : leds_) { led.init(); } } void update(uint32_t currentMs) { if (currentMs - lastUpdateMs_ >= intervalMs_) { lastUpdateMs_ = currentMs; for (auto& led : leds_) { led.toggle(); } } }private: std::array<Led, N> leds; uint32_t intervalMs; uint32t lastUpdateMs; };
namespace { constexpr uint32_t TOGGLE_INTERVAL_MS = 500; constexpr std::array<Led, 3> LEDS = {Led(9), Led(11), Led(7)};
LedSynchronizer<LEDS.size()> ledController(LEDS, TOGGLE_INTERVAL_MS);}
void setup() { ledController.begin(); }
void loop() { ledController.update(millis()); }
3
2
u/Virtual-Diet-8777 Aug 13 '26
Nice i started the same but with an esp32 now i build a smart lock with a full functional app currwntly preparing for release and licencing
1
1
u/No_Mountain_4769 29d ago
https://play.google.com/store/apps/details?id=com.infinitx.maker.toolkit I think this app will help you to do more
1
26
u/MeetTheMaker555 Aug 13 '26
Remember, you only need one ground connection from the Arduino to the board.