// For the Discord channel.
// Not working yet.
// The Arduino Uno seems to be too slow for this.
// 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;
// Turn on all ledPins for the new interval.
for (int i=0; i<3; i++)
digitalWrite(ledPins[i],HIGH);
// 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]);
}
}
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);
}
}
}
}