Fading HELP

So how to do it?

I think about to make some function that detect the CHANGE of the condition state. If the change happens (but just from 1 to 0), than the fading will begin - just once. I dont know how to program it. My syntax and programming knowledge is very little :cry:

I try also something with millis , but it doesnt work.:
(the red is the change in the code)

#define NUMREADINGS 10 // nastavuje pocet prumerovanych cteni na 10

int sensPin = 0; // sensor zapojeny na analog pin 0
int ledPin1 = 11; // led zapojena na digital pin pwm 11
int ledPin2 = 10; // led zapojena na digital pin pwm 10
int ledPin3 = 9; // led zapojena na digital pin pwm 9
int ledPin4 = 6; // led zapojena na digital pin pwm 6
int ledPin5 = 5; // led zapojena na digital pin pwm 5
int ledPin6 = 3; // led zapojena na digital pin pwm 3
int ledPin7 = 13; // led zapojena na digital pin klasik 13
int sensval = 0; // variable na signal ze senzoru
int ledval = 0; // variable na signal do LED

int readings[NUMREADINGS]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // vysledny prumer

long time; // pocita cas millis od spusteni programu
long LED1ON = 0; // will store last time LED1 was used
int interval = 1; // interval rozdilu mezi ON/OFF

void setup()
{
pinMode (ledPin1, OUTPUT); // deklaruje ledpin jako výstup
pinMode (ledPin2, OUTPUT);
pinMode (ledPin3, OUTPUT);
pinMode (ledPin4, OUTPUT);
pinMode (ledPin5, OUTPUT);
pinMode (ledPin6, OUTPUT);
pinMode (ledPin7, OUTPUT);
Serial.begin(115200); // initialize serial communication with computer - LZE TIM ZRYCHLIT ODEZVU
for (int i = 0; i < NUMREADINGS; i++)
readings = 0; // initialize all the readings to 0

}

void loop() {

*// toto zprumerovava mereni na IR *
total -= readings[index]; // subtract the last reading

  • readings[index] = analogRead(sensPin); // read from the sensor*

  • total += readings[index]; // add the reading to the total*

  • index = (index + 1); // advance to the next index*

  • if (index >= NUMREADINGS) // if we're at the end of the array...*

  • index = 0; // ...wrap around to the beginning*

  • average = total / NUMREADINGS; // calculate the average*

  • Serial.println(average); // send it to the computer (as ASCII digits)*

  • Serial.println(average); // send it to the computer (as ASCII digits)*

  • // toto spusti citac casu*

  • // ted jdu premapovat v krocich urovne ze senzoru na signaly pro LEDky *

  • if (average >= 600)*

  • { ledval = 255;*

  • analogWrite (ledPin1, ledval);*

  • LED1ON = millis ();}*

  • else*

  • {analogWrite (ledPin1, 250); }*

  • if (millis () - LED1ON == interval);*

  • ledval -= 25;*

  • {analogWrite (ledPin1, ledval);}*

  • if (ledval=0);*

  • {return;}*

*// the rest would be the same, but it doesnt work - just 2 leds shine all the time *
if (average >= 400 & average <600)

  • {analogWrite(ledPin2, 200); }*

  • else*

  • {analogWrite (ledPin2, 0); }*

  • if (average >= 300 & average <400 )*

  • {analogWrite (ledPin3, 200); }*

  • else*

  • {analogWrite (ledPin3, 0); }*

  • if (average >= 230 & average <300)*

  • {analogWrite (ledPin4, 200); }*

  • else*

  • {analogWrite (ledPin4, 0); }*

  • if (average >= 200 & average <230)*

  • {analogWrite (ledPin5, 200); }*

  • else*

  • {analogWrite (ledPin5, 0); }*

  • if (average >= 180 & average <200)*

  • {analogWrite (ledPin6, 200); }*

  • else*

  • {analogWrite (ledPin6, 0); }*

  • if (average >= 163 & average <180)*

  • {digitalWrite (ledPin7, HIGH); }*

  • else*

  • {digitalWrite (ledPin7, LOW); } *
    }
    Thanks a lot for any idea and suggestion!
    P.S.: Sorry of my english - I am not native speaker....