Knock sensor help

Hello guys.
I’m a design student and that’s my second experience with arduino. I manage to connect all the stuff I’m using in correct order but desperately need help with the code.

I’m using:

  1. Arduino duemilanove for prototyping (originally will use nano)
  2. Piezo buzzer as a sensor (plugged at analog pin 0)
  3. 10 LEDs ( pins from 4 to 13)

The effect I’m trying to achieve:
Knock the piezo buzzer → Leds fade in (in certain order 4 to 13) → delay (20) → Leds fade out → stop till the next knock

My Leds fade in/out in loop but even here not all Leds fading… some of them just turn on/off.
That’s the code I’m using.

int ledPin4 = 4;   
int ledPin5 = 5;
int ledPin6 = 6;   
int ledPin7 = 7;
int ledPin8 = 8;   
int ledPin9 = 9;
int ledPin10 = 10;   
int ledPin11 = 11;
int ledPin12 = 12;   
int ledPin13 = 13;



void setup()  { 
 
} 

void loop()  { 
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin4, fadeValue);         
    delay(20);                            
  } 
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin5, fadeValue);         
    delay(20);                            
  } 
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin6, fadeValue);         
    delay(20);                              
  } 
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin7, fadeValue);         
    delay(20);                              
  }  
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin8, fadeValue);         
    delay(20);                             
  } 
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin9, fadeValue);         
    delay(20);                              
  }  
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin10, fadeValue);         
    delay(20);                              
  } 
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin11, fadeValue);         
    delay(20);                             
  }  
  
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin12, fadeValue);         
    delay(20);                            
  } 
 
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { 
    analogWrite(ledPin13, fadeValue);         
    delay(20);                             
  } 



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

for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
   
    analogWrite(ledPin5, fadeValue);  
    delay(20);                              
  } 
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
 
    analogWrite(ledPin6, fadeValue);         
    delay(20);    
}

for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
   
    analogWrite(ledPin7, fadeValue);  
    delay(20);                              
  }   
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
 
    analogWrite(ledPin8, fadeValue);         
    delay(20);  
}

for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
   
    analogWrite(ledPin9, fadeValue);  
    delay(20);                              
  }   
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
 
    analogWrite(ledPin10, fadeValue);         
    delay(20);    
}

for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
   
    analogWrite(ledPin11, fadeValue);  
    delay(20);                              
  }   
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { 
 
    analogWrite(ledPin12, fadeValue);         
    delay(20);    
}

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

}

How to make all Leds fade in/out and add the code for piezo sensor?
Could someone please help me out with the code if possible. Thanks guys.

Please modify your post (there is a button on the right),
select the code
press the # button above the smileys

and your code will look much better!

Learn about arrays - http://arduino.cc/en/Tutorial/Array - http://arduino.cc/it/Reference/Array -

That will make your code 5x smaller

sanulchik:
... but even here not all Leds fading... some of them just turn on/off.

Check out the documentation for analogWrite. In particular this statement:

... this function works on pins 3, 5, 6, 9, 10, and 11.

Thanks for reply.

... this function works on pins 3, 5, 6, 9, 10, and 11.

Is there any chance to make it work on all pins (4-13)?

Do you want them to fade together or individually?

You could make a loop that turns them on and off. The looping is pretty fast, and if you do it fast enough you won't notice any flicker.