#define rotpot1 A0
#define rotpot2 A1
#define duty_s 1
#define chaser_len 15
int prefix_pin = 15;
int prefix_time = 100;
int duty_cycle = 70;
int select = 0;
typedef struct Switch {
const int pin;
int duty_cycle;
unsigned long set_at;
bool state;
Switch(int pin) : pin(pin), duty_cycle(duty_cycle) {
pinMode(pin, OUTPUT);
};
void
flip(unsigned long timestamp) {
unsigned long td = timestamp - this->set_at;
if ( (this->state
&&
td > (duty_s * 10 * this->duty_cycle))
||
(!this->state
&&
td > (duty_s * 10 * (100 - this->duty_cycle)))
) {
this->state = !this->state;
this->set_at = timestamp;
if (this->state) {
digitalWrite(prefix_pin, HIGH);
delay(50);
digitalWrite(prefix_pin, LOW);
}
digitalWrite(this->pin, this->state);
}
}
} Switch;
Switch chaser[chaser_len] = {16, 17, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
void
setup() {
Serial.begin(9600);
pinMode(rotpot1, INPUT);
pinMode(rotpot2, INPUT);
pinMode(prefix_pin, OUTPUT);
}
void
updateValues(int* pduty_cycle, int* pselect) {
int select = map(analogRead(rotpot1), 0, 1023, 0, chaser_len+1);
int duty_cycle = map(analogRead(rotpot2), 0, 1023, 1, 100);
if (*pselect != select) {
Serial.print("selected ");
if (select == chaser_len) {
Serial.println("all switches");
} else {
Serial.print("switch #");
Serial.println(select);
}
*pselect = select;
}
if (*pduty_cycle != duty_cycle) {
*pduty_cycle = duty_cycle;
}
}
void
loop() {
updateValues(&duty_cycle, &select);
if (select == chaser_len+1)
prefix_time = duty_cycle * 10 * duty_s; // ???
for (int offset = 0; offset < chaser_len; offset++) {
if (offset == select || select == chaser_len)
chaser[offset].duty_cycle = duty_cycle;
chaser[offset].flip(millis());
}
}