Relay "sequence" at motion

Hi guys. New to relays with arduino.
I need this with a relay when distance is <"xx-cm", the relay should turn on for 2 seconds, then off for 7 seconds, then on for 2 seconds and then off until next detection is made.
When using this code, its comes with error " 'else' without a previous 'if', and I actual doesnt understand that?
If I remove the "if" in the code, the relay just keeps looping with on/off with the second entered in delays.

Anyone that can guide me a little closer to the end? If you understand my needs :stuck_out_tongue:

const int TRIG_PIN  = 10;  // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN  = 11;  // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int RELAY_PIN = A5; // Arduino pin connected to Relay's pin
const int DISTANCE_THRESHOLD = 50; // centimeters

// variables will change:
float duration_us, distance_cm;

void setup() {
  Serial.begin (9600);        // initialize serial port
  pinMode(TRIG_PIN, OUTPUT);  // set arduino pin to output mode
  pinMode(ECHO_PIN, INPUT);   // set arduino pin to input mode
  pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
  // generate 10-microsecond pulse to TRIG pin
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // measure duration of pulse from ECHO pin
  duration_us = pulseIn(ECHO_PIN, HIGH);
  // calculate the distance
  distance_cm = 0.017 * duration_us;

  if(distance_cm < DISTANCE_THRESHOLD)
    digitalWrite(RELAY_PIN, HIGH); // turn on Relay
  delay(2000);
  digitalWrite(RELAY_PIN, LOW); // turn off Relay
  delay(7000);
  digitalWrite(RELAY_PIN, HIGH); // turn on Relay
  delay(2000);
  digitalWrite(RELAY_PIN, LOW); // turn off Relay  
  
  
  else
    digitalWrite(RELAY_PIN, LOW);  // turn off Relay

  // print the value to Serial Monitor
  Serial.print("distance: ");
  Serial.print(distance_cm);
  Serial.println(" cm");

  delay(500);
}

Thank you very much! I actually knew they should be there, dont know why i didnt noticed they were missing. Its late ind denmark (12:20 AM) :smile:

If I want the relay to turn on then off (the first sequence) when distance "<xx cm" , so instead of delay(7000) it should wait until distance is ">xx cm" then run the second on/off - can you you point me in some direction? :slight_smile:

Im trying to do it My self tomorrow with some distance > or something :slight_smile:

1 Like

You will need to "set a flag" that says "I have been less than 50cm" and test the flag until distance is greater than 50cm.

Understand, you are making the blink UNDER 50 and OVER 50... which means "any distance"

May I make a suggestion: Blink always when UNDER 50 and stop blinking when OVER 50. To do this, you will want to read, understand and implement, "Blink without delay()" because without this, your program will only blink when under 50, and not do anything else. Ask any questions...

1 Like

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