Fading LEDs in and out on a sequence

Hi there,

I have made an interactive light sculpture which can be seen here: http://www.aphrashemza.com/composition-x.html. I am installing it at an event later today and have just realised that for this event running the sculpture through a sensor will not work as it is going to be too busy, so I need to alter my code so that the LEDs just fade in and out on a loop instead of taking readings from the sensor and fading in/out accordingly.

My original code is as follows:

float maxValue = 200;//200 refers to led max brightness turn down less bright
float current5 = 0;
float current6 = 0;

int readDistance()
{
  long pulse = pulseIn(4, HIGH);
  return pulse / 58;
}


void setup()
{
  analogWrite(5, current5);
  analogWrite(6, current6);
     
  Serial.begin(9600);          // start serial communication at 9600bps
}



void loop()
{
  float reading = readDistance();
  
  if (reading < 700)
  {
    if (reading > 150)
    {
      current5 = 0.4 * current5;
      current6 = 0.4 * current6;
      
      // turn both off
      analogWrite(5, current5);
      analogWrite(6, current6);
    }
    else if (reading > 75)
    {
      // turn 6 off
      current6 = 0.4 * current6;
      analogWrite(6, current6);
    
      // dim 5
      int brightness = map(reading, 75, 150, maxValue, 0);
      current5 = 0.7 * current5 + 0.3 * brightness;
      analogWrite(5, current5);
    }
    else
    {
      // turn 5 on
      current5 = 0.7 * current5 + 0.3 * maxValue;
      analogWrite(5, current5);
      
      // dim 6
      int brightness = map(reading, 0, 75, maxValue, 0);
      current6 = 0.7 * current6 + 0.3 * brightness;
      analogWrite(6, current6);
    }
  }
    
  Serial.println(reading);
}

I would like my code to now do the following:

-define a maxvalue for the brightest stage the leds can go
-set both leds to 0 at beggining

  • Start with pin 5 and fade up to maxvalue
  • Then when pin 5 is at maximun brightness to keep that on full and the fade in pin 6 to maxvalue
  • When both pin 5 and 6 are at maximum brightness, fade out pin 6 to min value
  • When that is at value 0 fade out pin 5 to value 0
  • repeat sequence forever.

This is what I have been working on but it is not quite right! I though if I can do in two sections and put a delay on one then this would create the above sketch.

For example tell 5 to fade in and out and tell 6 at the same time but delay until it starts halfway through 5’s sequence.

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 */
#define triangle 5
#define square 6

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 5 to be an output:
  pinMode(triangle, OUTPUT);
  pinMode(square, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 5:
  analogWrite(triangle, 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);                            
  
 // set the brightness of pin 6:
  analogWrite(square, 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(100);                            
}

If anyone can help I would really appreciate it.

Thanks

Hi all,

I have been working on this code below in reference to my queries above I think if I tell the arduino to fade in and out pin 5 and delay then fade in/out pin 6 and use a longer delay then this should produce the desired affect which I can test with the sculpture itself to get the desired delay and brightness.

However I am getting an error message when i load this code up

see code

#define triangle 5
#define square 6

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 5 to be an output:
  pinMode(triangle, OUTPUT);
  pinMode(square, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin 5:
  analogWrite(triangle, 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);                            

{  
 // set the brightness of pin 6:
  analogWrite(square, 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 100 milliseconds to see the dimming effect    
  delay(100);  
  }  
}

error message:

In file included from /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Arduino.h:6, from sketch_oct11b.ino:12: /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/include/math.h:323: error: expected unqualified-id before numeric constant

Does anyone know what this means?

Thanks

The error is caused by your use of the reserved words triangle and square. Change them to myTriangle and mySquare and the code compiles. Whether it does what you want is a different matter.

The description of what you want describes four states which would lend itself to implementation as a finite state machine. Here is some code that may or may not work. I cannot test it as present and there may well be things wrong with it. It does, however, compile, which is a start. If it does not work and you cannot work out what is wrong then post here and you will get some more help.

const byte myTriangle = 5;
const byte mySquare = 6;
byte brightness = 0;
const byte fadeAmount = 5;
const byte fadeDelay = 30;
byte state = 0;
const byte maxBrightness = 255;

void setup() 
{
  pinMode(myTriangle, OUTPUT);
  pinMode(mySquare, OUTPUT);
  analogWrite(myTriangle, 0);
  analogWrite(mySquare, 0);
}

void loop() 
{
  switch (state)
  {
  case 0:    //ramp up the triangle lamp
    analogWrite(myTriangle, brightness);
    delay(fadeDelay);
    brightness = brightness + fadeAmount;
    if (brightness >= maxBrightness)
    {
      analogWrite(myTriangle, maxBrightness);
      state = 1;
      brightness = 0;
    }
    break;

  case 1:    //ramp up the square lamp
    analogWrite(mySquare, brightness);
    delay(fadeDelay);
    brightness = brightness + fadeAmount;
    if (brightness >= maxBrightness)
    {
      analogWrite(mySquare, maxBrightness);      
      state = 2;
      brightness = maxBrightness;
    }
    break;

  case 2:    //ramp down the square lamp
    analogWrite(mySquare, brightness);
    delay(fadeDelay);
    brightness = brightness - fadeAmount;
    if (brightness <= 0)
    {
      analogWrite(mySquare, 0);      
      state = 3;
      brightness = maxBrightness;
    }
    break;

  case 3:    //ramp down the triangle lamp
    analogWrite(myTriangle, brightness);
    delay(fadeDelay);
    brightness = brightness - fadeAmount;
    if (brightness <= 0)
    {
      analogWrite(myTriangle, 0);      
      state = 0;
      brightness = maxBrightness;
    }
    break;
  }
}