#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))
byte dataPin = 2;
byte clockPin = 4;
byte latchPin = 3;
int potPin[] = {A0, A1, A2, A3, A4, A5};
int potValue;
// timing in microseconds
unsigned long timerInterval = 2000;
unsigned long previousMicros = 0;
uint8_t data = 0;
uint16_t pulseWidths[] = { 200, 400, 600, 800, 1000, 1200, 2000 };
void setup() {
Serial.begin(115200);
Serial.println(F("Software PWM"));
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
unsigned long currentMicros = micros();
if (currentMicros - previousMicros >= timerInterval) {
previousMicros = currentMicros;
for (int i = 0; i < NUMELEMENTS(potPin); i++)
{
potValue = analogRead(potPin[i]);
int value = map(potValue, 0, 1023, 0, 2000);
pulseWidths[i] = value;
if (pulseWidths[i] > 0) {
data |= 1 << i;
}
}
} else {
for (int i = 0; i < NUMELEMENTS(potPin); i++) {
if (currentMicros - previousMicros >= pulseWidths[i]) {
data &= ~(1 << i);
}
}
}
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, data << 1);
digitalWrite(latchPin, HIGH);
}