Week 2

Week 2 Ultrasonic sensor and IR remote sensor in the real circuit

2.1 Ultrasonic

This week we successfully connected the motors and light-emitting diode in reality and achieved the additions functions of the opening door and LED decoration according to the software analogous circuit. Just for testing, we use a motor to represent the door. When people approach the motor will rotate to open the door and then close the door. Figure 1 shows the circuit connection and the video shows the testing.

                                                            Figure1 Motor connection


2.2 IR remote sensor 

In figure 2, we contact a digital clock and use IR remote and controller to control small LED lights shining in a programmed sequence.  When we use the controller and press button one, which is similar to scanning the room card. The digital light will display HELLO. Figure 2 shows the circuit connection and the video shows the testing.
       Figure3 7-segment display


Next week we plan to look into the PIR sensor and try to find the waveform change when people walk past the PIR sensor and use it to control the automatic door and try to program it to make it achieve better functions.

In figure 1, we test the ultrasound senor to drive the DC motor. The control code is shown below.
 
#define sensorpin A1
int EN1=A0;
int MC1=A3;
int MC2=A4;
const int RECV_PIN = A5;
int stopDistance = 0;
int i=0;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
  pinMode(triggerPin, OUTPUT);  // Clear the trigger
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echoPin, HIGH);
}

void setup()
{
  pinMode(A3,OUTPUT);
  pinMode(A4,OUTPUT);
  Serial.begin(9600);
  pinMode(sensorpin,INPUT);
  //Serial.begin(9600);
  pinMode(A0, OUTPUT);
}

void forward(int rate)//open the door
{
  digitalWrite(EN1,LOW);
  digitalWrite(MC1,HIGH);
  digitalWrite(MC2,LOW);
  analogWrite(EN1,rate);
}


void brake()//stop opening or closing the door
{
  digitalWrite(EN1,LOW);
  digitalWrite(MC1,LOW);
  digitalWrite(MC2,LOW);
  digitalWrite(EN1,LOW);
}

void back(int rate)//close the door
{
  digitalWrite(EN1,LOW);
  digitalWrite(MC1,LOW);
  digitalWrite(MC2,HIGH);
  analogWrite(EN1,rate);
}

void judgeDistance()
{
  int cm;
  int inches;
  // set distance to activate motors opening or closing the door
  stopDistance = 10;
  // measure the ping time in cm
  cm = 1.2528*0.01723 *1.64* readUltrasonicDistance(A2, A2);
  // convert to inches by dividing by 2.54
  inches = (cm / 2.54);
  Serial.print(cm);
  Serial.print("cm, ");
  Serial.print(inches);
  Serial.println("in");
  if (cm > stopDistance) {
    brake();
    delay(1000);
  }

  else {
    forward(255);
    delay(1000);
    back(255);
    delay(1000);
  }
}
  void loop()
  { 
    judgeDistance();

   }

In figure 2, we test IR remote sensor, the code is shown below

 #include <IRremote.hpp>

int RECV_PIN = 3;

IRrecv irrecv(RECV_PIN);

decode_results results;

#define sensorpin A1

int EN1=A0;

int MC1=A3;

int MC2=A4;

void setup(void)

{

  pinMode(13, OUTPUT);

  pinMode(12, OUTPUT);

  pinMode(11, OUTPUT);

  pinMode(10, OUTPUT);

  pinMode(9, OUTPUT);

  pinMode(8, OUTPUT);

  pinMode(7, OUTPUT);

  pinMode(6, OUTPUT);

  pinMode(A3,OUTPUT);

  pinMode(A4,OUTPUT);

  pinMode(sensorpin,INPUT);

  pinMode(A0, OUTPUT);

  Serial.begin(9600);

  // In case the interrupt driver crashes on setup, give a clue

  // to the user what's going on.

  Serial.println("Enabling IRin");

  irrecv.enableIRIn(); // Start the receiver

  Serial.println("Enabled IRin");

}

//My Functions

void h(void) {

  digitalWrite(13, HIGH);

  digitalWrite(12, HIGH);

  digitalWrite(11, LOW);

  digitalWrite(10, HIGH);

  digitalWrite(9, HIGH);

  digitalWrite(8, LOW);

  digitalWrite(7, HIGH);

  digitalWrite(6, LOW);

}

void e(void) {

  digitalWrite(13, HIGH);

  digitalWrite(12, HIGH);

  digitalWrite(11, HIGH);

  digitalWrite(10, LOW);

  digitalWrite(9, HIGH);

  digitalWrite(8, HIGH);

  digitalWrite(7, LOW);

  digitalWrite(6, LOW);

}

void l(void) {

  digitalWrite(13, LOW);

  digitalWrite(12, HIGH);

  digitalWrite(11, LOW);

  digitalWrite(10, LOW);

  digitalWrite(9, HIGH);

  digitalWrite(8, HIGH);

  digitalWrite(7, LOW);

  digitalWrite(6, LOW);

}

void o(void) {

  digitalWrite(13, LOW);

  digitalWrite(12, HIGH);

  digitalWrite(11, HIGH);

  digitalWrite(10, HIGH);

  digitalWrite(9, HIGH);

  digitalWrite(8, HIGH);

  digitalWrite(7, HIGH);

  digitalWrite(6, LOW);

}

void brake(){

  digitalWrite(13, LOW);

  digitalWrite(12, LOW);

  digitalWrite(11, LOW);

  digitalWrite(10, LOW);

  digitalWrite(9, LOW);

  digitalWrite(8, LOW);

  digitalWrite(7, LOW);

  digitalWrite(6, LOW);

}

void forward(int rate)//open the door

{

  digitalWrite(EN1,LOW);

  digitalWrite(MC1,HIGH);

  digitalWrite(MC2,LOW);

  analogWrite(EN1,rate);

}

void workout()//stop opening or closing the door

{

  digitalWrite(EN1,LOW);

  digitalWrite(MC1,LOW);

  digitalWrite(MC2,LOW);

  digitalWrite(EN1,LOW);

}

void back(int rate)//close the door

{

  digitalWrite(EN1,LOW);

  digitalWrite(MC1,LOW);

  digitalWrite(MC2,HIGH);

  analogWrite(EN1,rate);

}

// Start

void loop(void)

{

    if (irrecv.decode(&results)) {

    Serial.println(results.value, DEC);

    if (results.value != 0){

    forward(255);

    delay(1000);

    workout();

     h();

     delay(1000);

     e();

     delay(1000);

     l();

     delay(1000);

     l();

     delay(1000);

     o();

     delay(1000);

     brake();

     delay(1000);

    }

    back(255);

    delay(1000);

    workout();

    irrecv.resume(); // Receive thxt value

  }

}









                     
                                            





评论

此博客中的热门博文

Week 3

Week 1

Week 4