// Motor state variables
const int off = HIGH;
const int on = LOW;
// Define pin connections
const int presenceSensorPin = A0;
const int vacuumPin = 2;
const int blowerPin = 3;
// Define variables to control timing
unsigned long presenceStartTime = 0;
const unsigned long vacuumDuration = 10000; // 30 seconds in milliseconds
const int distanceThreshold = 60; // Set distance threshold for human presence in centimeters
const float presenceThreshold = 0.70;
//Define variables for switch smoothing
const int numReadingsSwitch = 8; // Number of readings to average
int readingsSwitch[numReadingsSwitch];
const float SwitchThreshold = 0.72;
//Array to store readings
int indexSwitch = 0; // Index of current reading
int totalSwitch = 0; // Total of readings
// Define variables for presence smoothing
const int numReadingsPresence = 24; // Number of readings to average
bool readingsPresence[numReadingsPresence]; // Array to store readings
int indexPresence = 0; // Index of current reading
int totalPresence = 0; // Total of readings
if (presenceSwitched) { // Human presence detected
digitalWrite(blowerPin, off); // Turn off the blower
if (presenceUnder30) {
digitalWrite(vacuumPin, on); // Turn on the vacuum
} else {
digitalWrite(vacuumPin, off); // Turn off the vacuum
}
} else { // No human presence detected
digitalWrite(vacuumPin, off); // Turn off the vacuum
digitalWrite(blowerPin, on); // Turn on the blower
}
Serial.print(analogRead(A0));
Serial.print(" ");
Serial.print(presence);
Serial.print(" ");
Serial.println(presenceSmoothed);
}
// Function to convert the analog sensor value to distance in centimeters
int getDistance(int sensorValue) {
// The following equation is specific to the GP2Y0A41SK0F sensor
float distance = 2076.0 / (sensorValue - 11.0);
return (int)distance;
}
bool isPresenceUnder30(bool presenceSwitched) {
if (presenceSwitched) {
// Store the start time of presence
if (presenceStartTime == 0) {
presenceStartTime = millis();
}
// Check if the vacuum has been on for 30 seconds
if (millis() - presenceStartTime >= vacuumDuration) {
return false;
} else {
return true;
}
} else {
// Reset presence start time
presenceStartTime = 0;
}
}
// Function to check if a human is present based on the IR sensor reading
bool isHumanPresent() {
int presenceSensorValue = analogRead(presenceSensorPin);
int distance = getDistance(presenceSensorValue);
if (distance <= distanceThreshold) {
return true;
} else {
return false;
}
}
// Function to smooth the presence readings
bool smoothPresence(bool presence) {
// subtract the oldest reading
totalPresence = totalPresence - readingsPresence[indexPresence];
// Store the new reading in the array
readingsPresence[indexPresence] = presence;
// add new
totalPresence = totalPresence + readingsPresence[indexPresence];
// handle index
indexPresence = indexPresence + 1;
if (indexPresence >= numReadingsPresence) {
indexPresence = 0;
}
// Calculate the average of the readings
float avaPresence = (float) totalPresence / numReadingsPresence ;
// Check if the average presence value is above the threshold
if ((float) avaPresence >= (float)presenceThreshold) {
return true;
} else {
return false;
}
}
// Function to smooth the presence readings
bool smoothSwitch(bool smoothPresence) {
// subtract the oldest reading
totalSwitch = totalSwitch - readingsSwitch[indexSwitch];
// Store the new reading in the array
readingsSwitch[indexSwitch] = smoothPresence;
// add new
totalSwitch = totalSwitch + readingsSwitch[indexSwitch];
// handle index
indexSwitch = indexSwitch + 1;
if (indexSwitch >= numReadingsSwitch) {
indexSwitch = 0;
}
// Calculate the average of the readings
float avaSwitch = (float) totalSwitch/ numReadingsSwitch ;
// Check if the average presence value is above the threshold
if ((float) avaSwitch >= (float)SwitchThreshold) {
return true;
} else {
return false;
}
}
include
// Motor state variables const int off = HIGH; const int on = LOW;
// Define pin connections const int presenceSensorPin = A0; const int vacuumPin = 2; const int blowerPin = 3;
// Define variables to control timing unsigned long presenceStartTime = 0; const unsigned long vacuumDuration = 10000; // 30 seconds in milliseconds const int distanceThreshold = 60; // Set distance threshold for human presence in centimeters const float presenceThreshold = 0.70;
//Define variables for switch smoothing const int numReadingsSwitch = 8; // Number of readings to average int readingsSwitch[numReadingsSwitch]; const float SwitchThreshold = 0.72; //Array to store readings int indexSwitch = 0; // Index of current reading int totalSwitch = 0; // Total of readings
// Define variables for presence smoothing const int numReadingsPresence = 24; // Number of readings to average bool readingsPresence[numReadingsPresence]; // Array to store readings int indexPresence = 0; // Index of current reading int totalPresence = 0; // Total of readings
void setup() { Serial.begin(9600); // Initialize pins pinMode(presenceSensorPin, INPUT); pinMode(vacuumPin, OUTPUT); pinMode(blowerPin, OUTPUT);
// Make sure vacuum and blower are off at startup digitalWrite(vacuumPin, off); digitalWrite(blowerPin, off); }
void loop() { // Read presence sensor value bool presence = isHumanPresent(); bool presenceSmoothed = smoothPresence(presence); bool presenceSwitched = smoothSwitch(presenceSmoothed); bool presenceUnder30 = isPresenceUnder30(presenceSwitched);
if (presenceSwitched) { // Human presence detected digitalWrite(blowerPin, off); // Turn off the blower
} else { // No human presence detected digitalWrite(vacuumPin, off); // Turn off the vacuum digitalWrite(blowerPin, on); // Turn on the blower } Serial.print(analogRead(A0)); Serial.print(" "); Serial.print(presence); Serial.print(" "); Serial.println(presenceSmoothed); }
// Function to convert the analog sensor value to distance in centimeters int getDistance(int sensorValue) { // The following equation is specific to the GP2Y0A41SK0F sensor float distance = 2076.0 / (sensorValue - 11.0); return (int)distance; }
bool isPresenceUnder30(bool presenceSwitched) {
if (presenceSwitched) { // Store the start time of presence if (presenceStartTime == 0) { presenceStartTime = millis(); } // Check if the vacuum has been on for 30 seconds if (millis() - presenceStartTime >= vacuumDuration) { return false; } else { return true; } } else { // Reset presence start time presenceStartTime = 0; } }
// Function to check if a human is present based on the IR sensor reading bool isHumanPresent() { int presenceSensorValue = analogRead(presenceSensorPin); int distance = getDistance(presenceSensorValue); if (distance <= distanceThreshold) { return true; } else { return false; } }
// Function to smooth the presence readings bool smoothPresence(bool presence) { // subtract the oldest reading totalPresence = totalPresence - readingsPresence[indexPresence]; // Store the new reading in the array readingsPresence[indexPresence] = presence; // add new totalPresence = totalPresence + readingsPresence[indexPresence]; // handle index indexPresence = indexPresence + 1; if (indexPresence >= numReadingsPresence) { indexPresence = 0; }
// Calculate the average of the readings float avaPresence = (float) totalPresence / numReadingsPresence ;
// Check if the average presence value is above the threshold if ((float) avaPresence >= (float)presenceThreshold) { return true; } else { return false; } }
// Function to smooth the presence readings bool smoothSwitch(bool smoothPresence) { // subtract the oldest reading totalSwitch = totalSwitch - readingsSwitch[indexSwitch]; // Store the new reading in the array readingsSwitch[indexSwitch] = smoothPresence; // add new totalSwitch = totalSwitch + readingsSwitch[indexSwitch]; // handle index indexSwitch = indexSwitch + 1; if (indexSwitch >= numReadingsSwitch) { indexSwitch = 0; }
// Calculate the average of the readings float avaSwitch = (float) totalSwitch/ numReadingsSwitch ;
// Check if the average presence value is above the threshold if ((float) avaSwitch >= (float)SwitchThreshold) { return true; } else { return false; } }