/*============ Configuration Parameters ============*/
#define BREATHE_IN_TIME (1000) /*Time taken in brightening in ms*/
#define BREATHE_OUT_TIME (1000) /*Time taken in fading in ms*/
#define PAUSE_AT_MAX_TIME (1000) /*Waiting time at the maximum brightness*/
#define PAUSE_AT_MIN_TIME (1000) /*Waiting time at the minimum brightness*/
#define MAX_BRIGHTNESS (255.0) /*Max analog output (PWM) = 255 for Arduino Uno*/
#define RED_PIN 3 /*Red pin connection*/
#define GREEN_PIN 5 /*Green pin connection*/
#define BLUE_PIN 6 /*Blue pin connection*/
#define COLOR ((unsigned long int)(0xeeff00)) /*Color in hexa format (R,G,B)*/
/*=============================================================================*/
/*=============================================================================*/
/*Gaussian curve settings*/
float gamma = 0.13; // affects the width of peak (more or less darkness)
float beta = 0.5; // shifts the gaussian to be symmetric
/*============ End of Configuration ============*/
/*Global arrays*/
float rgb[3] = {COLOR>>16&0xFF,COLOR>>8&0xFF,COLOR&0xFF}; /*Global array holds each color in an index*/
float rgbMod[3] = {0}; /*array for modified values*/
/*APIs declarations*/
void adjustRGBarr();
void writetoRGB(float pwm_val);
/*Arduino setup function*/
void setup() {
Serial.begin(9600); /*for debugging purpose*/
/*Setting the RGB pins as output*/
pinMode(RED_PIN,OUTPUT);
pinMode(GREEN_PIN,OUTPUT);
pinMode(BLUE_PIN,OUTPUT);
/*Normalizing the RGB array*/
adjustRGBarr();
}/*API*/
/*Arduino Loop Function*/
void loop() {
float pwm_val = 0;
float start = millis(); /*for time debugging purpose*/
/*============ Breathing In period ============*/
for (int i=0;i<(BREATHE_IN_TIME);i++){ /*each loop takes 5 millis*/
/*starting from the first half of the curve*/
pwm_val = (float)MAX_BRIGHTNESS*(exp(-(pow(((i/((float)((BREATHE_IN_TIME*2.0))))-beta)/gamma,2.0))/2.0));
writetoRGB(pwm_val); /*write adjacent gaussian value to the pins*/
delay(1);
}/*for*/
/*
/*============ Pause period ============*/
for (int i=0;i<(PAUSE_AT_MAX_TIME);i++){
writetoRGB(pwm_val);
delay(1);
}/*for*/
/*============ Breathing out period ============*/
for (int i=((BREATHE_OUT_TIME));i<((BREATHE_OUT_TIME*2));i++){ /*start from the second half*/
pwm_val = (float)MAX_BRIGHTNESS*(exp(-(pow(((i/((float)((BREATHE_OUT_TIME*2.0))))-beta)/gamma,2.0))/2.0));
writetoRGB(pwm_val);
delay(1);
}/*for*/
/*============ Pause period ============*/
for (int i=0;i<(PAUSE_AT_MIN_TIME);i++){
writetoRGB(pwm_val);
delay(1);
}/*for*/
Serial.println("s");
}/*API*/
/*API
*Used to normalize the RGB values
*to values less than 1
*We use it to multiply with any pwm value
*to save the color degree.
**/
void adjustRGBarr(){
float max_n = rgb[0]; /*Holds maximum value of the rgb array*/
for(int i = 0; i < 3; i++) { /*finding max element*/
if((int)rgb[i] > (int)max_n)
max_n = rgb[i];
}/*for*/
if (max_n !=0)
{
for(int i=0;i<3;i++){ /*normalizing rgb for all elements to be less than 1*/
rgb[i] = (float)rgb[i] / (float)max_n;
}/*for*/
}/*if*/
else{ /*if all elements are zeros*/
for(int i=0;i<3;i++){ /*normalizing rgb for all elements to be less than 1*/
rgb[i] = 1.0; /*set normalizing array to ones*/
}/*for*/
}/*else*/
}/*API*/
/*
*Function to write values to the RGB pins
*We pass the pwm_value we want to apply to
*the RGB and then it get multiplied with
*the normalizing array to maintain the same
*color gradient
*/
void writetoRGB(float pwm_val){
for(int i = 0;i<3;i++){
rgbMod[i] = rgb[i]*pwm_val; /*multiplying to normalizing array*/
}/*for*/
/*output modified array to the pins*/
analogWrite(RED_PIN,255-(int) rgbMod[0]);
analogWrite(GREEN_PIN,255-(int)rgbMod[1]);
analogWrite(BLUE_PIN,255-(int) rgbMod[2]);
}/*API*/