Pages: [1] 2 3 4   Go Down
Author Topic: Flash and Fade  (Read 789 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Lacey, Washington, USA
Offline Offline
Faraday Member
**
Karma: 164
Posts: 2575
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Steve Greenfield AE7HD
CET Consumer Electronics and Computer
Please don't read your attitudes into my messages

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to learn how to write the syntax of code first:-
Code:
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:
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to learn how to write the syntax of code first:-
Code:
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:
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 ;
  } 

}

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

}
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is this line supposed to do?
Code:
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 
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 smiley-sad
Logged

Offline Offline
Edison Member
*
Karma: 60
Posts: 1076
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 smiley-sad

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

Code:
while ( analogRead( A0 ) < 1023)

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

}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i put the {        }

and is same . look the code smiley-sad
Code:
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 ;
  }
}
}
}
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34726
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are missing the point:-
Code:
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:
while ( analogRead( A0 ) < 1023)
  {
    if ( analogRead( A0 ) > 630)
      {
        analogWrite(led, brightness);
       }
  }
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 ? smiley-neutral

and is same smiley-neutral ...
Logged

Pages: [1] 2 3 4   Go Up
Jump to: