Untitled

 avatar
unknown
plain_text
a month ago
3.6 kB
4
Indexable
#include <Servo.h> //we're going to call the Servo Library we used earlier in the semester to make things a bit easier

// Define your Pins and key variables (pins on the Ultrasonic Sensor, the pin on your servo, the distance you want people to be detected at, etc.)
#define TRIG_PIN     9
#define ECHO_PIN     10
#define SERVO_PIN    6
/////////////////
#define FAN_PIN      5   // Digital pin connected to your fan's transistor/relay
#define DIRA         3  // direction 
#define DIRB         2
/////////////////
#define DETECTION_DISTANCE_CM  50   // Stop threshold (cm)
#define SWEEP_STEP_DELAY_MS    15    // Delay between each degree step (ms)
#define PING_INTERVAL_MS       50    // How often to ping the sensor (ms)

// Set global variables
Servo fanServo;

int   servoAngle     = 0;
int   sweepDirection = 1;          // 1 = forward, -1 = backward
bool  objectDetected = false;

unsigned long lastPingTime  = 0;
unsigned long lastStepTime  = 0;

// On Initialization (happens once on load)
void setup() {
  Serial.begin(9600);

  pinMode(TRIG_PIN, OUTPUT); // you set these definitions at the top (TRIG_PIN, ECHO_PIN, etc.)
  pinMode(ECHO_PIN, INPUT);
  pinMode(DIRA, OUTPUT);
  pinMode(DIRB, OUTPUT);
  digitalWrite (DIRA, LOW);
  digitalWrite (DIRB, HIGH);

//////////////////////////////////
  pinMode(FAN_PIN, OUTPUT);       // Fan pin as output
  digitalWrite(FAN_PIN, LOW);     // Fan off by default
//////////////////////////////////

  fanServo.attach(SERVO_PIN);
  fanServo.write(servoAngle);

  Serial.println("Fan sweep started."); // check your serial monitor for this and other messages — this will help with debugging
}


void loop() {
  unsigned long now = millis();

  // Step 1. Ping the ultrasonic sensor
  if (now - lastPingTime >= PING_INTERVAL_MS) {
    lastPingTime = now;
    long distance = getDistanceCM(); // this line calls 

    objectDetected = (distance > 0 && distance < DETECTION_DISTANCE_CM);
//////////////////////////////////
    digitalWrite(FAN_PIN, objectDetected ? HIGH : LOW);
//////////////////////////////////
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.print(" cm  |  Status: ");
    Serial.println(objectDetected ? "STOPPED (object detected)" : "SWEEPING");
  }

  // 2. Sweep the servo *only* when no object is detected
  if (!objectDetected && (now - lastStepTime >= SWEEP_STEP_DELAY_MS)) {
    lastStepTime = now;

    fanServo.write(servoAngle);
    servoAngle += sweepDirection;

    // If you're at the limit (either 0 or 180 degrees on the servo, reverse direction.
    if (servoAngle >= 180) {
      servoAngle    = 180;
      sweepDirection = -1;
    } else if (servoAngle <= 0) {
      servoAngle    = 0;
      sweepDirection = 1;
    }
  }
}

// Here is where you read your Ultrasonic Sensor by sending a pulse and returns the measured distance in cm.
// Returns -1 if the reading is out of range or invalid. (This happens a lot with our Ultrasonic Sensors)
long getDistanceCM() {
  // Send a 10µs trigger pulse (think of how a simple form of echolocation works)
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Measure echo duration (with a timeout after ~38ms/~650 cm — this is much further than the sensor can read)
  long duration = pulseIn(ECHO_PIN, HIGH, 38000);

  if (duration == 0) return -1;           // No echo / out of range

  return duration / 58;                   // Convert µs → cm (÷ 58)
}
Editor is loading...
Leave a Comment