Pages: [1]   Go Down
Author Topic: Fading led's in sequence not together.  (Read 441 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

This is my first arduino project that i am trying to learn to do myself and not some pre assembled thing. What I am trying to do is to fade the led one after another not together. And I can't seem to manage to do that. here is my code.

Code:
#include <math.h>
unsigned long previousMillis = 0; // last time update
long interval = 2000; // interval at which to do something (milliseconds)

void setup()
{
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
 
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(11, val);
  if(currentMillis - previousMillis > interval) {
     previousMillis = currentMillis;
  analogWrite(11, 0);
  }
  if(currentMillis - previousMillis > interval) {
     previousMillis = currentMillis;
  analogWrite(10, val);
  }
}
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26934
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

Make your loop so that only fade action is done at a time when this comparison is true

  if(currentMillis - previousMillis > interval) {
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

like this ?

Code:
#include <math.h>

unsigned long previousMillis = 0; // last time update
long interval = 2000; // interval at which to do something (milliseconds)

void setup()
{
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
 
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(11, val);
  if(currentMillis - previousMillis > interval) {
    analogWrite(11, 0);
    previousMillis = currentMillis;
 
  }
  //if(currentMillis - previousMillis > interval) {
     //previousMillis = currentMillis;
  analogWrite(10, val);
 
}

Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 618
Posts: 33940
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No, only do the fading when the millis timer times out like CrossRoads said. You have not done this.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26934
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

I was thinking more like this concept
Code:
void loop(){
currentMillis = millis();
if (currentMillis - previousMillis >= interval){
previousMillis = previousMillis + interval;
led_to_fade = led_to_fade+1;
if (led_to_fade== max){led_to_fade = 0;}  // reset pointer, say max = 3 for this example
// float calculation
switch(led_to_fade){
case 0:
:
:
break;
case 1:
:
:
break;
case 2:
:
:
break;
} // end switch
} // end time check
} // end loop
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think that confused me more I tried something like this but i am pretty sure its wrong.

Code:
#include <math.h>

unsigned long previousMillis = 0; // last time update
long interval = 2000; // interval at which to do something (milliseconds)

int led1 = 11;
int led2 = 10;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop(){
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval){
    previousMillis = previousMillis + interval;
    led1 = led1+1;
    if (led1== 3){led1 = 3;}  // reset pointer, say max = 3 for this example
   
    float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
   
switch(led1){
case 0:
analogWrite(led1, val);

break;
case 1:
analogWrite(led2, val);

} // end switch
} // end time check
} // end loop
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 618
Posts: 33940
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are recreating the variable currentMillis every time the loop is execuited so it can never time out.
Logged

Pages: [1]   Go Up
Jump to: