Code runs but does not do what it is supposed to automatic box

Hi Im making an automatic box where a user would wave hand in front of the external ir sensor to open to box and the lid of the box has another ir sensor to detect if a hand is there and it will close if a hand is not there. The code I have runs but it does not do what its is supposed to do. Can I get some advice?

This is the code
#include <Servo.h>
Servo servo;
int ext_ir_pin = 2;
int int_ir_pin = 4;
int servo_pin = 3;
int ext_val = digitalRead(ext_ir_pin);
int int_val = digitalRead(int_ir_pin);

void setup() {
pinMode(ext_ir_pin, INPUT);
pinMode(int_ir_pin, INPUT);
servo.attach(servo_pin);
Serial.begin(9600);
}

void loop() {

if (ext_val == LOW)
servo.write(180);
else
servo.write(0);

if (int_val == LOW)
servo.write(180);
else
delay(5000);
servo.write(0);
}

You seem to think that those digitalRead()s above setup() cause a perpetual re-read of the sensors, which is not the case. You need to do that in loop().

int ext_val = digitalRead(ext_ir_pin);
int int_val = digitalRead(int_ir_pin);
  1. For a start those two lines need to be inside the loop part of your program. That way the sensors will be read again and again forever.

  2. You need, when you post here, to put the code inside the "code font", select the code part of your post and then click on the </> icon in the tool bar.

  3. You need to indent your code so it is clearer to other people where groups of statements start and end, for example:


void loop() {

    if (ext_val == LOW)
        servo.write(180);
    else
        servo.write(0);

    if (int_val == LOW)
        servo.write(180);
    else
        delay(5000);
    servo.write(0);
}

As you can see from the above the structure of the code is easier to understand if you indent parts of it.

And remember to put the two reads inside the loop....

Hi, Im building an automatic box for mask sanitisation which opens when the external ir sensor detects something and closes when the internal ir sensor does not detect anything after 5s. The relay also turns on when the ultrasonic sensor detects that there is something within 15cm. The code runs but it does not fufil its purpose. May I know why?

#include <Servo.h>
#define echoPin 5 //Echo pin connected to pin 5
#define trigPin 6 //Trigger pin connected to pin 6

Servo servo;

int ext_ir_pin = 2; //External ir sensor pin connected to pin 2
int int_ir_pin = 4; //Internal ir sensor pin connected to pin 4
int servo_pin = 3; //Servo pin connected to pin 3
long duration;
int distance;
int relay = 8; //Relay pin connected to pin 8
volatile byte relayState = LOW;
long lastDebounceTime = 0;
long debounceDelay = 10000;
int PowerPin1 = 9; //Power for ext ir sensor is connected to pin 9
int PowerPin2 = 11; //Power for int ir sensor is connected to pin 11
int PowerPin3 = 12; //Power for ultrasonic sensor is connected to pin 12

void setup() {
  pinMode(PowerPin1, OUTPUT);//Power for ext ir pin
  digitalWrite(PowerPin1, HIGH);
  pinMode(PowerPin2, OUTPUT);//Power for int ir pin
  digitalWrite(PowerPin2, HIGH);
  pinMode(PowerPin3, OUTPUT);//Power for ultrasonic sensor
  digitalWrite(PowerPin3, HIGH);

  pinMode(ext_ir_pin, INPUT); // Ext ir pin transmits data to the arduino
  pinMode(int_ir_pin, INPUT); //Int ir pin transmits data to the arduino
  servo.attach(servo_pin);
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT); //Trigger pin emits ultrasonic sound
  pinMode(echoPin, INPUT); //Echo pin receives reflected ultrasonic sound
  pinMode(relay, OUTPUT);
}

void loop() {
  int ext_val = digitalRead(ext_ir_pin);
  int int_val = digitalRead(int_ir_pin);

  if (ext_val == HIGH) //If external sensor detects nothing
    servo.write(0); //Servo remains at original position
  else
    digitalWrite(relay,LOW); //Light turns off
    delay(500);
    servo.write(180); //Servo rotates 180 degrees clockwise

  if (int_val == LOW) //If internal sensor detects an object
    servo.write(180); //Servo remains at 180 degrees clockwise
  else
   delay(5000); //Wait for 5s
   servo.write(0); //Servo rotates back to original position
   
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);  digitalWrite(trigPin, HIGH); //Ultrasonic sound emitted
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW); //Ultrasonic sound stops emitting
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  if (distance < 15)
    digitalWrite(relay, HIGH); //Light is turned on
  else
    digitalWrite(relay, LOW); //Light is turned off
}

not clear what it doesn't do. looks like it controls a servo in a confusing way, but looks like it controls a relay conventionally

why are there 2 independent conditions controlling the servo? and why a conditional 5 sec delay?

Because there are 2 sensors. The external sensor opens the box when a hand is detected and box remains closed in no hand is detected. The internal sensor closes the box when there is no hand in the box and remains open if a hand is present. There is a 5s delay to allow the user to have sufficient time to put the hand in the box.

this is contradictory!

seems that there should be a condition to open the box when a hand is detected either outside OR inside the box.

and if neither is TRUE (i.e. NOT outside AND NOT inside) there can be a 5 sec delay before closing the box

You are probably best of using a state machine to drive your code - identify the states and transitions, and the actions that need doing on each state change. Just drawing out a state-transition diagram usually helps think about the problem and highlight edge-cases.

The box is opened when a hand is detected outside or when a hand is detected inside but a hand cannot be detected inside when the box is closed so there is only 1 variable. The internal sensor is for the box to remain open when there is a hand inside so that the lid does not close oin the hand.

that's why i said

How can I solve this problem?

with what? your logic?
it's confusing

there should be a condition that cause the box to open and as you've suggested remain open. there should be a condition that causes the box the close (after a delay) but which is reset if the condition to open the box re-occurs

[b]Duplicate topics merged/b]
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.