Go Down

Topic: Fading led's in sequence not together. (Read 536 times) previous topic - next topic

boinger

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: [Select]
#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);
  }
}

CrossRoads

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

  if(currentMillis - previousMillis > interval) {
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

boinger

like this ?

Code: [Select]
#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);
 
}


Grumpy_Mike

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

CrossRoads

I was thinking more like this concept
Code: [Select]

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

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

boinger

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

Code: [Select]
#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

Grumpy_Mike

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy