Hello,
I am trying to light up some LEDs using PWM to create a dimming effect from zero to full power and back within a specific range of time. These ranges of time are the classic ones of a camera and, to have them displayed correctly on a screen I have created a struct called exposure so to have both the numerical values in millisec and the corresponding text.
struct exposure{
long arduinoexp;
String displayexp;}
}
exposure[]{
{125, "1/8'"},
{167, "1/6'"},
{200, "1/5'"},
{250, "1/4'"},
{300, "0''3"},
{400, "0''4"},
{500, "0''5"},
{600, "0''6"},
{800, "0''8"},
{1000, "1''0"},
{1333, "1''3"},
{1666, "1''6"},
{2000, "2''0"},
{2500, "2''5"},
{3200, "3''2"},
{4000, "4''0"},
{5000, "5''0"},
{6000, "6''0"},
{8000, "8''0"},
{10000, "10''0"},
{13000, "13''0"},
{15000, "15''0"},
{20000, "20''0"},
{25000, "25''0"},
{30000, "30''0"},
};
I set the desidered exposure value with a rotary encoder and I created a specific counter (which in my code is counter 0 , associated with rotary encoder 0 , so to have the possibility to add other counters to other rotary encoders, may I have the necessity).
Then I created a function to recall when I need to switch on the LEDs with two for cycle from 0 to 255 and from 255 to 0 each time with a delay that varies accordingly to the selected exposure value.
void ledlights_regular(){
initial_time = millis();
for (int i = 0; i < 255; i++) {
analogWrite(Channel_1, i);
delay((float)exposure[counter[0]].arduinoexp/510.0);
}
for (int i = 255; i >= 0; i--) {
analogWrite(Channel_1, i);
delay((float)exposure[counter[0]].arduinoexp/510.0);
}
Serial.print("Duration: "); Serial.println(millis()-initial_time);
delay(500);
}
It seems to work fine with long exposure values:
Here are the test I ran (millisec values taken from the serial):
30 Secs --> Duration 29666 millisec
25 Secs --> Duration 25065 millisec
20 Secs --> Duration 19956 millisec
Then things start getting worse:
6 Secs --> Duration 5649 millisec
5 Secs --> Duration 4627 millisec
4 Secs --> Duration 3606 millisec
3.2 Secs --> Duration 3094 millisec
The problem arise when I lower the exposure values, it seems to be like some "rounding" occurs with certain blocks of values:
2 Secs --> Duration 1561 millisec
1.6 Secs --> Duration 1560 millisec
1.3 Secs --> Duration 1051 millisec
1 Sec --> Duration 541 millisec
0.8 Secs --> Duration 540 millisec
0.6 Sec --> Duration 541
0.5 Secs --> Duration 27 millisec
0.4 Secs --> Duration 27 millisec
0.3 Secs --> Duration 27 millisec
1/4 Secs --> Duration 26 millisec
1/5 Secs --> Duration 25 millisec
1/6 Secs --> Duration 25 millisec
1/8 Secs --> Duration 27 millisec
I've thought it must been something connected to the rounding which takes place here:
delay((float)exposure[counter[0]].arduinoexp/510.0);
So I tried to, instead of making the calculation each time in "delay", to create a float variable which would incorporate ther result prior to enter the for cycle like:
void ledlights_regular(){
float led_interval = (float)exposure[counter[0]].arduinoexp/510.0);
initial_time = millis();
for (int i = 0; i < 255; i++) {
analogWrite(Channel_1, i);
delay(led_interval);
}
for (int i = 255; i >= 0; i--) {
analogWrite(Channel_1, i);
delay(led_interval);
}
Serial.print("Duration: "); Serial.println(millis()-initial_time);
delay(500);
}
This would compile and upload but then arduino seems to freeze, first time it happens in my life...
If anyone has any suggestions on how to solve this it would be of great help!
Many many thanks,
Alessandro