ONLY
ON……………………………………………………………………………………………………………………………………………………………………………
….
#include <Servo.h>
int sensor = A0; // KY-038 sound sensor module connected to analog
pin A0
boolean is_on = false; // Track system state (on/off)
unsigned long lastClapTime = 0; // To track the last clap detection
time
const int clapDebounceDelay = 500; // Minimum time between claps in
milliseconds
// Defines Trig and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;
const int buzzerPin = 9; // Buzzer pin
const int redLEDPin = 7; // Red LED pin (for object detection)
const int greenLEDPin = 6; // Green LED pin (indicates system on
and no object in range)
// Variables for the ultrasonic sensor
long duration;
int distance;
Servo myServo; // Servo object for controlling the motor
void setup() {
pinMode(sensor, INPUT); // Sound sensor
pinMode(trigPin, OUTPUT); // Ultrasonic sensor trigPin
pinMode(echoPin, INPUT); // Ultrasonic sensor echoPin
pinMode(buzzerPin, OUTPUT); // Buzzer
pinMode(redLEDPin, OUTPUT); // Red LED
pinMode(greenLEDPin, OUTPUT); // Green LED
// Ensure everything is OFF initially
digitalWrite(buzzerPin, HIGH); // Buzzer OFF
digitalWrite(redLEDPin, LOW); // Red LED OFF
digitalWrite(greenLEDPin, LOW); // Green LED OFF
myServo.detach(); // Motor off
Serial.begin(9600);
Serial.println("System ready. Waiting for clap...");
}
void loop() {
int data = analogRead(sensor); // Read from the sound sensor (now
using analogRead)
unsigned long currentTime = millis(); // Get the current time in
milliseconds
// Debugging: Print sound sensor data
Serial.print("Sound sensor data: ");
Serial.println(data);
// Detect if a clap is detected (if the analog value is above a
certain threshold)
if (data > 500 && (currentTime - lastClapTime >
clapDebounceDelay)) { // Adjust threshold as needed
lastClapTime = currentTime; // Update the time of the last clap
// Debugging: Clap detected
Serial.println("Clap detected!");
// Toggle the system state between on and off
if (is_on) {
turnOffSystem(); // If the system is on, turn it off
} else {
turnOnSystem(); // If the system is off, turn it on
}
// Add a short delay to avoid multiple triggers from one clap
delay(100);
}
// If system is ON, perform radar operations
if (is_on) {
// Servo motor movement from 0 to 180 degrees
for (int i = 0; i <= 180; i++) {
myServo.write(i);
delay(30);
distance = calculateDistance(); // Calculate distance
// Control LED and buzzer based on the distance
controlLEDAndBuzzer(distance);
Serial.print(i); // Print current degree
Serial.print(",");
Serial.print(distance); // Print distance
Serial.println(".");
}
// Servo motor movement from 180 to 0 degrees
for (int i = 180; i > 0; i--) {
myServo.write(i);
delay(30);
distance = calculateDistance(); // Calculate distance
// Control LED and buzzer based on the distance
controlLEDAndBuzzer(distance);
Serial.print(i); // Print current degree
Serial.print(",");
Serial.print(distance); // Print distance
Serial.println(".");
}
}
}
// Function to calculate distance using Ultrasonic sensor
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Get echo time in
microseconds
distance = duration * 0.034 / 2; // Convert time to distance
(cm)
return distance;
}
// Function to control LED and buzzer based on distance
void controlLEDAndBuzzer(int distance) {
if (distance > 0 && distance < 30) {
digitalWrite(buzzerPin, LOW); // Buzzer ON
digitalWrite(redLEDPin, HIGH); // Red LED ON (object
detected)
digitalWrite(greenLEDPin, LOW); // Green LED OFF
} else {
digitalWrite(buzzerPin, HIGH); // Buzzer OFF
digitalWrite(redLEDPin, LOW); // Red LED OFF
digitalWrite(greenLEDPin, HIGH); // Green LED ON (no object in
range)
}
}
// Function to turn the system OFF
void turnOffSystem() {
digitalWrite(buzzerPin, HIGH); // Buzzer OFF
digitalWrite(redLEDPin, LOW); // Red LED OFF
digitalWrite(greenLEDPin, LOW); // Green LED OFF
myServo.detach(); // Stop the motor
is_on = false; // Update system state
Serial.println("System OFF");
}
// Function to turn the system ON
void turnOnSystem() {
digitalWrite(greenLEDPin, HIGH); // Green LED ON (system is on,
no object in range)
myServo.attach(12); // Re-attach motor control
is_on = true; // Update system state
Serial.println("System ON");
}
ON AND OFF…………………………………………………………………………………………………………………………
#include <Servo.h>
int sensor = A0; // KY-038 sound sensor module connected to analog
pin A0
boolean is_on = false; // Track system state (on/off)
unsigned long lastClapTime = 0; // To track the last clap detection
time
const int clapDebounceDelay = 500; // Minimum time between claps in
milliseconds
unsigned long previousMillis = 0; // Time tracker for non-blocking
servo movement
const long interval = 30; // Time interval for smoother servo
rotation
// Defines Trig and Echo pins of the Ultrasonic Sensor
const int trigPin = 10;
const int echoPin = 11;
const int buzzerPin = 9; // Buzzer pin
const int redLEDPin = 7; // Red LED pin (for object detection)
const int greenLEDPin = 6; // Green LED pin (indicates system on
and no object in range)
// Variables for the ultrasonic sensor
long duration;
int distance;
int servoAngle = 0; // Track current servo angle
int step = 1; // Angle step for smooth rotation
Servo myServo; // Servo object for controlling the motor
void setup() {
pinMode(sensor, INPUT); // Sound sensor
pinMode(trigPin, OUTPUT); // Ultrasonic sensor trigPin
pinMode(echoPin, INPUT); // Ultrasonic sensor echoPin
pinMode(buzzerPin, OUTPUT); // Buzzer
pinMode(redLEDPin, OUTPUT); // Red LED
pinMode(greenLEDPin, OUTPUT); // Green LED
// Ensure everything is OFF initially
digitalWrite(buzzerPin, HIGH); // Buzzer OFF
digitalWrite(redLEDPin, LOW); // Red LED OFF
digitalWrite(greenLEDPin, LOW); // Green LED OFF
myServo.detach(); // Motor off
Serial.begin(9600);
Serial.println("System ready. Waiting for clap...");
}
void loop() {
// Check for clap
checkForClap();
// Perform radar operations if the system is ON
if (is_on) {
unsigned long currentMillis = millis();
// Non-blocking servo rotation with smooth, gradual movement
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Smooth rotation and continuous angle sweep between 0 and
180
myServo.write(servoAngle);
servoAngle += step;
// If the angle exceeds limits, reverse direction and ensure
servo doesn't get stuck
if (servoAngle >= 180 || servoAngle <= 0) {
step = -step; // Reverse direction smoothly
delay(5); // Short delay to prevent abrupt changes that
may cause sticking
}
// Calculate distance and send data to Processing
distance = calculateDistance();
controlLEDAndBuzzer(distance);
sendDataToProcessing(servoAngle, distance);
}
}
}
// Function to detect claps
void checkForClap() {
int data = analogRead(sensor); // Read from the sound sensor
(analogRead)
unsigned long currentTime = millis(); // Get current time in
milliseconds
if (data > 500 && (currentTime - lastClapTime >
clapDebounceDelay)) {
lastClapTime = currentTime; // Update time of last clap
Serial.println("Clap detected!");
if (is_on) {
turnOffSystem();
} else {
turnOnSystem();
}
delay(100); // Avoid multiple triggers from one clap
}
}
// Function to calculate distance using Ultrasonic sensor
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Get echo time in
microseconds
distance = duration * 0.034 / 2; // Convert time to distance
(cm)
return distance;
}
// Function to control LED and buzzer based on distance
void controlLEDAndBuzzer(int distance) {
if (distance > 0 && distance < 30) {
digitalWrite(buzzerPin, LOW); // Buzzer ON
digitalWrite(redLEDPin, HIGH); // Red LED ON (object
detected)
digitalWrite(greenLEDPin, LOW); // Green LED OFF
} else {
digitalWrite(buzzerPin, HIGH); // Buzzer OFF
digitalWrite(redLEDPin, LOW); // Red LED OFF
digitalWrite(greenLEDPin, HIGH); // Green LED ON (no object in
range)
}
}
// Function to turn the system OFF
void turnOffSystem() {
digitalWrite(buzzerPin, HIGH); // Buzzer OFF
digitalWrite(redLEDPin, LOW); // Red LED OFF
digitalWrite(greenLEDPin, LOW); // Green LED OFF
myServo.detach(); // Stop the motor
is_on = false; // Update system state
Serial.println("System OFF");
}
// Function to turn the system ON
void turnOnSystem() {
digitalWrite(greenLEDPin, HIGH); // Green LED ON (system is on,
no object in range)
myServo.attach(12); // Re-attach motor control
is_on = true; // Update system state
Serial.println("System ON");
}
// Function to send angle and distance data to Processing
void sendDataToProcessing(int angle, int distance) {
Serial.print(angle);
Serial.print(",");
Serial.print(distance);
Serial.println(".");
}