Hi, I'm new to Arduino programming i tried to make the Arduino control two LEDs by gradually turning up the brightness of said LEDs to the desired amount and then gradually turning it down in a loop
if the speed of the two LEDs are the same the program works correctly but when one of the LEDs is half the speed of the other one they desynchronize after a bit of time is there a way to fix this?
const int whiteLed = 2;
const int blueLed = 3;
long prevWhiteTimeMillis = 0;
boolean whiteState = false;
long prevBlueTimeMillis = 0;
boolean blueState = false;
void setup() {
// put your setup code here, to run once:
pinMode(whiteLed, OUTPUT);
pinMode(blueLed, OUTPUT);
}
int timer = 0;
void loop() {
whiteLedControl();
blueLedControl();
}
int whiteLedBrightness = 0;
int whiteLedMaxBrightness = 50;
boolean whiteLedSwitch = false;
void whiteLedControl() {
if (millis() - prevWhiteTimeMillis >= 500 / 25) {
if (whiteLedBrightness >= whiteLedMaxBrightness) {
whiteLedSwitch = true;
}
if (whiteLedBrightness <= 1) {
whiteLedSwitch = false;
}
if (!whiteLedSwitch) {
if (whiteLedBrightness <= whiteLedMaxBrightness) {
whiteLedBrightness++;
} else {
whiteLedSwitch = true;
}
} else {
if (whiteLedBrightness >= 0) {
whiteLedBrightness--;
} else {
whiteLedSwitch = false;
}
}
prevWhiteTimeMillis = millis();
}
analogWrite(whiteLed, whiteLedBrightness);
}
int blueLedBrightness = 0;
int blueLedMaxBrightness = 50;
boolean blueLedSwitch = false;
void blueLedControl() {
if (millis() - prevBlueTimeMillis >= 500 / 50) {
if (blueLedBrightness >= blueLedMaxBrightness) {
blueLedSwitch = true;
}
if (blueLedBrightness <= 1) {
blueLedSwitch = false;
}
if (!blueLedSwitch) {
if (blueLedBrightness <= blueLedMaxBrightness) {
blueLedBrightness++;
} else {
blueLedSwitch = true;
}
} else {
if (blueLedBrightness >= 0) {
blueLedBrightness--;
} else {
blueLedSwitch = false;
}
}
prevBlueTimeMillis = millis();
}
analogWrite(blueLed, blueLedBrightness);
}
thanks to user @Delta_G the problem was in this line
prevBlueTimeMillis = millis();
because the millis() function does not run every millisecond exactly it causes problems
this is the code working now
const int whiteLed = 2;
const int blueLed = 3;
long prevWhiteTimeMillis = 0;
boolean whiteState = false;
long prevBlueTimeMillis = 0;
boolean blueState = false;
unsigned long currentMillis;
void setup() {
// put your setup code here, to run once:
pinMode(whiteLed, OUTPUT);
pinMode(blueLed, OUTPUT);
}
int timer = 0;
void loop() {
currentMillis = millis();
blueLedControl();
whiteLedControl();
}
int whiteLedBrightness = 0;
int whiteLedMaxBrightness = 50;
boolean whiteLedSwitch = false;
void whiteLedControl() {
if (currentMillis - prevWhiteTimeMillis >= 500 / 25) {
if (whiteLedBrightness >= whiteLedMaxBrightness) {
whiteLedSwitch = true;
}
if (whiteLedBrightness <= 1) {
whiteLedSwitch = false;
}
if (!whiteLedSwitch) {
if (whiteLedBrightness <= whiteLedMaxBrightness) {
whiteLedBrightness++;
} else {
whiteLedSwitch = true;
}
} else {
if (whiteLedBrightness >= 0) {
whiteLedBrightness--;
} else {
whiteLedSwitch = false;
}
}
prevWhiteTimeMillis += 500 / 25;
}
analogWrite(whiteLed, whiteLedBrightness);
}
int blueLedBrightness = 0;
int blueLedMaxBrightness = 50;
boolean blueLedSwitch = false;
void blueLedControl() {
if (currentMillis - prevBlueTimeMillis >= 500 / 50) {
if (blueLedBrightness >= blueLedMaxBrightness) {
blueLedSwitch = true;
}
if (blueLedBrightness <= 1) {
blueLedSwitch = false;
}
if (!blueLedSwitch) {
if (blueLedBrightness <= blueLedMaxBrightness) {
blueLedBrightness++;
} else {
blueLedSwitch = true;
}
} else {
if (blueLedBrightness >= 0) {
blueLedBrightness--;
} else {
blueLedSwitch = false;
}
}
prevBlueTimeMillis += 500 / 50;
}
analogWrite(blueLed, blueLedBrightness);
}
