0

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.

Luke
  • 982
  • 1
  • 7
  • 27

1 Answers1

0

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.

PKG
  • 1
  • higher pin number? are you referring to the digital pin of the arduino?? THose 2 pin numbers are used from the relay and it shouldn't matter – Luke Oct 21 '16 at 15:03
  • I quickly checked with my Micro board (might not necessarily correspond to your board) but here, pins 0 and 1 are used for serial communication. You wrote that signalPin is using pin 1. That certainly is one of the Serial pins. Switch those to another pin and it should work (if the code is correct, I did not check that yet). – PKG Oct 21 '16 at 16:36
  • Ohh, but I am using analog PIN 1 to read. Does that matter too? – Luke Oct 22 '16 at 02:09
  • Whoops - my fault! No, you are right, it should not matter then [commencing in-depth analysis, since there are no other answers yet...] ... ... ... okay, analyzed your code and I can tell you that the `if ((millis()-Timer)>MotorFanOff)` will never be true. Problem is that it is an if, and not a "waitFor". It will evaluate to false, then the loop-function will exit (with a 1 s delay) and start over. Distance is checked, if the `distance` falls into the desired interval the timer **is reset** and then checked against the MotorFanOff timeout, which will, again fail, since this is more or less 0. – PKG Oct 22 '16 at 13:58
  • ok!I am a bit lost on that analogy whereby, the evaluation on the if condition for `if ((millis()-Timer)>MotorFanOff)` will never be true, as in shouldn't it count for 5 s while the condition of the distance is true to run that if condition? – Luke Oct 23 '16 at 01:29