1

I need help on a project. I just started to experiment with arduino 2 weeks ago and I am building a lamp that react to movement and interactions. So the idea is; when there is nobody in the room, the light stay white. And when there is someone, it turns orange and I want it to « breathe » (fade function).

I am using a LDR sensor to make it happen and so far my code looks like:

int LDRvalue = 1;
int RedLED = 9;
int GreenLED=10;
int BlueLED=11;
int pinLDR = A0;

void setup()
{
pinMode(RedLED, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(BlueLED, OUTPUT);
Serial.begin(9600);

}
void loop()
{
LDRvalue = analogRead(pinLDR);
Serial.print("valeur ldr: ");
Serial.println(LDRvalue);

int colour = map(LDRvalue, 0, 1023, 0, 255);
Serial.print("valeur mappe: ");
Serial.println(colour);


if(LDRvalue >= 1023)
{
analogWrite(RedLED, 255);
analogWrite(GreenLED, 255);
analogWrite(BlueLED, 255);
delay(1000);
}

else if((LDRvalue >= 800) & (LDRvalue < 1023))
{
analogWrite(RedLED, 255);
analogWrite(GreenLED, 255);
analogWrite(BlueLED, 255);
delay(1000);

}

else if((LDRvalue >= 600) & (LDRvalue < 800))
{
analogWrite(RedLED, 255);
analogWrite(GreenLED, 105);
analogWrite(BlueLED, 0);
delay(1000);

}
else if((LDRvalue >= 300) & (LDRvalue < 600))
{
analogWrite(RedLED, 255);
analogWrite(GreenLED, 35);
analogWrite(BlueLED, 0);
delay(1000);
}

else if((LDRvalue >= 150) & (LDRvalue < 300))
{
analogWrite(RedLED, 255);
analogWrite(GreenLED, 15);
analogWrite(BlueLED, 0);
delay(1000);

}

else if((LDRvalue >= 0) & (LDRvalue < 150))
{
analogWrite(RedLED, 255);
analogWrite(GreenLED, 0);
analogWrite(BlueLED, 0);
delay(1000);
}

delay(1);

}
void color(int red, int green, int blue)

{
analogWrite(RedLED, red);
analogWrite(GreenLED, green);
analogWrite(BlueLED, blue);
}

The code is working for the light changes depending on the human presence. But I don’t know how to make it fade and breathe when it turns orange.

Could you help me to make it happen ?

2
  • For one, you need to change the & in your if statements with &&. & is a bitwise AND, && is a logical AND. Which one of the if statements is orange? It seems like you have 3 different kinds of orange.
    – Gerben
    Mar 28, 2017 at 18:55
  • Thanks for the answer, I have 3 orange because the more there is a light change the more it turns orange to red. I forgot to talk about it. But for each of these orange, I need it to breathe, maybe more each time.
    – ghlln
    Mar 29, 2017 at 7:02

1 Answer 1

1

While some parts of your code don't make much sense to me, the breathing led code is easily searchable on the internet, with one of the first results being:

#include <math.h>
#define ledPin 11 
int i=0;
int breathe_delay = 15;   // delay between loops
unsigned long breathe_time = millis();
void setup() { }
void loop() {
  nonBlockingBreath();  // call the nonblocking function
  // yourOtherCodeHere();
}

void nonBlockingBreath() {
  if( (breathe_time + breathe_delay) < millis() ){
    breathe_time = millis();
    float val = (exp(sin(i/2000.0*PI*10)) - 0.36787944)*108.0; 
    // this is the math function recreating the effect
    analogWrite(ledPin, val);  // PWM
    i=i+1;
  }
}

As you can see the code above works for monochromatic LEDs only, so my advise is to combine the code above with a conversion from HSL to RGB, like RGBConverter library, specifically the hsvToRgb function.

This way your version of the nonBlockingBreath function will become:

byte[3] rgb;
void nonBlockingBreath() {
  if( (breathe_time + breathe_delay) < millis() ){
    breathe_time = millis();
    float value= (exp(sin(i/2000.0*PI*10)) - 0.36787944)*108.0; 
    hsvToRgb(hue, saturation, value, rgb);
    analogWrite(RedLED, rgb[0]);  // PWM
    analogWrite(GreenLED, rgb[1]);  // PWM
    analogWrite(BlueLED, rgb[2]);  // PWM
    i=i+1;
  }
}

Obviously you will have to set the hue and saturation variables accordingly to the tonality of orange you wish to breath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.