fade down, STOP. fade up, STOP.

trying to make an led fade (no mapping) up then stop @ 255 (stay at HIGH), then when a sensor marker is hit, then fade down and 0 pwm and stay there.

 if (sensorVal =38 ){
  // fade in from min to max in increments of 5 points:
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
    // wait for 30 milliseconds to see the dimming effect    

   break;
  }
    delay(30); 
 
//break;    
  
 
  } else if (sensorVal =20 ){
    

  // fade out from max to min in increments of 5 points:
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);         
  
    
     break; 
  }
    delay(30);  
//break;
  }

Im new to C 'style' programming.. but why would you use the break; then a delay();?

couldnt you just use your delay INSIDE the loop? no breaking out of the loop (so to speak?)

Why do you break in the for loop? that makes it escape the for loop, and thus will not dim your LED.

Also, you try to make a statement with:

if (sensorVal =38 ) {

Please change that to:

if (sensorVal ==38 ){

Notice the double ==.

a single '=' is used to assign a value to a variable, e.g. cnt = 5; and cnt will be 5.
to make a statement, you have to use a double '==' so that it know it's a statement, and not an assignment:
if (sensorVal == 38) { etc

oops simple mistakes. but I'm still a bit confused, heres what I've reworked without much results.

 int sensorVal = cm;
  int threshold = 24;
  
 if (sensorVal <=38 && sensorVal >=25){

   for (int x = 0; x <= 255; x ++)
{
    analogWrite(ledPin, x);
  
    if (sensorVal < threshold){      // bail out on sensor detect
       x =255;
       break;
    }  
    delay(30);

 }}}
if(sensorVal < 24){
 for (int x = 255; x <= 0; x --)
 if (sensorVal < threshold){      // bail out on sensor detect
       x =0;
       break;
}
delay(30);

  
}}
}

could you please post all the code, this seems to be only the loop part..

and another thing, where do you assign your value to the sensorVal now? since i don't see any analogRead() somewhere, i might be missing something though..

If you want it to be checked every loop then you'll have to update it every loop, or the if-statements within the for loops will never get executed, since before the for loop you state that it is bigger than 25, so when you do not update it, it will never get smaller than that 25..

/*
 Fading
 
 This example shows how to fade an LED using the analogWrite() function.
 
 The circuit:
 * LED attached from digital pin 9 to ground.
 
 Created 1 Nov 2008
 By David A. Mellis
 modified 30 Aug 2011
 By Tom Igoe
 
 http://arduino.cc/en/Tutorial/Fading
 
 This example code is in the public domain.
 
 */

const int pingPin = 7;
int ledPin = 9;    // LED connected to digital pin 9
int fadeValue;
void setup()  { 
   Serial.begin(9600); // nothing happens in setup 
} 

void loop()  { 
  
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  int sensorVal = cm;
  int threshold = 24;
  
 if (sensorVal <=38 && sensorVal >=25){

   for (int x = 0; x <= 255; x ++)
{
    analogWrite(ledPin, x);
  
    if (sensorVal < threshold){      // bail out on sensor detect
       x =255;
       break;
    }  
    delay(30);
}
  
    for (int XX = 255; XX <= 0; XX --)
{
    analogWrite(ledPin, XX);
 
}
if (sensorVal <=24 && sensorVal >=2){
   
       XX =255;
       break;
    }  
    delay(30);
}
}
}


long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

just changed it a bit. its from a ping sensor.

if (sensorVal <=38 && sensorVal >=25){

So are testing to be in the range from 25 to 38. Looks odd written that way.

if(sensorVal < 24){
 for (int x = 255; x <= 0; x --) {  //<< need a bracket here
 if (sensorVal < threshold){      // bail out on sensor detect  << this is guaranteed to happen due to getting here by sensorVal <24 above
       x =0;
       break;  // this is not needed
}
delay(30);

  
}}

Simplify this to:

if(sensorVal < 24){
       x =0;
}
delay(30);

where would the for statement go then to fade up? and the delay seems to be screwing up my sensor readings.

rewrote this :

boolean compare = true; 
const int anPin = A5;
int ledPin = 9;    // LED connected to digital pin 9
int fadeValue;
void setup()  { 
   Serial.begin(9600); // nothing happens in setup 

  pinMode(anPin, INPUT);
} 

void loop()  { 
int   avRead = analogRead(anPin);    //11-25 values
 Serial.println(avRead);
 delay(100);
 


  
if(avRead > 15){
  if (compare = true){
 for (int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {  //<< need a bracket here
 analogWrite(ledPin, fadeValue);
 
delay(30);
 

 }
 compare = !compare;
 
 }
 else if (avRead< 15){
   if (compare = !compare){
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {  
   analogWrite(ledPin, fadeValue);
   delay(30);
}}}}}

You know, if you were to put each { and each } on its own line, and use Tools + Auto format, your code would be much easier to understand.

}}}}}

WTF?

id love some constructive feedback or samples instead of aggressive, non productive responses because its not a viable solution to someone who may not have as many skill sets as others.

danieljay:
id love some constructive feedback or samples instead of aggressive, non productive responses because its not a viable solution to someone who may not have as many skill sets as others.

that's perfectly said!

change those }}} into one } for each line.
split up your statements with tabs and other structurating things. that might be easier to understand :slight_smile:

const int anPin = A5;
int ledPin = 9;    // LED connected to digital pin 9
int fadeValue;
void setup()  { 
  Serial.begin(9600); // nothing happens in setup 

  pinMode(anPin, INPUT);
} 

void loop()  { 
  int   avRead = analogRead(anPin);    //11-25 values
  Serial.println(avRead);
  delay(100);




  if(avRead > 15){

    for (int fadeValue = 0 ; fadeValue <=255; fadeValue +=5) {  //<< need a bracket here
      analogWrite(ledPin, fadeValue);

      delay(30);


    }
  }


  else if (avRead< 15){

    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {  
      analogWrite(ledPin, fadeValue);
      delay(30);
    }
  }
}

nevermind the last post, i edited it completely

so you completely changed it, it seems.. well, tbh, i don't see any mistakes anymore now. but it still does not work?
what should happen when your value is 15? since now nothing will happen

ok so this fade sequence I'm conceptually looking at as an animation. this is whats obviously going on:

you go above 15, it triggers the fade to 255.

below, triggers fade to 0.

now my question: i want the led to reach end of the loop and STOP at the values for each statement. so, you go above 15, it triggers the fade "animation" to 255 and STOPS until you reverse and the same holds true other direction.

danieljay:
i want the led to reach end of the loop and STOP at the values for each statement. so, you go above 15, it triggers the fade "animation" to 255 and STOPS until you reverse and the same holds true other direction.

Now it will continuously check the value and adapt the led to it, but what do you mean with stop. Do you mean stopping within the for loop? and what do you mean with reverse?

I'm sorry for all these questions, but i don't really get the point of it xs i'm trying to understand it :slight_smile:

Argh, network crash, wasted my response!
Don't forget to declare ledPin as an output in setup.

I think you might be after this:

boolean compare = true; 
const int anPin = A5;
int ledPin = 9;    // LED connected to digital pin 9
int fadeValue;
void setup()  { 
  Serial.begin(9600); // nothing happens in setup 
  pinMode(anPin, INPUT);
  pinMode (ledPin, OUTPUT); // ADD THIS
} 

void loop()  { 
  int   avRead = analogRead(anPin);    //11-25 values
  Serial.println(avRead);
  delay(100);

  if(avRead > 15 && compare == true){ // fade up if avRead is >15 and fade down has run
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
      analogWrite(ledPin, fadeValue);
      delay(30);
    }
    compare = false;  // set compare for fade down when avRead goes 15 or lower next pass thru loop
  }
  if (avRead<=15 && compare == false ){
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {  
      analogWrite(ledPin, fadeValue);
      delay(30);
    }
    compare = true;
  }
} // end void loop

GREAT! thanks so much this is exactly what i needed. thanks so much.

Cool.
Just curious, what makes anPin change?