JinhaKangLAB / Arudino-Codes

0 stars 0 forks source link

4.0 #9

Open JinhaKangLAB opened 1 year ago

JinhaKangLAB commented 1 year ago

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 = 20; // Set distance threshold for human presence in centimeters const float presenceThreshold = 0.75;

// Define variables for distance smoothing const int numReadingsDistance = 8; // Number of readings to average int readingsDistance[numReadingsDistance]; // Array to store readings int indexDistance = 0; // Index of current reading int totalDistance = 0; // Total of readings

// Define variables for presence smoothing const int numReadingsPresence = 8; // 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 presenceUnder30 = isPresenceUnder30();

if (presenceSmoothed) { // 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 } }

// 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 presence = isHumanPresent(); bool presenceSmoothed = smoothPresence(presence);

if (presenceSmoothed) { // 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); long avaDistance;

// Add new distance reading to the total and subtract the oldest reading totalDistance = totalDistance - readingsDistance[indexDistance]; // Store the new distance reading in the array readingsDistance[indexDistance] = distance; // add new totalDistance = totalDistance + readingsDistance[indexDistance];

// handle index indexDistance = indexDistance + 1; if (indexDistance >= numReadingsDistance) { indexDistance = 0; }

// Calculate the average of the distance readings avaDistance = totalDistance / numReadingsDistance; Serial.print("avaDistance"); Serial.println(avaDistance);

if (avaDistance <= 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 ;

Serial.print("avaPresence"); Serial.println(avaPresence);

// Check if the average presence value is above the threshold if ((float) avaPresence >= (float)presenceThreshold) { return true; } else { return false; }

}