0
votes

functionality:

Arduino make use of the following: 1. IR sensor 2. LED Light 3. MotorFan 4. Relay

hence, when user approaches the IR sensor, it will give a serialPrint of '1' since it has detected proximity, this will then toggle the state of LED and motorfan from LOW to HIGH. However, after 5s, the state of motorfan will toggle to LOW from HIGH, while the state of LED will still remain HIGH and will remain as HIGH as long as the serialPrint is '1'.

However, when user leaves the proximity of the IR sensor, the LED state will toggle to LOW from HIGH after 10s.

What I have done:

Code that I have done:

const int signalPin = 1; //wire pin to analog for IR Sensor 


//Motor-Fan Relay
byte FanRelay = 4;
byte LightRelay = 6;

int IRSignal; //variable signal, will hold the analog value read by Arduino


 long duration;
int distance;
unsigned long Timer;
unsigned long Interval = 10000; //teh repeat Interval
unsigned long MotorFanOff = 5000;

void setup() 
{
  //Execute only once at startup 

  //pinMode (FanPin , OUTPUT) ; // Set pinMode for FanPin as OUTPUT, display  
  pinMode (signalPin, INPUT);  //infared sensor line will be an input to the Arduino
  pinMode(FanRelay, OUTPUT);
  pinMode(LightRelay, OUTPUT);
   Serial.begin(9600); // Open serial port to communicate with the Ultrasaonic Sensor
}

void loop() 
{

  //execute multiple times in a loop

  IRSignal = analogRead(signalPin); //arduino reads the value from the infared sensor
  distance = 9462 / (IRSignal -16.92);
  if(distance < 30 && distance > 0)
  {
     Timer = millis(); 
     // Write a pin of HIGH
     Serial.println("1");
    //Set motor-fan to operate

    digitalWrite (FanRelay, HIGH);
    digitalWrite (LightRelay, HIGH);

     //After a delay of 5s, MotorFan will toggle to LOW
    //Toggle MotorFan to LOW after 5s
    if ((millis()-Timer)>MotorFanOff){

       digitalWrite (FanRelay, LOW);
    }
  }
  else
  {

     Serial.println("0");
     //Check if Timer is longer than 10s

     if ((millis()-Timer)>Interval){

        digitalWrite (LightRelay, LOW);
        digitalWrite (FanRelay, LOW);
     }

  }
  delay(1000);
}

Issue:

At this point, when tested, both the state of the LED and motorfan will toggle from LOW to HIGH when the serialPrint is '1' from the IR sensor. However, the main issue that i am facing is that the state of the MOTORFAN does not toggle to LOW from HIGH after 5seconds, but both state will only toggle to LOW when serialPrint is '0'.

Hence, what have I done wrong? pls help.thanks.

1

1 Answers

0
votes

Be careful! The first two pins on the Arduino are used for serial communication and break if you use them for something else. Switch over to higher pin numbers and the problem should go away.