1 LED Blink 1 LED Fades

I am working on a project that has 1 LED blink at small intervals and the other fading in and out. Currently when it runs, the LED that blinks works fine but the LED that is supposed to fade in and out turns on for around 30 seconds and then turns off for 30 seconds. I tried to make seperate loops for the two but I do not have the knowledge for that yet. This is my code so far.

int led2 = 4;
int led = 2;
int brightness = 0;
int fadeAmount = 5;

void setup() {
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
analogWrite(led, brightness);

brightness = brightness + fadeAmount;

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

digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(500);
}

Make sure that pin 2 is enabled for analogWrite. On an Uno for example, it's not.

Hello

On Arduino Uno, the PWM pins are 3, 5, 6, 9, 10 and 11.

Try this..

#include <multiMap.h>
#include <blinker.h>

#define  FADE_CYCLE_TIME   10                      // how many seconds for a full on/off fade?
#define  START_TIME        0                       // ms
#define  END_TIME          FADE_CYCLE_TIME * 1000  // ms
#define  MID_TIME          END_TIME/2              // Ms

#define  FADE_PIN          9        // The fading pin. (Must be pwm out pin)
#define  BLINK_PIN         13       // Any digital pin with an LED attached.
#define  STEP_TIME         50       // Number of mililseconds per change in brightness.

multiMap fadeMapper;                // A non-liner mapper to hold fade over time info.
blinker  LEDBlinker(BLINK_PIN);     // A fire and forget blinker to take care of the blinking LED.
timeObj  frameTimer(STEP_TIME);     // A timer for changing the fade value over time.
int      frame;                     // What step in the fading sequence are we in now?


void setup(void) {

   analogWrite(FADE_PIN,0);
   fadeMapper.addPoint(START_TIME,0);     // Starting at zero
   fadeMapper.addPoint(MID_TIME,255);     // Mid time is at full bright.
   fadeMapper.addPoint(END_TIME,0);       // End time, back to zero.
   LEDBlinker.setOnOff(true);             // Fire up blnkings.
   frame = 0;                             // Starting at frame time zero.
}


void loop() {

   int fadeVal;
   
   idle();                                      // Runs baclground stuff like the bliner.
   if (frameTimer.ding()) {                     // If the frame timer has expired (->ding<-)..
      fadeVal = round(fadeMapper.map(frame));   // Caculate a new fade value by mapping the frame time.
      analogWrite(FADE_PIN,fadeVal);            // Round the fade value to an int and send it out to the PWM pin.
      frame = frame + STEP_TIME;                // Bump up the frame time for the next reading
      if (frame>END_TIME) {                     // If the frame is beyond the last reading..
         frame = 0;                             // Reset it to zero.
      }
      frameTimer.stepTime();                    // Restart our frame timer for the next setting.
   }
}

You'll need LC_baseTools from the library manager to compile this.

-jim lee

Thank you for the help! If you do not mine explaining how to get to the library manager for my arduino that would be awesome. If not it is alright. Have a good day.

-Braydon F.

Edit:
I have downloaded the LC_baseTools

For some reason on this code the blinking LED runs perfectly fine but the fade LED now is blinking at the times it is needing to fade. Is there some error on my part? Thank you for your time.

-Braydon F.

Try this. It uses "millis timing" to give the effect that the blinking and fading are happening at the same time.

/*
 * Sketch...:   sketch_apr16a.ino
 * Target...:   Mega2560
 * 
 * Notes....:
 */

//includes

//defines

//constants
const uint8_t led2 = 4;
const uint8_t led = 2;      //note: if using an Uno choose one of pin 3, 5, 6, 9, 10, 11 

const uint32_t kDelayBlink = 500ul;     //500mS blink time
const uint32_t kDelayFade = 30ul;       //30mS between fading LED updates

//variables
int16_t 
    fadeAmount = 5,
    brightness = 0;
    
uint32_t
    timeNow,
    timerBlink,
    timerFade;
bool
    bLEDState = false;    

void setup( void )
{
    pinMode(led, OUTPUT);
    pinMode(led2, OUTPUT);
        
}//setup

void loop( void )
{
    //get the millis count
    timeNow = millis();
    
    //blinking LED
    //is it time to toggle the blinking LED?
    if( (timeNow - timerBlink) >= kDelayBlink )
    {
        //yes; save the time for the next toggle
        timerBlink = timeNow;
        //XOR with 'true' flips bLEDState between true and false each pass
        bLEDState ^= true;
        //update LED
        digitalWrite(led2, bLEDState);
        
    }//if

    //fading LED
    if( (timeNow - timerFade) >= kDelayFade )
    {
        timerFade = timeNow;
        brightness = brightness + fadeAmount;
        if (brightness <= 0 || brightness >= 255) 
        {
            fadeAmount = -fadeAmount;
            
        }//if    
        
        analogWrite(led, brightness);
        
    }//if
        
}//loop

Tested on a Mega. Note that pin 2 may not work with analogWrite on an Uno. It's fine on a Mega. See the comment in the code if you're using an Uno on using a different pin for the fading LED.

Thank you so much! It did work but I am a little upset that I wrote over 50 lines before you sent this and this works smoother. Thank you for your time!

-Braydon F.

Excellent. Do you understand what it's doing? The idea of not using delays() to time things?

Yes I believe I understand it a little better. Thank you so much.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.