const int inputPin = 2; // Input pin for the rising edge signal
const int outputPins[] = {3, 4, 5}; // Output pins for binary output
int counts[]={3,6,4}; // Array to store rising edge counts
volatile int currentIndex = 0; // Index to keep track of current count
volatile int risingEdgeCount = 0; // Counter for rising edge detections
void setup() {
Serial.begin(9600);
for (int i = 0; i < 4; i++) {
pinMode(outputPins[i], OUTPUT);
digitalWrite(outputPins[i], LOW); // Initialize output pins to LOW
}
pinMode(inputPin, INPUT);
attachInterrupt(digitalPinToInterrupt(inputPin), risingEdgeInterrupt, RISING);
//setOutput(currentIndex);
//currentIndex++;
}
void loop() {
// Read and store counts from serial input
if (Serial.available()) {
for (int i = 0; i < 3; i++) {
while (!Serial.available()) {
// Wait for input
}
counts[i] = Serial.parseInt();
}
currentIndex = 0;
risingEdgeCount = 0;
}
}
void risingEdgeInterrupt() {
risingEdgeCount++;
Serial.println(risingEdgeCount);
if (risingEdgeCount >= counts[currentIndex]) {
Serial.println("------------");
Serial.println(outputPins[currentIndex]);
Serial.println("------------");
setOutput(currentIndex);
risingEdgeCount = 0;
currentIndex++;
if (currentIndex >= 10) {
currentIndex = 0; // Reset index if exceeds array size
}
}
}
void setOutput(int index) {
for (int i = 0; i < 4; i++) {
digitalWrite(outputPins[i], i==index);
}
}