I am an absolutely beginner that has read everything and tried cut and paste and still can't get an if statement to work. I am not a C programmer.
I'm using an ultrasonic sensor, LCD and UNO R3 board with a single channel relay.
I can't get my code to process my if statement after measuring distance to turn my relay on or off based on distance of less than or greater than.
Here's my code. I'm at wits end and I'm so frustrated with Arduino coding... I keep on getting basic error of "exit status 1
expected unqualified-id before 'if' and I don't have a clue what that means because I have no experience with C language.
I realize I am a total idiot but please tell me where I've gone so simplistically wrong. I don't even want to try to start and shut off my buzzer until I figure out what I've done wrong with relay.
#include <LiquidCrystal.h> //http://www.arduino.cc/en/Tutorial/LiquidCrystal
int RS=A5, E=A4, D4=A3, D5=A2, D6=A1, D7=A0;
LiquidCrystal LCD(RS, E, D4, D5, D6, D7); // initialize the library with the numbers of the interface pins
int trigPin=12; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11; //Sensor Echo pin connected to Arduino pin 11
float pingTime; //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.
int Buzzer = 13;
int Relay = 10;
int val=0;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode (Buzzer, OUTPUT);
pinMode (Relay, OUTPUT);
LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0); //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:"); //Print Message on First Row
}
void loop() {
delay(50);
digitalWrite(trigPin, LOW); //Set trigger pin low
delayMicroseconds(2000); //Let signal settle
digitalWrite(trigPin, HIGH); //Set trigPin high
delayMicroseconds(15); //Delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); //Delay in high state
pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds
pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
targetDistance=speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour
targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
targetDistance=targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile)
LCD.setCursor(0,1); //Set cursor to first column of second row
LCD.print(" "); //Print blanks to clear the row
LCD.setCursor(0,1); //Set Cursor again to first column of second row
LCD.print(targetDistance); //Print measured distance
LCD.print(" inches"); //Print your units.
delay(500);
}
// targetDistance=(inches);
if (targetDistance < 4 )
{
digitalWrite(Relay,HIGH);
}
else
{
(targetDistance >= 4 )
{
digitalWrite(Relay,LOW);
} //Water level reaches the Top of the Tank
delay(50);
}
}