// Demonstration potentiometers and RGB leds
// 9 May 2024, by Koepel, Public Domain
// Pins that support PWM to dim the leds
const int ledPins[3] = {6, 5, 3}; // pins of the R, G, B leds
const int potPins[3] = {A0, A1, A2};
void setup()
{
Serial.begin(115200);
Serial.println("The demonstration sketch has started.");
// Set the led pins as output
for(int i=0; i<3; i++)
{
pinMode(ledPins[i], OUTPUT);
}
// The analog pins don't need a pinMode().
}
void loop()
{
for(int i=0; i<3; i++)
{
int value = analogRead(potPins[i]);
Serial.print(value);
// The Arduino Uno has analog inputs of 10 bits,
// with a value from 0...1023.
// The analogWrite is only 0...255.
// The human eye sees the brightness
// with a curve close to 10log.
// scale to 0...1 to prepare for the exponential function
float t1 = float(value) / 1023.0;
// convert 0...1 to 1...10 with exponential scale with base 10
float t2 = pow( 10, t1);
// convert 1...10 to 0...255. Add 0.5 to 255 for rounding
float t3 = (255.5 / 9.0 * (t2 - 1.0));
int pwm = int(t3);
analogWrite(ledPins[i], pwm);
Serial.print(" (");
Serial.print(pwm);
Serial.print(")");
if(i != 2)
Serial.print(", ");
}
Serial.println();
delay(250);
}
RED
GREEN
BLUE