// For the Discord channel.
// It works more or less.
// The Arduino Uno seems to be too slow for this.
//
// See also the previous project:
// https://wokwi.com/projects/446448649602880513
//
// Interval in microseconds
unsigned long timerInterval = 2000UL;
unsigned long timings[3] = {0UL, 0UL, 0UL};
unsigned long previousMicros = 0;
const int ledPins[3] = {3, 5, 6};
const int potmeterPins[3] = {A0, A1, A2};
void setup()
{
Serial.begin(115200);
Serial.println(F("Software PWM"));
for (int i = 0; i < 3; i++)
pinMode(ledPins[i], OUTPUT);
}
void loop()
{
unsigned long currentMicros = micros();
unsigned long elapsedMicros = currentMicros - previousMicros;
if(elapsedMicros >= timerInterval)
{
// Time is up for this interval.
previousMicros = currentMicros;
// Set the new timings for the coming interval.
for(int i=0; i<3; i++)
{
int value = analogRead(potmeterPins[i]);
// Convert 0...1023 into 0...2000.
timings[i] = (unsigned long) value * 2000UL / 1023UL;
//Serial.print(i); Serial.print(", "); Serial.println(timings[i]);
}
// Turn on ledPins for the new interval.
// But keep the leds off that do not need
// to be turned on at all.
for (int i=0; i<3; i++)
if(timings[i] > 0)
digitalWrite(ledPins[i],HIGH);
}
else
{
// Check if a led has to be turned off.
for (int i=0; i<3; i++)
{
if(elapsedMicros >= timings[i])
{
digitalWrite(ledPins[i],LOW);
}
}
}
}