1

I am trying to make a alarm and I have two blinking leds (red and blue). They work fine but I want two more that fade in and out.

I have a buzzer on it too.

I am very new to programming. By the way, the code below runs with no errors; just does not do what its intended to.

int led = 9;
int brightness = 0;
int fadeAmount = 5;

void setup() {
  pinMode(11,OUTPUT);
   pinMode(12,OUTPUT);
    pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(11, HIGH);
  delay(200);
  digitalWrite(11, LOW);
  delay(200);

  digitalWrite(12, HIGH);
  delay(200);
  digitalWrite(12, LOW);
  delay(200);

 digitalWrite(led, brightness);

 brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {
  fadeAmount = -fadeAmount;
}
 delay(30);
}
4
  • 2
    see the BlinkWithoutDelay example in IDE
    – Juraj
    Sep 18, 2018 at 11:52
  • ive done that i cant find anything wrong with my code
    – jace
    Sep 18, 2018 at 12:02
  • 1
    your code has delays
    – Juraj
    Sep 18, 2018 at 12:11
  • Delay = The execution of the program STOP during the delay time! So to continue to execute it is recommended to use 'millis();' as described in the BlinkWithoutDelay!
    – MatsK
    Sep 18, 2018 at 13:51

2 Answers 2

2

While there are better ways to do this, as pointed out by other posters, for what you're trying to learn right now, your program (almost!) works just fine. You'll need the more advanced techniques as you progress to more complex programs. But for now, I made a couple of edits to your program to get you going:

  • The most important one was to use analogWrite() for the PWM LED (LED 9). That makes it fade in and out instead of turn on and off (with a long period).
  • I turned one LED on and one off at the same time, getting rid of the dark periods and 400ms of combined delay.
  • I doubled the fadeAmount to fade in and out a little faster; it wasn't very visible with an increment of 5.

Here it is with those changes:

int led = 9;
int brightness = 0;
int fadeAmount = 10;

void setup() {
  pinMode(11,OUTPUT);
   pinMode(12,OUTPUT);
    pinMode(led, OUTPUT);
}

void loop() {
  digitalWrite(11, HIGH);
  digitalWrite(12, LOW);
  delay(200);
  digitalWrite(11, LOW);
  digitalWrite(12, HIGH);
  delay(200);

 analogWrite(led, brightness);

 brightness = brightness + fadeAmount;

if (brightness <= 0 || brightness >= 255) {
  fadeAmount = -fadeAmount;
}
 delay(30);

}

Update:

what if i wanted to add a a second led

I'm assuming you mean a fading LED, that it's pretty clear where you'd a blinking one?

Blinking in-phase is easy - write the same brightness to both. To fade them out of phase, you'll need a second brightness-variable (and possibly a second fade-amount), and an increment/decrement and limit test for each LED.

But note how your code starts getting more complex as you add components and try to manage them in a single loop, with delay()s? That's when you'll want to try the Blink Without Delay example, and compare adding new LEDs that way, to adding new LEDs to your current program.

3
  • so if i just delete my old program and copy this is in theory it should work or just change the other values
    – jace
    Sep 18, 2018 at 19:33
  • It did, on my board. Though, you might want to run my code in a new sketch and save yours to compare the two.
    – JRobert
    Sep 18, 2018 at 22:07
  • what if i wanted to add a a second led
    – jace
    Sep 18, 2018 at 23:21
0

You have 2 problems:

  1. You are using digitalWrite() with an analog value. digitalWrite() will interpret a value of 0 as LOW and any other value as HIGH. So the LED will mostly be on and only off in the time, when the brightness value becomes zero. You will have to use analogWrite() to get a fading effect.

  2. Currently your fading effect will be very very slow, since the value is only updated once every 830 ms. You should ditch all the delay() calls and instead use the principle, that is shown in the BlinkWithoutDelay example of the Arduino IDE. Think of the blinking as being like baking a pizza. When you want to bake a pizza in your oven, you wouldn't want to sit in front of the oven, staring at the pizza and waiting for it to be ready. This is basically, what you are doing with LED blinking in your sketch. With the pizza you would look at the clock, when you put the pizza in. Then you would go away doing other things and regularly check the clock for how much time has gone by. And then, when it is finally the time to take the pizza out, you would go back to the oven. There are plenty explanations and tutorials on the web, what exactly is done there.

Here at the Arduino documentation you can find the example with explaining text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.