Sunday, 25 December 2016

Shoe Fan with Arduino

Have ever heared people joking about smelly shoes?

Well, this the perfect way of keep your shoes stink free. Let me explain how it works. There is a ultrasonic sensor which measure the distance between itsefl and a object front of it (in this case the shoes). When someone places shoes front of it, it turns on the servo motor which brings the main motor to the correct position and then the main motor turns on for few seconds (you can make it longer if you want) and then goes back to the side so that shoes don't come in the way.
Ad

The sketch
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
int motor = 4;

int trigPin = 5;
int echoPin = 6;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  myservo.attach(7);  // attaches the servo on pin 7 to the servo object
  pinMode (motor,OUTPUT);
}

void firstsensor(){ // This function is for first sensor.
  int duration1, distance1;
  digitalWrite (trigPin, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin, LOW);
  duration1 = pulseIn (echoPin, HIGH);
  distance1 = (duration1/2) / 29.1;

      Serial.print("1st Sensor: ");
      Serial.print(distance1);
      Serial.print("cm    ");

  if (distance1 < 10) {  // Change the number for long or short distances.
//
  for (pos = 0; pos <= 95; pos += 1) { // goes from 0 degrees to 95 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 30ms for the servo to reach the position
  }
  digitalWrite (motor, HIGH);
      delay(10000);  // motor will keep rotating for 10 seconds
  digitalWrite (motor, LOW);    
    
  for (pos = 95; pos >= 0; pos -= 1) { // goes from 95 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 30ms for the servo to reach the position
  }
//
  } else {
   // nothing
  } 
}

void loop() {
Serial.println("\n");
firstsensor();
delay(1000); // takes the reading after 1 second
}

Ad

Note:
If you use the sketch above then the main motor will come back  after each 11 to 12 second that's because the ultra sonic sensor gets reading after 1 second and the motor runs for 10 seconds.

How can you make the motor run long time?
To do that simply change the green highlighted delay to what ever amount of time you want.

How can you make the cycle longer like once in 2 hours?
To do that you can change yellow highlighted daley to delay(7200000); that means that the ultrasonic sensor will take the reading after each 2 hours.

Let's see the code in action

Ad



No comments:

Post a Comment