Make my relay on a off state or standby when the sensor catches something

i am very new to this, we need this for our project and i ve just gotten into it last week.

these codes are tasked to do 2 things:
(the relay is turned off at the beginning)

  • when something is detected, the relay must be turned off.
  • once that something is undetected the relay spurts out pulses to the board then turns off completely.

its for our modified doorknob sanitizer so when someone uses the knob then lets go a spray of solution will disperse via water pumps to that doorknob.

the problem is, when something is detected the relay remains turned on rather than turned off.

code:

// Pin definitions
const int obstacleSensorPin = 9;  // Pin connected to obstacle sensor
const int relayPin = 8;           // Pin connected to relay

// Variables
bool obstacleDetected = false;    // Flag to track obstacle detection

void setup() {
  pinMode(obstacleSensorPin, INPUT);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);    // Ensure relay is initially off
  Serial.begin(9600);
}

void loop() {
  // Check obstacle sensor
  if (digitalRead(obstacleSensorPin) == HIGH) {
    // Obstacle detected
    if (!obstacleDetected) {
      obstacleDetected = true;
      digitalWrite(relayPin, HIGH);  // Standby mode
      Serial.println("Obstacle detected. Standby mode.");
    }
  } else {
    // No obstacle detected
    if (obstacleDetected) {
      obstacleDetected = false;
      Serial.println("No obstacle detected. Turning relay on and off rapidly.");
      // Rapidly turn relay on and off
      for (int i = 0; i < 1; i++) {
        digitalWrite(relayPin, HIGH);
        delay(100);
        digitalWrite(relayPin, LOW);
        delay(100);
      }
      // Turn off completely
      digitalWrite(relayPin, LOW);
      Serial.println("Relay turned off.");
    }
  }
}

You may want to react when the sensor goes HIGH, not because it is (or continues to be) HIGH.

Or maybe going LOW is when your sensor is triggered.

This is called state change detection, and there is an example in the IDR and a decent article on it

https://docs.arduino.cc/built-in-examples/digital/StateChangeDetection/

a7

look this over

const int obstacleSensorPin = 9;  // Pin connected to obstacle sensor
const int relayPin = 8;           // Pin connected to relay

bool obstacleDetected;

enum { Off = LOW, On = HIGH };

// -----------------------------------------------------------------------------


void loop () {
    if (digitalRead (obstacleSensorPin) == HIGH) {
        if (! obstacleDetected)
            Serial.println ("Obstacle detected. Standby mode.");
        obstacleDetected = true;
        digitalWrite (relayPin, Off);       // stop pulsing

        delay (20);             // debounce when testing with switch
    }
    else if (obstacleDetected) {
        obstacleDetected = false;
        Serial.println ("No obstacle detected - pulse rapidly.");

        for (int i = 0; i < 5; i++) {   // just once ??
            digitalWrite (relayPin, On);
            delay        (100);
            digitalWrite (relayPin, Off);
            delay        (100);
        }
    }
}

void setup () {
    pinMode (obstacleSensorPin, INPUT_PULLUP);
    pinMode (relayPin,          OUTPUT);
    digitalWrite (relayPin, Off);
    Serial.begin (9600);
}

Help! only one works, tried to do 2 loops, arrays.
new to this, found it hard to solve.

2 relays and 2 sensors. only one of them works...

// Pin definitions
const int obstacleSensorPin = 9;  // Pin connected to obstacle sensor
const int relayPin = 8;           // Pin connected to relay
const int obstacleSensorPin2 = 11;
const int relayPin2 = 10;

// Variables
bool obstacleDetected = false;    // Flag to track obstacle detection

void setup() {
  pinMode(obstacleSensorPin, INPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(obstacleSensorPin2, INPUT);
  pinMode(relayPin2, OUTPUT);
  digitalWrite(10, LOW); 
  digitalWrite(11, LOW);  // Ensure relay is initially off
  Serial.begin(9600);
}

void loop() {
  // Check obstacle sensor
  float marks [4];
  marks[0] = obstacleSensorPin;
  marks[1] = obstacleSensorPin2;
  marks[2] = relayPin;
  marks[3] = relayPin2;

  if (digitalRead(marks[0, 1]) == HIGH) {
    // Obstacle detected
    if (!obstacleDetected) {
      obstacleDetected = true;
      digitalWrite(marks[2,3], HIGH);  // Standby mode
      Serial.println("Obstacle detected. Standby mode.");
    }
  } else {
    // No obstacle detected
    if (obstacleDetected) {
      obstacleDetected = false;
      Serial.println("No obstacle detected. Turning relay on and off rapidly.");
      // Rapidly turn relay on and off
      for (int i = 0; i < 1; i++) {
        delay(3000);
        digitalWrite(marks[2, 3], HIGH);
        delay(100);
        digitalWrite(marks[2, 3], LOW);
        delay(900);
      }
      // Turn off completely
      digitalWrite(marks[2, 3], LOW);
      Serial.println("Relay turned off.");
    }
  }
}

You are using floating point numbers as indices, and improperly beyond that.

You've used the comma operator, so that index is 1.

marks is a one dimensional array of 4 elements.

A two dimensional array in C/C++ would look like

    int myTwoDArrsy[3][4];

and an element would be accessed like

   anotherInt = myTwoDArrsy[2][3];

Without tearing your code apart, I can't be sure what you were going for, but your syntax was obvsly incorrect.

Review C/C++ arrays.

a7

for easy understanding of the code, you case use it like whereever you use it.

Blockquote
if (digitalRead(marks[0]) == HIGH && digitalRead(marks[1]) == HIGH ) {

@c0dy19,

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

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

It will help you get the best out of the forum in the future.

Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.