Pages: 1 [2]   Go Down
Author Topic: Fading HELP  (Read 1852 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 207
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Thanks for all replies!
BigMike: I´ll try it. Thanks a lot in way how to thing about the problem!
I think I will have to divide (not multiple) it by 2.11 to get the result on range 0-255. Isnt it true?

You are correct, it should be a divide. Or you could calculate 256 / 540 and multiply by that.

I haven't tried it on an arduino, but intuitively I guess is a floating point divide will take longer than a floating point multiply. It feels safer at least.

Quote
Floating points... Is there some problem with them using as a value for a PWM output?

No problem - its just integer sums are quicker than floating point sums.

Regards,

Mike


Logged

Dyess AFB, Tx
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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... smiley-razz

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....

To fix the new problem you could reverse all of your code, changing 255's to 0's and 0's to 255's aswell as changing to ledval-= to ledval+=, or you could do it the easy way and reverse your LED as it sounds to me like it's in backwards. smiley-wink

The millis() function returns the number of milliseconds since the program started, so it's like an internal clock.  So when it says LED1ON = millis(), if the program has been running for 7298319 milliseconds, that's what is stored in LED1ON.  Then later when you have if(millis() - LED1ON >= interval) it's comparing the current number of milliseconds since program start to LED1ON.  So "interval" is how many milliseconds the LED will stay on until it starts to fade out.

Using millis() instead of delay() allows the program to do other things (like compare the other ranges) while it waits to fade the LED, instead of sitting and doing nothing until it's time to fade the LED.  So using:
LED1ON = millis();
if(millis() - LED1ON >= interval);

is the same as:
delay(interval);

Only it lets your program keep running.  If the LED isn't fading out smoothly/slowly enough for you then you can decrease the subtraction, such as led1val-=1.  If it starts fading too soon, increase interval.

You could also hook a potentiometer up to another analog input and read it's value as interval, or do the same for the subtraction value, or both, and you would be able to adjust them while the program is running without having to rewrite your program every time.

Also:
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
     }


The problem with this test fade code is every time it runs it sets led1val to 255, so even though it subtracts afterwards, as long as you're >=600, led1val gets set to 255 again.  The subtraction and analogwrite should be outside the if statement.

Try my code again, but with the LED reversed from how it is now and try simply adjusting ledDecay and interval as I described.

Also, you are correct about using && in the if statement.  That's why I asked you to look it over smiley-wink.  I haven't programmed in a long time so I'm still remembering the syntax.

[edit]Also, here's the reference for millis():
http://www.arduino.cc/en/Reference/Millis[/edit]
« Last Edit: July 09, 2008, 11:16:16 am by Quijonsith » Logged

Czech republic, Brno
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your suggestions!

I ve tested something and when I think the code could be working its telling me " expected ) befor { token "

here is the new ON/OFF fading code:

   
if  (average >= 600 )                                                   //if the average is high enough
   { led1val = 250;                                    //set the brightness of LED1 - its normal FULL SHINE without fade
      LED1ON = millis ();                                                 //store what time LED1 was turned on
    }                                            
 if (average < 600 && (millis () - LED1ON <= 25 )         // if the average has gone below this range and the time elapsed from the
                                                                                  // change of the condition is less or equal 25ms
   { do { led1val -= ledDecay;                                  // start with fading out
            analogWrite (ledPin1, led1val);                      // write the fading value  
         }        
     while (led1val > 0);                                               // untill the LED is completly OFF
    }  


I really didnt think that the fading could be so dificult.... smiley-razz
Nothing worked well yet... nonstop looped fading, or just little bit blinking.....

But thanks to your comments I slowly start to understand the data flow smiley


« Last Edit: July 09, 2008, 05:02:23 pm by JURA » Logged

Dyess AFB, Tx
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
thanks for your suggestions!

I ve tested something and when I think the code could be working its telling me " expected ) befor { token "

here is the new ON/OFF fading code:

   
if  (average >= 600 )                                                   //if the average is high enough
   { led1val = 250;                                    //set the brightness of LED1 - its normal FULL SHINE without fade
      LED1ON = millis ();                                                 //store what time LED1 was turned on
    }                                            
 if (average < 600 && (millis () - LED1ON <= 25 )         // if the average has gone below this range and the time elapsed from the
                                                                                  // change of the condition is less or equal 25ms
   { do { led1val -= ledDecay;                                  // start with fading out
            analogWrite (ledPin1, led1val);                      // write the fading value  
         }        
     while (led1val > 0);                                               // untill the LED is completly OFF
    }  


I really didnt think that the fading could be so dificult.... smiley-razz
Nothing worked well yet... nonstop looped fading, or just little bit blinking.....

But thanks to your comments I slowly start to understand the data flow smiley



if  (average >= 600 )                                                   //if the average is high enough
   { led1val = 250;                                    //set the brightness of LED1 - its normal FULL SHINE without fade
      LED1ON = millis ();                                                 //store what time LED1 was turned on
    }                                            
 if (average < 600 && (millis () - LED1ON <= 25 ))         // if the average has gone below this range and the time elapsed from the
                                                                                  // change of the condition is less or equal 25ms
   { do { led1val -= ledDecay;                                  // start with fading out
            analogWrite (ledPin1, led1val);                      // write the fading value  
         }        
     while (led1val > 0);                                               // untill the LED is completly OFF
    }

You missed a ) in the if second if statement (see where I put the red one).
Logged

Czech republic, Brno
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ups :smiley (I ll never again make post like this....)

But the led is still fading onceagain and onceagain  :-/

I just want to fade out once

Somewhere here is the problem / please look at my RED interpretation of the code:

if  (average >= 600 )                       //if the average is high enough  
   { led1val = 250;                            //set the brightness of LED1 - its normal FULL SHINE without fade
     LED1ON = millis ();                     /*store what time LED1 was turned on - does it stop writing millis() to LED1ON      
                                                          when the if  condition is not valid???
*/
    }                                              
 if (average < 600 && (millis () - LED1ON <= 25 )         /* if the average has gone below this range and the time  
                                                                                elapsed from the  change of the condition is less or equal 25ms
*/
   { do { led1val -= ledDecay;                                  // start with fading out
           analogWrite (ledPin1, led1val);                      // write the fading value  
        }        
     while (led1val > 0);                               /* untill the LED is completly OFF - when the led1val will be 0 then the  
                                                               do loop will stop and than program will check the if statement: the result might be false,  
                                                                because millis() - LED1ON might be bigger than 25ms - is it right???
*/
    }  
Logged

Czech republic, Brno
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Finally I ve found simple soulution. Thanks for any support smiley-wink

But I have another begginers problem:

Please how to write in code three conditions for if?

I used multiplied &&:

   if (condition1 && condition2 && condition 3 )
     { //do something }

but only first two conditions were checked.
Do you know how to test all three conditions?

Thanks a lot!
« Last Edit: July 22, 2008, 02:05:04 pm by JURA » Logged

London
Offline Offline
Faraday Member
**
Karma: 10
Posts: 6248
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In a compound logical and statement like that, the compiler produces code that will stop checking as soon as any condition is false. So if condition1 is false, conditions 2 & 3 aren't checked. If condition2 is false then condition3 is not checked.

If this doesn't explain what is happening then post the smallest working sketch that illustrates what you are observing.
« Last Edit: July 22, 2008, 02:27:04 pm by mem » Logged

Czech republic, Brno
Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

 :)Thanks. I understand. Now works well.

So it si possible to set even unlimited more conditions with &&, isn´t it right?  :smiley
Then the code is checking from the first condition on left to the last on the  right and when the first is false the loop stops.
Logged

London
Offline Offline
Faraday Member
**
Karma: 10
Posts: 6248
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Exactly. So if you have a condition that is more likely than the others to be false, its most efficient to have that one first so no time is wasted evaluating expressions that don't matter.
« Last Edit: July 22, 2008, 04:34:29 pm by mem » Logged

Pages: 1 [2]   Go Up
Jump to: