#include <Arduino.h>
// Define constants for pins
const int relayPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
const int sensorPin = 3;
const int buttonPin = 11;
const int pwmPin = 12; // Choose an unused pin that supports PWM
// Define criteria for each test
const int criteria[][2] = {
{0, 0}, // Test 1
{0, 0}, // Test 2
{0, 0}, // Test 3
{0, 0}, // Test 4
{0, 0} // Test 5
};
volatile int count = 0;
volatile bool testRunning = false;
// Function prototypes
void runTest(int testNum);
void activateRelays(int startIndex, int endIndex);
void deactivateAllRelays();
void sensorInterrupt();
void setup() {
// Initialize relay pins as output
for (int i = 0; i < 8; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW); // Ensure all relays are initially turned off
}
// Initialize button pin
pinMode(buttonPin, INPUT_PULLUP);
// Attach interrupt for sensor pin
attachInterrupt(digitalPinToInterrupt(sensorPin), sensorInterrupt, RISING);
pinMode(pwmPin, OUTPUT);
analogWrite(pwmPin, 1);
Serial.begin(9600);
}
void loop() {
// Wait for button press to start tests
while (digitalRead(buttonPin) == HIGH);
// Run all tests
bool allTestsPassed = true;
for (int i = 0; i < 5; i++) {
count = 0;
testRunning = true;
runTest(i);
testRunning = false;
if (digitalRead(buttonPin) == LOW) {
Serial.println("Tests stopped");
allTestsPassed = false;
break; // Exit loop if button is pressed
}
}
if (allTestsPassed) {
Serial.println("All tests passed");
}
// Turn off all relays after tests
deactivateAllRelays();
delay(1000); // Wait for 1 second before restarting tests
}
void runTest(int testNum) {
// Deactivate all relays before activating new ones
deactivateAllRelays();
// Activate relays based on test number
switch (testNum) {
case 0:
activateRelays(0, 7); // All relays
break;
case 1:
activateRelays(0, 3); // First 4 relays
break;
case 2:
activateRelays(4, 7); // Last 4 relays
break;
case 3:
activateRelays(1, 7); // All except first relay
break;
case 4:
activateRelays(0, 6); // All except last relay
break;
}
// Wait for test to complete
delay(5000);
// Calculate counts per minute
int cpm = count * 12; // (5 seconds * 12) = 60 seconds
// Print counts per minute and check against criteria
Serial.print("Test ");
Serial.print(testNum + 1);
Serial.print(": ");
Serial.println(cpm);
if (!(cpm >= criteria[testNum][0] && cpm <= criteria[testNum][1])) {
Serial.println("Test failed");
}
}
void activateRelays(int startIndex, int endIndex) {
for (int i = startIndex; i <= endIndex; i++) {
digitalWrite(relayPins[i], HIGH);
}
}
void deactivateAllRelays() {
for (int i = 0; i < 8; i++) {
digitalWrite(relayPins[i], LOW);
}
}
void sensorInterrupt() {
if (testRunning) {
count++;
}
}