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 ![]()
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);
}