#include <Arduino.h>
#include <stack> // Include stack library
std::stack<int> st; // Define stack
void setup() {
Serial.begin(6000);
// Push devices onto the stack (Last In, First Out)
int pins[] = {13, 12, 33, 14, 25};
for (int i = 0; i < 5; i++) {
pinMode(pins[i], OUTPUT);
digitalWrite(pins[i], LOW); // Ensure all are OFF
st.push(pins[i]); // Push to stack
}
}
void loop() {
if (!st.empty()) {
int device = st.top(); // Get the top device
st.pop(); // Remove it from stack
Serial.print("Activating device: ");
Serial.println(device);
digitalWrite(device, HIGH); // Activate
// delay(1000); // Simulate operation
Serial.print("Deactivating device: ");
Serial.println(device);
digitalWrite(device, LOW); // Deactivate
} else {
Serial.println("All devices processed. Restarting...");
// delay(2000);
// Refill stack for next cycle
int pins[] = {13, 12, 33 , 14, 25};
for (int i = 0; i < 5; i++) {
st.push(pins[i]);
}
}
}