/*

    This is a positive-edge trigger one-shot timer that has one input which when it goes high should:
    - Give 1 500ms pulse on PIN7
    - Delay 1s
    - Give 6 500ms pulses on PIN 6
    - Delay 1s
    - Give 1 1s pulse on PIN 3
    - Wait for the input to go high again (max wait 10s)
    - Give 1 500ms pulse on PIN 7
    - Delay 1s
    - Give 6 500ms pulses on PIN 5
    - Delay 1s
    - Give 1 1s pulse on PIN 3

    This code will run on AtTiny85

*/

#define PIN_3   4
#define PIN_5   0
#define PIN_6   1
#define PIN_7   2
#define PIN_IN  3

void setup() {

    // Set pin modes
    pinMode(PIN_5, OUTPUT);
    pinMode(PIN_6, OUTPUT);
    pinMode(PIN_7, OUTPUT);
    pinMode(PIN_3, OUTPUT);
    pinMode(PIN_IN, INPUT);

    // Set initial states
    digitalWrite(PIN_5, LOW);
    digitalWrite(PIN_6, LOW);
    digitalWrite(PIN_7, LOW);
    digitalWrite(PIN_3, LOW);

}

void loop() {

    wait_for_input();

    runOptionA();

    unsigned long waitStartMillis = millis();

    wait_for_input();

    if(millis() - waitStartMillis > 10000) {
        runOptionA();
    } else {
        runOptionB();
    }

}

void wait_for_input() {

    // Wait for input go from LOW to HIGH with debouncing
    int currentState = digitalRead(PIN_IN);
    int previousState = currentState;
    unsigned long debounceTime = 50; // Debounce time in milliseconds

    while (currentState == LOW) {
        currentState = digitalRead(PIN_IN);
        if (currentState != previousState) {
            delay(debounceTime);
            currentState = digitalRead(PIN_IN);
            if (currentState != previousState) {
                previousState = currentState;
            }
        }
        delay(1);
    }
    
}

void runOptionA() {

    // Give 1 500ms pulse on PIN7
    performPulse(PIN_7, 500);

    // Delay 1s
    delay(1000);

    // Give 6 500ms pulses on PIN 6
    for (int i = 0; i < 6; i++) {
        performPulse(PIN_6, 500);
        delay(500);
    }

    // Delay 1s
    delay(1000);

    // Give 1 1s pulse on PIN 3
    performPulse(PIN_3, 1000);

}

void runOptionB() {

    // Give 1 500ms pulse on PIN7
    performPulse(PIN_7, 500);

    // Delay 1s
    delay(1000);

    // Give 6 500ms pulses on PIN 5
    for (int i = 0; i < 6; i++) {
        performPulse(PIN_5, 500);
        delay(500);
    }

    // Delay 1s
    delay(1000);

    // Give 1 1s pulse on PIN 3
    performPulse(PIN_3, 1000);

}

void performPulse(int pin, unsigned long duration) {
    digitalWrite(pin, HIGH);
    delay(duration);
    digitalWrite(pin, LOW);
}
ATTINY8520PU