analogWrite not functioning in if statement

Hey y'all,

I'm working on a project for my studio class that's a sort of LED star pattern with different modes (controlled by an IR sensor) for blinking, waves, etc. So far, I've been able to make my counter work and cycle through the modes, and to make simple patterns using digitalWrite. I could stop there, but the general aim of the project is to be more calming than just twitchy lights, so I want to be able to make the LEDs fade on and off. I tried to copy the included Fade sketch into my counter 1 statment, but it doesn't even twitch on and off like my code was doing before I fixed the modes. I'm sure it's obvious, but what am I missing? Can I not use digitalWrite and analogWrite both?

int sensorPin = 0; //analog pin 0
int counter = 0; //counter
void setup(){
  //initialize sensor and
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  
}

void loop(){
  int brightness = 0;
  int fadeAmount = 5;
  int targetBright = 0;
  int val = analogRead(sensorPin);
  Serial.println(val);

  //just to slow down the output - remove if trying to catch an object passing by
  delay(1000);
  
  if (val >= 1000){
    
    counter++; 
    counter = counter%5;
  
  }
  
  Serial.println(counter);
  

if (counter == 1){
  // set the brightness of pin 9:
  analogWrite(2, 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 ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30); 
  
}

//some else ifs here for the counters 2 - 4, cut for length

else {
  //all off
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);

}

  
}

brightness is zero every time through loop. loop is essentially this...

void loop(){
  int val = analogRead(sensorPin);
  Serial.println(val);

  //just to slow down the output - remove if trying to catch an object passing by
  delay(1000);
  
  if (val >= 1000){
    
    counter++; 
    counter = counter%5;
  
  }
  
  Serial.println(counter);
  

if (counter == 1){
  // set the brightness of pin 9:
  analogWrite(2, 0);    

  // wait for 30 milliseconds to see the dimming effect    
  delay(30); 
  
}

//some else ifs here for the counters 2 - 4, cut for length

else {
  //all off
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);

}

  
}

...which is clearly not what you want. Does this bring any clarity... http://arduino.cc/en/Reference/scope

I...not really. I think I understand what you were saying about how it's basically resetting brightness every time it goes through the loop, but I don't understand how I'd go about fixing that.

... it's basically resetting brightness every time it goes through the loop ...

void loop(){
  int brightness = 0;

Yes it is. You could move it outside:

int brightness = 0;
void loop(){

Now it only does it once.

The analogWrite [u]is[/u] functioning inside the "if" statement BTW. It's just that you are not using it properly.

Oh, I see now. Thank you!

Final question...it only increases brightness when the sensor checks. Is there a way to get around that without completely altering the (simple) structure of my code? Or will it just have to be tied to that delay?

Er, you can restructure to not use delays. It's not that hard. Depends what you want.

Eh. The only reason the sensor has a delay is so that it won't jump several numbers ahead when you pass your hand over it. It looks alright when the fade is relatively slow, so I think it's alright. Thank you for your help. :open_mouth:

You can just not advance the number if the time now (from millis() ) has not advanced much (eg. by more than 500) from when you last noticed the hand.