0
votes

functionality:

User approaches ultrasonic/infra-red sensor connected to Arduino Uno, if the proximity distance is less than 20cm, triggerPin of ultrasonic/infra-red sensor will toggle from LOW to HIGH -> list of '0's will be toggled to list of '1's.

When the ultrasonic/infra-red sensor is toggled from '0' to '1', the state LED and state MotorFan will toggle to HIGH from LOW. Both states will remain as long as the return data from the ultrasonic sensor is '1'.

When user leaves the proximity of the ultrasonic/infra-red sensor, triggerPin of ultrasonic/infra-red sensor will toggle from HIGH to LOW -> list of '1's will be toggled to list of '0's.

However, at this point, the state LED and state MotorFan will remain in state HIGH for 10s, before toggling to state LOW. In the event, that another user approaches the proximity sensor and the ultrasonic/infra-red sensor toggles back to '1' within the delay of the 10s, state LED and state MotorFan should remain in state HIGH without toggling.

Therefore, the correct flow as follows:

ultrasonic/infra-red Sensor returns '0's -> but when sense user within (distance < 20) ultrasonic/infra-red sensor toggles to '1's when ultrasonic/infra-red sensor toggles to '1's, MotorFan state and LED state will toggle to HIGH from LOW ultrasonic/infra-red Sensor is returns '1's when sensing proximity-> but when sense user more (distance > 20) ultrasonic/infra-red sensor toggles to '0's, at this moment, MotorFan state and LED state will remain in state HIGH for 10s before toggling to LOW. Condition: when another user approaches before the 10s delay is up, MotorFan state and LED state will remain in state HIGH.

What I have done:

I have assigned the following digital pins for the following vairable:

trigPin of ultrasonic sensor is digital Pin 8 echoPin of ultrasonic sensor is digital Pin 9 FanPin of motor Fan is digital Pin 5 LED is assigned to relay 4.

Secondly, I have tried to make use of millis() to get the time and if it should exceed limit of 10000, it will digitalWrite both state to LOW from HIGH.

Code: const int trigPin = 8; const int echoPin = 9; //Motor-Fan connected to arduino pin number const int FanPin = 5; byte relay = 4;

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

void setup() {
  Timer = millis();
  pinMode(FanPin, OUTPUT); // Set pinMode for FanPin as OUTPUT, display  
  pinMode(trigPin, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600); // Open serial port to communicate with the Ultrasaonic Sensor
}

void loop() {

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = duration * 0.034 / 2;

  if (distance < 20) {
    // Write a pin of HIGH
    Serial.println("1");
    //Set motor-fan to operate
    digitalWrite(FanPin, HIGH);
    digitalWrite(relay, HIGH);
  } else {

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

    if ((millis() - Timer) > Interval) {
      digitalWrite(FanPin, LOW);
      digitalWrite(relay, LOW);
    }

  }
  delay(100);
}

Issue:

The Sensor is able to toggle from '0' to '1' when sense proximity, secondly, it is also able to toggle both the LED state and motorFan state to HIGH from LOW.

However, Both the LED state and motorFan toggle back to LOW when the Arduino sensor toggle from HIGH to LOW without waiting for the 10s delay

I would like to request for some assistance on this as I am absolutely stuck on this problem.

Thanks

1
0) You're getting the value for startTime(timer) in the setup phase. (1) Your interval is set to 1000, i.e 1 thousand milliseconds - a second. (2) You probably wish to re-trigger the 10 second delay. If so, get the new value of timer each time the last difference exceeds 10,000 (i.e, do it in the last if-block of your code)enhzflep
@enhzflep, I have edited but am not getting the result as wellLuke
@enhzflep so what do you mean?cause, I have edited and change the interval variable to 10000 and it is still not showing the correct behaviourLuke

1 Answers

0
votes

You assign a value to Timer in setup() function, which means you do it only once, when the system starts. I would move Timer = millis(); to if (distance < 20) { condition. Then, if user moves out if sensing distance, you will already have a time of "last contact".

Remember: if you want to have something executed only at startup - put it in setup(). If you want something to be executed repeatedly - put it in the loop().