Hello everybody i'm trying to code a program that change the color of the led from blue to red,
But i want to make let's say a 3 second where the red and blue stay togheter with same PMW, so i can hold purple color for some time i can choose.
i expressed r as percentage of PMW of redValue
and b as percentage of PMW of blueValue
i did this for two reasons:
(1) because i set an if statement and expressing PMW as percentage it gives the if statement "much grip" (when the if was expressed in PMW it gave me redValue = 122 and blueValue = 123 and this didn't work for the if statement).
(2)because i really wanted to monitor the percentage of the colors changing in Serial.plotter as percentage
here's my code, but it seems it doesn't work the Do-While part
//RGB blue, purple, red
int eventInterval = 5000;
unsigned long previousTime = 0;
//using serial plotter
// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6
void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, LOW);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
Serial.begin(9600);
}
// define variables
int redValue;
int greenValue;
int blueValue;
int k;
int r;
int g;
int b;
int v;
// main loop
void loop()
{
#define delayTime 50 // fading time between colors
redValue = 0; // choose a value between 1 and 255 to change the color.
greenValue = 255;
blueValue = 0;
k = 0;
r = 0;
g = 0;
b = 0;
v = 0;
for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
{
//char data[50];
//sprintf(data, "PWM Red is: %d, Red is: %u %%", k, r);
//così facendo possiamo vedere l'andamento del blue e del rosso in termini di PWM espressi in percentuale
char plot[100];
sprintf(plot, "%d,%u", b, r);
Serial.println(plot);
redValue += 1;
blueValue -= 1;
analogWrite(RED, redValue); //PWM
analogWrite(BLUE, blueValue);
delay(delayTime);
k = k+1;
r = (k*100/255);
b = 100 - (k*100/255);
if (r == b) {
//Updates frequently//-- It starts to count//
unsigned long currentTime = millis();
do {
analogWrite(RED, redValue); //PWM
analogWrite(BLUE, blueValue);
Serial.println(plot);
previousTime = currentTime;
} while (previousTime < eventInterval);
//Update the timing fore the next time around //
}
}
}
I tried another way to achieve what i want: i used another for loop inside the if statement and it works, but it's not what i wanted (inside the for loops i can't choose the seconds for hold the purple color).
here's the part of code with this variation
if (r == b) {
//Updates frequently//-- It starts to count//
unsigned long currentTime = millis();
for (v ;v < eventInterval; v++) {
analogWrite(RED, redValue); //PWM
analogWrite(BLUE, blueValue);
Serial.println(plot);
}
//Update the timing fore the next time around //
}
How can i set a timer for keeping purple color? Thank you