1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | // Ultrasonic sensor (3-pin) const int ultrasonicPin = 11;
// Distance threshold in inches float distanceThreshold = 10;
// Duration for pulseIn long duration;
// Distance in inches float inches;
// Tracks whether the alarm is currently active bool alarmTriggered = false;
// RGB LED, buzzer, button const int redPin = 3; const int greenPin = 5; const int bluePin = 6;
const int buzzerPin = 7;
const int buttonPin = 9;
void setup() { Serial.begin(9600); // Set up pins pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); pinMode(buzzerPin, OUTPUT); pinMode(buttonPin, INPUT); Serial.println("System armed...waiting for object."); }
void loop() { // Send pulse to ultrasonic sensor pinMode(ultrasonicPin, OUTPUT); digitalWrite(ultrasonicPin, LOW); delayMicroseconds(5); digitalWrite(ultrasonicPin, HIGH); delayMicroseconds(10); digitalWrite(ultrasonicPin, LOW);
// Now wait for response pinMode(ultrasonicPin, INPUT); duration = pulseIn(ultrasonicPin, HIGH);
// Calculate distance in inches inches = (duration * 0.0133) / 2;
Serial.print("Distance: "); Serial.print(inches); Serial.println(" in");
if (!alarmTriggered) { // If alarm is not triggered, blink yellow while waiting blinkYellow();
// If object is within threshold, activate alarm if (inches < distanceThreshold) { Serial.println("Object Detected!"); alarmTriggered = true; } } else { // alarm IS triggered flashRed(); tone(buzzerPin, 1000); Serial.println("Alarm triggered.");
// Check if button is pressed to deactivate if (digitalRead(buttonPin) == HIGH) { noTone(buzzerPin); alarmTriggered = false; Serial.println("Alarm deactivated.");
// Wait until object moves away while (inches < distanceThreshold) { // Repeat pulse to check if it's gone pinMode(ultrasonicPin, OUTPUT); digitalWrite(ultrasonicPin, LOW); delayMicroseconds(5); digitalWrite(ultrasonicPin, HIGH); delayMicroseconds(10); digitalWrite(ultrasonicPin, LOW); pinMode(ultrasonicPin, INPUT); duration = pulseIn(ultrasonicPin, HIGH); inches = (duration * 0.0133) / 2;
delay(100); }
Serial.println("System re-armed.");
delay(1000); } } }
void setColor(int red, int green, int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); }
void blinkYellow() { setColor(255, 255, 0); delay(500); setColor(0, 0, 0); delay(500); }
void flashRed() { setColor(255, 0, 0); delay(200); setColor(0, 0, 0); delay(200); }
|