Fading LED with multiple sensors issues

alright…I have a working code for my multiple sensors (Parallax PIR and a PING):

int pin = 7;
unsigned long duration;
int ledPin = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int ledPin4 = 10;
int sensorValue = 0;
int val = 0;

void setup()
{
Serial.begin(9600);
pinMode(sensorValue, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
Serial.begin(9600);
}

void getAdistance(){
pinMode(pin, OUTPUT);
delay(500);
digitalWrite(pin, LOW);
delayMicroseconds(5);
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
duration = pulseIn(pin, HIGH);
}

void loop()
{
getAdistance();
Serial.println(duration, DEC);
val = duration;
if(duration < 2000)
{
digitalWrite(ledPin2, HIGH);
}
if(duration < 1000)
{
digitalWrite(ledPin3, HIGH);
}
if(duration < 500);
{
digitalWrite(ledPin4, HIGH);
}
{
delay(1000);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}

sensorValue = analogRead(0);
Serial.println(sensorValue, DEC);
delay(500);

if((sensorValue) == 0){
pinMode(0, INPUT);
digitalWrite(ledPin, LOW);
delay(5);
}

else if((sensorValue) > 1){
pinMode(0, INPUT);
digitalWrite(ledPin, HIGH);
delay(1000);
}
}

I also found a great fade/pulse code:

// Fading LED
// by Spiffed http://spiffie.org/;
// original by BARRAGAN http://people.interaction-ivrea.it/h.barragan;

int value = 0;
int rep = 0;
int ledpin = 9;
int count = 0; // light connected to digital pin 9

void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
}

void loop()
{
for(value = 4 ; value <= 250; value++) // fade in (from min to max)
{
for(rep=0;rep <= 10;rep++){
digitalWrite(ledpin, HIGH); // sets the value (range from 0 to 255)
delayMicroseconds(value); // waits for 30 milli seconds to see the dimming effect
digitalWrite(ledpin, LOW);
delayMicroseconds((255-value));
}
}
Serial.println(count);
count++;
for(value = 250; value >=4; value–) // fade out (from max to min)
{
for(rep=0;rep <= 10;rep++){
digitalWrite(ledpin, HIGH); // sets the value (range from 0 to 255)
delayMicroseconds(value); // waits for 30 milli seconds to see the dimming effect
digitalWrite(ledpin, LOW);
delayMicroseconds(255-value);
}
}
}

Now, I want to put those two together to make all my signals fade…but it’s not working, I get multiple errors and I’m starting to go crazy…any ideas anyone?!

int pin = 7;
unsigned long duration;
int rep = 0;
int ledPin = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int ledPin4 = 10;
int sensorValue = 0;
int val = 0;
int count = 0;
int value = 0;

void setup()
{
Serial.begin(9600);
pinMode(sensorValue, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
Serial.begin(9600);
}

void getAdistance(){
pinMode(pin, OUTPUT);
delay(500);
digitalWrite(pin, LOW);
delayMicroseconds(5);
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
duration = pulseIn(pin, HIGH);
}

void fade(){
{
for(value = 4 ; value <= 250; value++) // fade in (from min to max)
{
for(rep=0;rep <= 10;rep++){
digitalWrite(ledPin2, HIGH); // sets the value (range from 0 to 255)
delayMicroseconds(value); // waits for 30 milli seconds to see the dimming effect
digitalWrite(ledPin2, LOW);
delayMicroseconds((255-value));
}
}
Serial.println(count);
count++;
for(value = 250; value >=4; value–) // fade out (from max to min)
{
for(rep=0;rep <= 10;rep++){
digitalWrite(ledPin2, HIGH); // sets the value (range from 0 to 255)
delayMicroseconds(value); // waits for 30 milli seconds to see the dimming effect
digitalWrite(ledPin2, LOW);
delayMicroseconds(255-value);
}
}
}
}

void loop()
{
getAdistance();
Serial.println(duration, DEC);
val = duration;
if(duration < 2000)
{
digitalWrite(ledPin2, fade());
}
if(duration < 1000)
{
digitalWrite(ledPin3, fade());
}
if(duration < 500);
{
digitalWrite(ledPin4, fade());
}
{
delay(1000);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}

sensorValue = analogRead(0);
Serial.println(sensorValue, DEC);
delay(500);

if((sensorValue) == 0){
pinMode(0, INPUT);
digitalWrite(ledPin, LOW);
delay(5);
}

else if((sensorValue) > 1){
pinMode(0, INPUT);
digitalWrite(ledPin, fade());
delay(1000);
}
}

error: invalid use of void expression
In function ‘void loop()’:
error: invalid use of void expression At global scope:
In function ‘void setup()’:
In function ‘void loop()’:

I doubt this affects anything, but you have called “Serial.begin” twice.

You seem to have a couple of cases of redundant curly brackets, too, but it looks as though they match correctly. Now, the source of your problem is here:

if(duration < 2000)
{
digitalWrite(ledPin2, fade());
}
if(duration < 1000)
{
digitalWrite(ledPin3, fade());
}
if(duration < 500);
{
digitalWrite(ledPin4, fade());
}

In each of those cases where you call “digitalWrite”, you use the return value from the “fade” function. But “fade” is defined as a void function, with no return value. In fact, “fade” contains its own loop and “digital"write”, so it looks like it’s correctly declared void, but the call should look like this:

if(duration < 2000)
{
fade();
}
if(duration < 1000)
{
fade();
}
if(duration < 500);
{
fade();
}

However, that fails to take into account the different pins that you (seem to) want to fade. So maybe you need to declare “fade” with a single integer parameter that specifies the pin number, and the pass in a pin number when you call it.

BTW, it makes the code a little clearer if you use the “insert code” button when posting. It’s the one with the hash symbol “#” on it.

Anachrocomputer beat me to it:

void fade(int outPin)
{
      for(int value = 4; value <= 250; value++)
            for(int rep = 0; rep <= 10; rep++)
            {
                  digitalWrite(outPin, HIGH);  // sets the value (range from 0 to 255)
                  delayMicroseconds(value);  // waits for 30 milli seconds to see the dimming effect
                  digitalWrite(outPin, LOW);
                  delayMicroseconds((255 - value));
            }
      
}

if(duration < 2000)
      fade(ledPin2)
if(duration < 1000)
      fade(ledPin3);
if(duration < 500);
      fade(ledPin4);

Is it just me or is Tab broken?

this works perfectly! Thanks for all the advice!

int pin = 7;
unsigned long duration;
int rep = 0;
int ledPin = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int ledPin4 = 4;
int sensorValue = 0;
int val = 0;
int count = 0;
int value = 0;

void setup()
{
pinMode(sensorValue, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
Serial.begin(9600);
}

void getAdistance(){
pinMode(pin, OUTPUT);
delay(500);
digitalWrite(pin, LOW);
delayMicroseconds(5);
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin, LOW);
pinMode(pin, INPUT);
duration = pulseIn(pin, HIGH);
}

void fadeIn(int outPin)
{
for(int value = 4; value <= 250; value++)
for(int rep = 0; rep <= 10; rep++)
{
digitalWrite(outPin, HIGH);
delayMicroseconds(value);
}
}

void fadeOut(int outPin)
{
for(int value = 4; value <= 250; value++)
for(int rep = 0; rep <= 10; rep++)
{
digitalWrite(outPin, LOW);
delayMicroseconds(value);
}
}


void loop(){
{
getAdistance();
Serial.println(duration, DEC);
val = duration;
{
if(duration < 2000)
{
fadeIn(ledPin2);
}
if(duration < 1000)
{
fadeIn(ledPin3);
}
if(duration >= 2000)
{
fadeOut(ledPin2);
}
if(duration >= 1000)
{
fadeOut(ledPin3);
}
}
}

{
sensorValue = analogRead(0);
Serial.println(sensorValue, DEC);
delay(500);

if((sensorValue) == 0){
pinMode(0, INPUT);
digitalWrite(ledPin, LOW);
delay(5);
}

else if((sensorValue) > 1){
pinMode(0, INPUT);
fadeIn(ledPin);
delay(500);
}
}

{
sensorValue = analogRead(1);
Serial.println(sensorValue, DEC);
delay(500);

if((sensorValue) == 0){
pinMode(1, INPUT);
fadeOut(ledPin4);
delay(5);
}

else if((sensorValue) > 1){
pinMode(1, INPUT);
fadeIn(ledPin4);
delay(500);
}
}
}