Flash and Fade

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

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 !

What's your boggle, fellow citizen?

Why not to use delay() for timing events:

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

polymorph:
What's your boggle, fellow citizen?

Why not to use delay() for timing events:

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

You have to learn how to write the syntax of code first:-

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

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.

Grumpy_Mike:
You have to learn how to write the syntax of code first:-

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

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

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 ;
}

}

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.

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 .

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

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 ;
  } 

}
}

What is this line supposed to do?

while ( analogRead( A0 ) < 1024 )

An analogue read will always be below 1024 because the biggest value is 1023.
What is supposed to execute here?

Grumpy_Mike:
What is this line supposed to do?

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

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 :frowning:

edgaryo:

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:

while ( analogRead( A0 ) < 1023)
while ( analogRead( A0 ) < 1023)
{

}

i put the { }

and is same . look the code :frowning:

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 ;
  } 
}
}
}

You are missing the point:-

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.

while ( analogRead( A0 ) < 1023)
  {
    if ( analogRead( A0 ) > 630)
      {
        analogWrite(led, brightness); 
       }
  }
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 ? :expressionless:

and is same :expressionless: ...

at this point, he may be better to go back to pseudo code:

it looks like you may want to fade up/down when your analog pin returns greater than 630, yes?

it looks also like you want to turn on a 2nd LED all the time but then only for a finite interval of time once the analog pin returns less than 630, yes?

it also looks like you want all of the LEDs off when the analog pin returns full voltage, or 1023, yes?

BulldogLowell:
at this point, he may be better to go back to pseudo code:

it looks like you may want to fade up/down when your analog pin returns greater than 630, yes?

it looks also like you want to turn on a 2nd LED all the time but then only for a finite interval of time once the analog pin returns less than 630, yes?

it also looks like you want all of the LEDs off when the analog pin returns full voltage, or 1023, yes?

yes the value of sensor is > 630 then all leds is off
when sensor value is < 630 , first led is flash , and another led is fadeing . But ONE TIME . After the loop is repet

And the first led is important to stay on 100 ms ( only flash , like a camera flash )

edgaryo:
And the first led is important to stay on 100 ms ( only flash , like a camera flash )

OK, then look at this:

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
int turnLedOffAfterTime = 100;

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

void loop()
{
  if ( analogRead( A0 ) < 630)
  {
    flashLedPinOnce();
    fadePinNine();
  }
  else 
  {
    allPinsOff();
  }
}

void flashLedPinOnce()
{
  
}

void fadePinNine()
{
  
}

void allPinsOff()
{
  
}

we are going to build off of this. Does it look OK? Do we need to add a condition regarding the high pin reading of A0?

BulldogLowell:

edgaryo:
And the first led is important to stay on 100 ms ( only flash , like a camera flash )

OK, then look at this:

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
int turnLedOffAfterTime = 100;

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

void loop()
{
  if ( analogRead( A0 ) < 630)
  {
    flashLedPinOnce();
    fadePinNine();
  }
  else
  {
    allPinsOff();
  }
}

void flashLedPinOnce()
{
 
}

void fadePinNine()
{
 
}

void allPinsOff()
{
 
}




we are going to build off of this. Does it look OK? Do we need to add a condition regarding the high pin reading of A0?

is very ok ! This is all wath i need :smiley: .