Ondelay and Offdelay using 2 potentiometers on Arduino

I have been trying to make a circuit with an off delay and an on delay potentiometer. the input is a sensor and the output is a relay. I have tried many things but nothing seems to be working. I believe that a map function should work but I am not too positive that I am doing it correctly. I am fairly new to Arduino so a lot of this still confuses me. I appreciate any tips or advice that you might have about my code.

const int analogPin = A0;   // pin that the potentiometer is attached to
const int analogPin2 = A4;  // pin that the potentiometer is attached to
const int relay = 13;       // SPDT relay
const int sensor = 2;       // pin that the sensor is attached to
bool latch = 0;

void setup() {
  // initialize the relay pin as an output:
  pinMode(relay, OUTPUT);
  pinMode(sensor, INPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the potentiometer:
  int analogValue = analogRead(A0);
  analogValue = map(analogValue, 0, 1023, 0, 10);
  delay(50);
  int analogValue2 = analogRead(A4);
  analogValue2 = map(analogValue2, 0, 1023, 0, 10);
  delay(50);
  int sensorValue = digitalRead(sensor);

  if (sensorValue == 1) {
    latch = 1;
  }
  if (latch == 1) {
    digitalWrite(relay, HIGH);
    delay(analogValue);
  } else {
    digitalWrite(relay, LOW);
    delay(analogValue2);
  }
  latch = 0;
  // print the analog value:
  Serial.print(analogValue);
  Serial.print("      ");
  Serial.print(analogValue2);
  Serial.print("      ");
  Serial.print(sensorValue);
  Serial.print("      ");
  Serial.println(latch);
  delay(500);  // delay in between reads for stability
}

I dont seem to understand. How do I have the mapping value set to a delay of 10ms? The 10 in the mapping function doesn't translate to 10 seconds?

I just thought that the mapping function was different then a regular delay. Would this be the reason as to why the mapping function has no effect on my circuit at all? Right now the only delay I have is the natural delay of the PIR sensor

I'm not sure that what that code looks like you are aiming for is what you really want to do.

If the PIR makes a shortish pulse when it sees motion, and let us say you got one such trigger, it looks like you want the relay to close for ten seconds, and then go back open for ten seconds, during which the sensor is ignored.

More usually seen is something that does another thing once per motion sensed. Counting ppl maybe somehow.

Also asked for is keep the relay closed as long as there is motion, which as you know the PIR can do, but might not be adjustable to, say, keeping the light on the cat's litter box on ask long as he's in there plus five minutes. The light and a fan, OK? For him and us, if you get what I'm talking about.

Call that stretching the PIR indication.

So please use a few more words to describe how you want to exploit the periodic and already somewhat stretched motion indications from the PIR.

I'm small logic like this, saying ant getting what you want clear is harder than coding it.

a7

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