Go Down

Topic: Flash and Fade (Read 839 times) previous topic - next topic

edgaryo

Hey , i want to make a project . In this project i want to have a led to flash ( stay on 100 ms ) and when is on i want another led to make fade . This led and flash is power on from a sensor . I put my code and please someone to help me to make completed this project .
So i want when sensor is active , one led is flash ( one time ) and another led is fade ( one time ) .

Code: [Select]
byte ledPin = 13; // This led is flash
int led = 9;           // this led is fadeing
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by


word turnLedOffAfterTime = 100;

void setup()
{
  pinMode( ledPin, OUTPUT );
   
}

void loop()
{

  while ( analogRead( A0 ) > (int)( 35L * 900L / 50L ));;
{
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );

while ( analogRead( A0 ) < (int)( 35L * 1024L / 50L ));;
}
if ( analogRead( A0 ) > (int)( 35L * 1023L / 50L ));;
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 ;
  } 

}




Please help me , thanks !

polymorph

What's your boggle, fellow citizen?

Why not to use delay() for timing events:

http://playground.arduino.cc/Code/AvoidDelay

http://arduino.cc/en/Tutorial/BlinkWithoutDelay
Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

edgaryo


What's your boggle, fellow citizen?

Why not to use delay() for timing events:

http://playground.arduino.cc/Code/AvoidDelay

http://arduino.cc/en/Tutorial/BlinkWithoutDelay


The code is working but problem is the fadeing . Is fade when is activ the sensor , i mean the fade is growing up when is activ sensor , but i want all cicle of fadeing to make when the sensor is activ . . .

Grumpy_Mike

You have to learn how to write the syntax of code first:-
Code: [Select]
while ( analogRead( A0 ) > (int)( 35L * 900L / 50L ));;
Two semicolons where you need non, this statement will just hold and do nothing.
Why use Long constants, do arithmetic and cast them as an int? That makes no sense at all.
In fact why do that arithmetic at all just replace it with the number that works out to be which is 630.
So the first line should be:-
Code: [Select]
while ( analogRead( A0 ) > 630 )
Same goes for the next while and the if statement.


Correct that, post the code again and say what it does and what you want it to do.

edgaryo


You have to learn how to write the syntax of code first:-
Code: [Select]
while ( analogRead( A0 ) > (int)( 35L * 900L / 50L ));;
Two semicolons where you need non, this statement will just hold and do nothing.
Why use Long constants, do arithmetic and cast them as an int? That makes no sense at all.
In fact why do that arithmetic at all just replace it with the number that works out to be which is 630.
So the first line should be:-
Code: [Select]
while ( analogRead( A0 ) > 630 )
Same goes for the next while and the if statement.


Correct that, post the code again and say what it does and what you want it to do.


is same

Quote
byte ledPin = 13; // This led is flash
int led = 9;           // this led is fadeing
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by


word turnLedOffAfterTime = 100;

void setup()
{
  pinMode( ledPin, OUTPUT );
   
}

void loop()
{

  while ( analogRead( A0 ) < 630)
{
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );

while ( analogRead( A0 ) < (int)( 35L * 1024L / 50L ));;
}
if ( analogRead( A0 ) < 630)
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 ;
  } 

}


Grumpy_Mike

Try again to do what I told you.
You have only changed the first while and you have messed up the comparison into the bargain.
Do not use the quote icon for posting code, use the # icon next to it.

edgaryo

Is fade when is activ the sensor , i mean the fade is growing up when is activ sensor , but i want all cicle of fadeing to make when the sensor is activ one time , , and after is fadeing when is active another time the sensor . . But total fadeing ( led to grow up and decrease ) to make when is activ sensor , like the flash . I need when activ the sensor one led is flashing one time and another led to fade one time  .

edgaryo

now is not fadeing , and is not repet the void . I need to reset Arduino to work another time :D

Code: [Select]
byte ledPin = 13; // This led is flash
int led = 9;           // this led is fadeing
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by


word turnLedOffAfterTime = 100;

void setup()
{
  pinMode( ledPin, OUTPUT );
   
}

void loop()
{

  while ( analogRead( A0 ) < 630)
{
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );

while ( analogRead( A0 ) < 1024 )


if ( analogRead( A0 ) < 630)
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 ;
  }

}
}

Grumpy_Mike

What is this line supposed to do?
Code: [Select]
while ( analogRead( A0 ) < 1024 )
An analogue read will always be below 1024 because the biggest value is 1023.
What is supposed to execute here?

edgaryo


What is this line supposed to do?
Code: [Select]
while ( analogRead( A0 ) < 1024 )
An analogue read will always be below 1024 because the biggest value is 1023.
What is supposed to execute here?


yes is an analog sensor . And i need to stop flash to not stay on when is activ , to stay on only 100 ms . So is working but the problem is the fade not flash 

edgaryo

Code: [Select]
byte ledPin = 13; // This led is flash
int led = 9;           // this led is fadeing
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by


word turnLedOffAfterTime = 100;

void setup()
{
  pinMode( ledPin, OUTPUT );
   
}

void loop()
{

  while ( analogRead( A0 ) < 630)
{
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );

while ( analogRead( A0 ) < 1023)


if ( analogRead( A0 ) > 630)
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 ;
  }

}
}


and the flash is not working like i want to work :(

BulldogLowell


Code: [Select]
byte ledPin = 13; // This led is flash
int led = 9;           // this led is fadeing
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by


word turnLedOffAfterTime = 100;

void setup()
{
  pinMode( ledPin, OUTPUT );
   
}

void loop()
{

  while ( analogRead( A0 ) < 630)
{
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );

while ( analogRead( A0 ) < 1023)


if ( analogRead( A0 ) > 630)
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 ;
  }

}
}


and the flash is not working like i want to work :(


did you want it to do something while this was (nearly always) true:

Code: [Select]
while ( analogRead( A0 ) < 1023)

Code: [Select]
while ( analogRead( A0 ) < 1023)
{

}

edgaryo

i put the {        }

and is same . look the code :(
Code: [Select]
byte ledPin = 13; // This led is flash
int led = 9;           // this led is fadeing
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by


word turnLedOffAfterTime = 100;

void setup()
{
  pinMode( ledPin, OUTPUT );
   
}

void loop()
{

  while ( analogRead( A0 ) < 630)
{
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );

while ( analogRead( A0 ) < 1023)
{

if ( analogRead( A0 ) > 630)
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 ;
  }
}
}
}

Grumpy_Mike

You are missing the point:-
Code: [Select]
while ( analogRead( A0 ) < 1023)
if ( analogRead( A0 ) > 630)
analogWrite(led, brightness);  

That while loop only ever end when the sensor gives a maximum reading, how likely is this?

You are better putting it in braces so you can see what is happening more easily.

Code: [Select]

while ( analogRead( A0 ) < 1023)
 {
   if ( analogRead( A0 ) > 630)
     {
       analogWrite(led, brightness);
      }
 }


edgaryo

Code: [Select]
void loop()
{

  while ( analogRead( A0 ) < 630)
{
  digitalWrite( ledPin, HIGH );

  delay( turnLedOffAfterTime );
  digitalWrite( ledPin, LOW );

while ( analogRead( A0 ) < 1023)
  {
    if ( analogRead( A0 ) > 630)
      {
        analogWrite(led, brightness);
     if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
     }
  }
}
}
}

and is not working .... why ? :|

and is same :| ...

Go Up