It might not be causing a problem right now, but this function uses floating point maths.
uint8_t BrightnessFromPot() {
uint16_t potRead_b = analogRead(POT_PIN_BRIGHTNESS); // Reads analog value from potentiometer.
uint8_t brightness = map(potRead_b, 0, 1023, 60, 255); // Maps the 16-bit analog value to an 8-bit value.
uint8_t temp_bright_a = (1.0 / 10.0) * brightness; // Half of running average to stabilize jitter in the analog value.
uint8_t temp_bright_b = ((10.0 - 1.0) / 10.0) * brightnessAverage; // Other half
brightnessAverage = temp_bright_a + temp_bright_b; // Averaged brightness level over N=10 samples. (Thank you /u/stockvu!)
brightnessAverage_map = map(brightnessAverage, 60, 241, 65, 255); // Averaged values only go from 60 - 241 instead of 0 - 255, this re-maps them to 65-255
return brightnessAverage_map;
}
which is slow on almost all Arduino because it is done in software rather than in hardware, like it is on your PC/laptop's CPU.
Using floating point maths also brings in more code libraries, increasing the size of your code. Again, that's not really a problem at the moment.
Also, this function uses map() twice. The first time is to reduce a 16-bit integer value down to an 8-bit. Then it calculates a moving average before using map() a second time to get the required final range of values. This can also be simplified and made more efficient.
It would be easy to remove the use of floating point maths:
uint8_t BrightnessFromPot() {
uint16_t potRead_b = analogRead(POT_PIN_BRIGHTNESS); // Reads analog value from potentiometer.
brightnessAverage = (potRead_b + 9 * brightnessAverage) / 10; // Averaged brightness level over N=10 samples. (Thank you /u/stockvu!)
brightnessAverage_map = map(brightnessAverage, 0, 1023, 65, 255);
return brightnessAverage_map;
}
Does the above work ok?