Fading HELP

Quijonsith thanks a lot!

I´ve tried your notes and here is the result:

1.)GOOD NEWS: map function is mostly working. I´ve made simple code to test it:

average=map(average, 170, 700, 0, 255);
analogWrite (ledPin1, average);

it is working, but only when IR is detcting something.
BUT when the space infront of the IR is empty ( so the measured data is about 160 -- so this might be remaped to 0) the LED is fully shining. Thats the first problem... :stuck_out_tongue:

2.)
I ve put your improved code into arduino (and check the syntax ;}{ etc..) and the result is, that the led was very little blinking all the times and it does not react to average data....

so I have deleted all the parts with the millis, because logicaly I though it could also work in very simple way
(also I ve put && instead & in if condition - as I understand when used && then both conditions must be true.
But what happens when just & is written - is it the same or not???):

if (average >= 600 && LED1Trigger == 0) //if the average is high enough and LED1 is not already on
{
led1val = 255; //set the brightness of LED1
LED1Trigger = 1; //store the fact that LED1 is now on and average has not gone out of range
}
if (average < 600 && LED1Trigger == 1) //if the average has gone below this range and LED1 was already on
{ LED1Trigger = 0; //store the fact that average has gone out of this range
led1val -= ledDecay; //start fading out LED1's brightness
analogWrite (ledPin1, led1val); //set LED1 to it's brightness
}

The result was that when I was in range, the LED lights on}OK thats GOOD!) and stay shining even when I moved out of the range... (thats BAD...)

So I ve tried simple test of the fading code:

if (average >= 600 ) //if the average is high enough and LED1 is not already on
{ led1val=255; // set the starting value to max
led1val -= ledDecay; // start fading out LED1's brightness
analogWrite (ledPin1, led1val); //set LED1 to it's brightness
}

But the result was , the same - LED starts shinig and stay shining all the time and didnt react on anything....

3.) I ve noted that when LED puted on PWM+ (just pin6) and no specified data is send there, the LED is shining.
(i ve just declard it as output - nothing more...). Is it normal? But the PWM+ in pin 5 doesn´t do this....

4.)

LED1ON = millis ();} //store what time LED1 was turned on

How does this work? Is it storing just the time the led is shining (for example 300ms) or is it giving to LED1ON the value from the central clock?
Does the millis work as the central clock of the program running all the time from the starting program?
So when i say LED1ON = millis and the millis in this moment is for example 32512345, then the LED1ON is 32512345??
Or it starts counting the time in millis from this moment? (so it will be 0 and rising up...)

Thanks a lot!

I am really beginer - I´ve bought the arduino one year ago, but when I looked at programming enviroment I said UFFF! :o and put arduino back to the box...
I opened it again 5 days ago. Fisrt day I pathed through Geting started. Next day I read mostly the all Reference. The third day I tried some learning programs and check what the hell these numbers and signs are doing... Then I tried to changing some values and check what it is doing.... As I simply understand it I ve tried to create my first program. So and here it is...

Thanks alot with helping! :slight_smile:

P.S.:
Now it is experimenting - learning the code and hardware, but yes I want to make a small interactive project - thats the reason why I am learning and putting effort in it. I will put video on youtube, when it will be working.
Anyway I live in the Czech republic and making music and video, but now interactivity is intersting me very much!
The idea of audio as input source is great! This could be some LED music like an old chinesse cassete players... :smiley:

Cheers JIRI