Pages: [1]   Go Down
Author Topic: Fading LED on pin 13 without delay.  (Read 740 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having trouble writing modifying this code that fades the LED on pin 13 on and off to do the exact same thing but without using any delays.

Here is the code which I currently have.

Code:
int ledPin = 13;
unsigned int i=0;
boolean fade=true;
int interval=1000;
void setup(){
  pinMode(ledPin, OUTPUT);
}
void loop()
{
  if(i == interval)  {
    i=1;
    fade=!fade;
  }
  if(fade == false)  {
    digitalWrite(13, LOW);
    delayMicroseconds(i);
    digitalWrite(13, HIGH);
    delayMicroseconds(interval-i);
  }
  if(fade == true)  {
    digitalWrite(13, LOW);
    delayMicroseconds(interval-i);
    digitalWrite(13, HIGH);
    delayMicroseconds(i);
  }
  i+=1;
}

So far, I converted some of the code but I don't know how to make the Arduino wait without interrupting other code I plan to have running simultaneously.

Can please give me some ideas?

Code:
int ledState = LOW;
long previous = 0;
long interval = 0;
boolean fade = true;

void setup() {
  pinMode(13, OUTPUT);     
}

void loop()
{
  unsigned long current = micros();
  if(current - previous > interval) {
    previous = current;
    fade = !fade;  //change fading
  }
  //fade out
  if(fade == false)  {
    digitalWrite(13, LOW);
    digitalWrite(13, HIGH);
  }
  //fade in
  if(fade == true)  {
    digitalWrite(13, LOW);
    digitalWrite(13, HIGH);
  }
}
Logged

Sie sind das Essen und wir sind die Jäger.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/blink

Use millis ().
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4545
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, delay...the devil's function.

Logged

No, I don't answer questions sent in private messages...

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having trouble writing modifying this code that fades the LED on pin 13 on and off to do the exact same thing but without using any delays.
It would be much easier if you connected your LED on one of the PWM pins to control the fading.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 437
Posts: 23660
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Easier with PWM, but certainly not required. I just wrote some code to output 13 notes from 13 button presses, with frequencies from 261 Hz to 523 Hz. Was certainly not hard using blink without delay type coding.

All you're doing is controlling the on/off time. Setup a time within a time - the outer time is how fast you are pulsing, the inner time is how long you leave the output high within the total time for brightness control.


Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, delay...the devil's function.

Code:
delay (666);
Logged

Pages: [1]   Go Up
Jump to: