Untitled

 avatar
unknown
plain_text
a month ago
3.0 kB
2
Indexable
// Pin assignments
int Input = A0;          // Analog input pin connected to the sensor
int Buzzer = A1;         // Pin connected to the buzzer
int GreenLED = A2;       // Pin connected to the green LED
int KillSwitch = 4;      // Pin connected to the kill switch
int RedLEDs[10] = {5, 6, 7, 8, 9, 10, 11, 12, 13, A4}; // Array of pins for the 10 red LEDs

// Variables
int value;               // Variable to store sensor reading
int MAX = 600;           // Threshold value for the sensor reading

void setup() {
  // Initialize serial communication for debugging
  Serial.begin(9600);

  // Set up pins
  pinMode(Input, INPUT);            // Set the sensor pin as input
  pinMode(GreenLED, OUTPUT);        // Set the green LED pin as output
  pinMode(Buzzer, OUTPUT);          // Set the buzzer pin as output
  pinMode(KillSwitch, INPUT_PULLUP); // Set the kill switch pin as input with pull-up resistor

  // Set all 10 red LEDs as output
  for (int i = 0; i < 10; i++) {
    pinMode(RedLEDs[i], OUTPUT);
    digitalWrite(RedLEDs[i], LOW); // Ensure all LEDs are off at startup
  }
}

void loop() {
  // Check if the kill switch is activated
  if (digitalRead(KillSwitch) == LOW) {
    // Kill switch is activated, stop all operations
    stopAll();
    return; // Exit the loop early
  }

  // Read the sensor value
  value = analogRead(Input);

  // If the sensor reading is above the threshold
  if (value >= MAX) {
    // Turn off the green LED
    digitalWrite(GreenLED, LOW);

    // Activate the buzzer
    digitalWrite(Buzzer, HIGH);

    // Animate the 10 red LEDs in a "back and forth" pattern
    for (int k = 0; k < 10; k++) {
      lightUpLEDs(); // Call the animation function
      // Check the kill switch during the animation
      if (digitalRead(KillSwitch) == LOW) {
        stopAll();
        return; // Exit the loop early
      }
    }

    // Turn off the buzzer after animation
    digitalWrite(Buzzer, LOW);

    // Short delay before checking again
    delay(6000);
  } else {
    // If the sensor reading is below the threshold
    // Turn off the buzzer and red LEDs, and turn on the green LED
    digitalWrite(Buzzer, LOW);
    digitalWrite(GreenLED, HIGH);

    // Turn off all red LEDs
    for (int i = 0; i < 10; i++) {
      digitalWrite(RedLEDs[i], LOW);
    }
  }
}

// Function to animate the 10 red LEDs in a "back and forth" pattern
void lightUpLEDs() {
  // Light up LEDs from left to right
  for (int i = 0; i < 10; i++) {
    digitalWrite(RedLEDs[i], HIGH);
    delay(100); // Delay for smooth animation
    digitalWrite(RedLEDs[i], LOW);
  }

  // Light up LEDs from right to left
  for (int i = 9; i >= 0; i--) {
    digitalWrite(RedLEDs[i], HIGH);
    delay(100); // Delay for smooth animation
    digitalWrite(RedLEDs[i], LOW);
  }
}

// Function to stop all operations
void stopAll() {
  digitalWrite(Buzzer, LOW);
  digitalWrite(GreenLED, LOW);
  for (int i = 0; i < 10; i++) {
    digitalWrite(RedLEDs[i], LOW);
  }
  Serial.println("Kill switch activated. All operations stopped.");
}
Editor is loading...
Leave a Comment