Fade 4 Leds with delay

Dopo aver postato questo topic in inglese mi sono reso conto che c'è la sezione in italiano.... magari qualche connazionale mi puoi dare una mano in italiano che è piú immediato.... :smiley:


I try to fade 4 led with delay in this way:

When the first led reach 255 (max) must wait 2 seconds and after start to fade out. At the same time, when the first led fade out, the second led must start to fade on.... This loop must be repeat for the 4 leds.

The problem:

First led doesn't switch off completly, when the second fade on. The 3th led remain switch on at 255 value.... the 4th doesn't switch on at all.

this is the code that I'm using:

int brightness=0; 
int brightnessnext=0;
int change = 5; 
int changenext = 5; 
int ledact = 3; 
int lednext = 0; 
int ledchange = 1; 
int ledactcompletefadeon = 0;
int lednextcompletefadeon = 1;
int ledactfadeout = 0;
int lednextfadeout = 1;
int p = 0;
int x = 1;

void setup()
{
  pinMode(10,OUTPUT); 
  pinMode(3,OUTPUT); 
  pinMode(5,OUTPUT); 
  pinMode(6,OUTPUT);
}

int main()
{
  init();
 int i, pin[4] = { 3, 5, 6, 10};
  while (x < 10) {
  
  if (lednextcompletefadeon == 1 || ledactfadeout == 0){
     analogWrite(ledact, brightness); 
     brightness = brightness + change;
    }	 

  if (brightness == 255){ 
    delay(2000);
	lednextfadeout = 0;
    change=-change;       
    p = p +1;
    if (p==4) {
      p =-1;
    }
    lednext = pin[p]; 
    ledactcompletefadeon = 1;
  }
  
  if (ledactcompletefadeon == 1 || lednextfadeout == 0){
    analogWrite(lednext, brightnessnext);
    brightnessnext = brightnessnext + changenext; 
	
   }	
  if (brightnessnext == 255){ 
	delay(2000);
    ledactfadeout = 0;
    changenext=-changenext;     
    p = p +1;
     if (p==4) {
      p =-1;
      }
    ledact = pin[p];
    lednextcompletefadeon = 1;
  }
	
  if (brightness == 0){            
    analogWrite(ledact,0);      
	change=-change;              
	ledactfadeout = 1;
	ledactcompletefadeon = 0;
  }

  if (brightnessnext == 0){            
    analogWrite(lednext,0);       
	changenext=-changenext;              
	lednextfadeout = 1;
	lednextcompletefadeon = 0;
  }
  
  delay(30);              

  } 
  return 0;
 }

Could you help me to fix it?

Thank you in advance
Best Regards,
Marco

Dovresti commentare il codice.

int brightness = 0;
int brightnessnext = 0;
int change = 5;
int changenext = 5;
int ledact = 3;
int lednext = 0;
int ledchange = 1;
int ledactcompletefadeon = 0;
int lednextcompletefadeon = 1;
int ledactfadeout = 0;
int lednextfadeout = 1;
int p = 0;
int x = 1;
int pin[4] = { 3, 5, 6, 10};

void setup()
{
  pinMode(pin[0], OUTPUT);
  pinMode(pin[1], OUTPUT);
  pinMode(pin[2], OUTPUT);
  pinMode(pin[3], OUTPUT);

  int p = 0;
  int x = 1;
  while (x < 10) {

    if (lednextcompletefadeon == 1 || ledactfadeout == 0) {
      analogWrite(ledact, brightness);
      brightness = brightness + change;
    }

    if (brightness == 255) {
      delay(2000);
      lednextfadeout = 0;
      change = -change;
      p++;
      if (p == 4) p = -1;
      lednext = pin[p];
      ledactcompletefadeon = 1;
    }

    if (ledactcompletefadeon == 1 || lednextfadeout == 0) {
      analogWrite(lednext, brightnessnext);
      brightnessnext = brightnessnext + changenext;

    }
    if (brightnessnext == 255) {
      delay(2000);
      ledactfadeout = 0;
      changenext = -changenext;
      p++;
      if (p == 4) p = -1;
      ledact = pin[p];
      lednextcompletefadeon = 1;
    }

    if (brightness == 0) {
      analogWrite(ledact, 0);
      change = -change;
      ledactfadeout = 1;
      ledactcompletefadeon = 0;
    }

    if (brightnessnext == 0) {
      analogWrite(lednext, 0);
      changenext = -changenext;
      lednextfadeout = 1;
      lednextcompletefadeon = 0;
    }

    delay(30);
  } // End while
}

void loop()
{
}

L'ho arduinizzato! :grin:

Intanto grazie....

ho messo qualche commento.... magari risulta più chiaro...

La cosa che mi fa rimanere piu´perplesso è perchè il primo pin non si spegne.... mah...

int brightness = 0;
int brightnessnext = 0;
int change = 5;
int changenext = 5;
int ledact = 3;
int lednext = 0;
int ledchange = 1;
int ledactcompletefadeon = 0;
int lednextcompletefadeon = 1;
int ledactfadeout = 0;
int lednextfadeout = 1;
int p = 0;
int x = 1;
int pin[4] = { 3, 5, 6, 10};

void setup()
{
  pinMode(pin[0], OUTPUT);
  pinMode(pin[1], OUTPUT);
  pinMode(pin[2], OUTPUT);
  pinMode(pin[3], OUTPUT);

  int p = 0;
  int x = 1;
  while (x < 10) {

    if (lednextcompletefadeon == 1 || ledactfadeout == 0) { //actual pin sequence (start from 1th pin)
      analogWrite(ledact, brightness);
      brightness = brightness + change;
    }

    if (brightness == 255) {  //when fade on is complete wait 2 sec. and change the value to start
      delay(2000);              the fade off.After, change pin.
      lednextfadeout = 0;
      change = -change;
      p++;
      if (p == 4) p = -1;
      lednext = pin[p];
      ledactcompletefadeon = 1;
    }

    if (ledactcompletefadeon == 1 || lednextfadeout == 0) { //Next pin sequence start when the actual pin
      analogWrite(lednext, brightnessnext);                    sequence is finish
      brightnessnext = brightnessnext + changenext;

    }
    if (brightnessnext == 255) {
      delay(2000);
      ledactfadeout = 0;
      changenext = -changenext;
      p++;
      if (p == 4) p = -1;
      ledact = pin[p];
      lednextcompletefadeon = 1;
    }

    if (brightness == 0) { //when brightness is 0 switch off the pin and re-change the change value
      analogWrite(ledact, 0);
      change = -change;
      ledactfadeout = 1;
      ledactcompletefadeon = 0;
    }

    if (brightnessnext == 0) {
      analogWrite(lednext, 0);
      changenext = -changenext;
      lednextfadeout = 1;
      lednextcompletefadeon = 0;
    }

    delay(30); //delay of fade on-off
  } // End while
}

void loop()
{
}

Ti consiglio di fare gli if su brightness non con == ma con => 250 e =< 10, sei più sicuro ...

Brado:
Ti consiglio di fare gli if su brightness non con == ma con => 250 e =< 10, sei più sicuro ...

Grazie. Per quanto riguarda il programma vedi qualcosa per cui non dovrebbe fare quello che ho in mente?...intendo, ci sono degli errori logici?

Grazie

Innanzitutto: perchè hai messo tutto nel setup?
Nel loop è meglio, e soprattutto non devi mettere quel ciclo while.

Poi, mi sembra che non funzioni perchè finito il primo giro, ed hai attribuito il secondo led, quando arriva il momento del terzo led, utilizzi brightnessnext che è già valorizzata.

Secondo me dovresti usare due array, uno per i pin (come già fai) ed uno per la luminosità tipo:

int brightness[4] = { 0, 0, 0, 0};

Così puoi con i dovuti controlli del caso e le variabili di stato necessarie, valorizzare in modo corretto l'array, e poi girare il tutto sull'uscita con analogWrite(pin[p], brightness[p]).

Brado:
Innanzitutto: perchè hai messo tutto nel setup?
Nel loop è meglio, e soprattutto non devi mettere quel ciclo while.

Poi, mi sembra che non funzioni perchè finito il primo giro, ed hai attribuito il secondo led, quando arriva il momento del terzo led, utilizzi brightnessnext che è già valorizzata.

Secondo me dovresti usare due array, uno per i pin (come già fai) ed uno per la luminosità tipo:

int brightness[4] = { 0, 0, 0, 0};

Così puoi con i dovuti controlli del caso e le variabili di stato necessarie, valorizzare in modo corretto l'array, e poi girare il tutto sull'uscita con analogWrite(pin[p], brightness[p]).

Ti ringrazio per la risposta.

Allora ho messo tutto in setup e non in loop semplicemente per il fatto che se no non si incrementerebbe correttamente la variabile 'p' che indica il numero del PIN.
Il terzo led secondo i miei calcoli si dovrebbe accendere con brightness non brightnessnext. La sequenza dovrebbe essere:

brightness (1 led)
brightnessnext (2 led)
brightness (3 led)
brightnessnext (4led)

Il terzo led si accende ma non inizia neanche a fare fade off....è il 4 che non si accende nemmeno.

Comunque... come avrai capito sono una rana in programmazione.... mi puoi buttare giu`un esempio con i 2 array come suggerisci?

Grazie mille per l'aiuto!

surfparadise:
Ti ringrazio per la risposta.

Allora ho messo tutto in setup e non in loop semplicemente per il fatto che se no non si incrementerebbe correttamente la variabile 'p' che indica il numero del PIN.

Hai dichiarato due volte int p = 0; e int x = 1; ecco perchè non si incrementa in modo corretto.

Sposta tutto in loop, elimina il while, e la seconda dichiarazione di p ed x.

surfparadise:
Il terzo led secondo i miei calcoli si dovrebbe accendere con brightness non brightnessnext. La sequenza dovrebbe essere:

brightness (1 led)
brightnessnext (2 led)
brightness (3 led)
brightnessnext (4led)

Il terzo led si accende ma non inizia neanche a fare fade off....è il 4 che non si accende nemmeno.

Comunque... come avrai capito sono una rana in programmazione.... mi puoi buttare giu`un esempio con i 2 array come suggerisci?

Grazie mille per l'aiuto!

Invece di diventare matto a scriverti da 0 io il tuo codice, metti delle stampe di debug con il serial.print, e vedi che le variabili assumano il valore che tu credi debbano assumere.
Così impari anche a debuggare il tuo sketch :wink:

Brado:
Invece di diventare matto a scriverti da 0 io il tuo codice, metti delle stampe di debug con il serial.print, e vedi che le variabili assumano il valore che tu credi debbano assumere.
Così impari anche a debuggare il tuo sketch :wink:

COme si fa?

QUI il reference di Arduino, QUI un'altra guida.

...ora ho capito cosa intendi.

Grazie

:wink: