fade led variabile ?

salve come da oggetto volevo sapere se era possibile fare una modifica al esempio fade:

mi spiego meglio ho ricreato una fila di led collegata al arduino e tutto funziona con fade ma se io volessi ricreare un paio di ritardi differenti da eseguire a rotazione e possibile?

/*
Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(50);
}

mi spiego meglio vorrei che una funzione duri 5 min e poi ne inizi un altra sempre per 5 min con un delay diverso

Crea una macchina a "stati" ...
... in una variabile ti segni "lo stato" in cui ti trovi e, in base ad esso, nel loop(), chiami una funzione piuttosto che un'altra. In ogni funzione fai un differente ciclo di fade.

In questo modo, creando N funzioni, potrai avere N "stati" controllati nel loop() e, cambiando "lo stato" (il valore della variabile che lo contiene ... 0, 1, 2, 3, ... N) potrai cambiare effetto..

Guglielmo

Macchina a stati, pagina in italiano con esempio per Arduino:
http://www.lucadentella.it/2013/04/30/macchina-a-stati-finiti-e-arduino/

grazie 1000 a tutti ma mi sono incasinato di brutto provo ancora se ho problemi posto lo sketch e vediamo dove sbaglio

scusate ho io non ho capito niente (cosa sicura) perché e la mia seconda volta che provo a capire arduino incollo il codice che ho scritto nello sketch

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(50);                            
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis1 > interval1) {
    previousMillis1 = currentMillis; // save the last time I blinked the LED
    //if the LED is off turn it on and vice-versa:
    ledState ^= 1;
    digitalWrite(ledPin, ledState);
  }
  currentMillis = millis();
  if (currentMillis - previousMillis2 > interval2) {
    previousMillis2 = currentMillis; // save the last time I printed on the serial
    Serial.println(++counter);
  }
}

dove che sbaglio ?

... nell'avere DUE loop() !!! :o

Puoi avere UN setup() ed UN loop(), più altre funzione tue con altri nomi decisi da te, ma NON già utilizzati in librerie, ecc. ecc. ...

Guglielmo

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(50);                            
}

{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis1 > interval1) {
    previousMillis1 = currentMillis; // save the last time I blinked the LED
    //if the LED is off turn it on and vice-versa:
    ledState ^= 1;
    digitalWrite(ledPin, ledState);
  }
  currentMillis = millis();
  if (currentMillis - previousMillis2 > interval2) {
    previousMillis2 = currentMillis; // save the last time I printed on the serial
    Serial.println(++counter);
  }
}

fatto ma ancora non funziona

Hnera ... perdona ... ma non hai neanche idea cosa sia una "funzione" ... siamo veramente alle "basi" del C ... senza quelle NON riesci a fare nulla ... :confused:

Dai retta, dedica un minimo di tempo alla lettura di qualche testo sulla programmazione in C ... puoi cominciare con QUESTO ... o, se riesci a leggere testi in inglese puoi prendere QUESTO ... ma comunque ... un po' ti ci devi dedicare ... e vedrai che poi ti riavvicinerai sicuramente più produttivo al tuo programma. :slight_smile:

Guglielmo

grazie mille vediamo che riesco a fare