# IF condition inside itself

Hey guys! I'm new here, but i've recently started using arduino and now that i went to the IF statement part, i'm confused. :~
The problem is:
I have an ultrasonic sensor that every second reads the values from the space available in front of it. (the sensor is glued to the computer monitor and its pointed to everyone who sits in front of the pc)
I'v turned the values to centimetres for me to handle it better.
If i'm more that 60cm closer to the sensor it means that i'm in front of the pc and i want the program to open a .mp3 file in the computer that play a sound "HELLO!!!!!!"
If not i want it to play a .mp3 sound that says "GoodBye!!!!!".
The thing is that every second the sensor looks for someone in front of the pc and play the sound. i want it to keep looking for someone but must put a condition:
while looking if anyone get in front of the pc he "says hello" and dont say again after 1 second because every 1 second he keeps saying hello. i just want him to say once hello and the if i keep in front of the pc there must be a flag that goes 1 ... 1.....1....1.....1.....1.....1... except when i move away the flag goes 0 and plays goodbye.mp3 and dont play goodbye.mp3 anymore until i move in front of it. and so the flag goes 0....0....0...0...0.....
here is the code i got so far.
(everything is correct except the part of the if statements)

``````#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
int hello = 0; // the variable that check if already said hello so that he once say every 1 second
int goodbye = 0; // the variable that check if already said goodbye so that he once say every 1 second

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance <60 ){ // means that i'm closer enough to the computer to say hello to me. 60 cm is really close to it

if (hello = 0) {
Serial.println("#S|HELLO|[]#"); //sends a message to a program in windows 7 to run an hello.mp3 file
hello = 1; //so now he said hello and he wont say it anymore until i move away and i will say goodbye
goodbye = 0; //so like this 0 means that he didnt say goodbye and in case i move away he's ready to say it
}
}
else { // means that im not closer to the computer
if (goodbye = 0) {
Serial.println("#S|GOODBYE|[]#"); //sends a message to a program in windows 7 to run an goodbye.mp3 file
goodbye = 1;
hello = 0;
}
}

delay(100);
}
``````

Moderator edit:
`</mark> <mark>[code]</mark> <mark>`

`</mark> <mark>[/code]</mark> <mark>`

Why is your question in bold? Is it extra important?

``````   if (hello = 0) {
``````

That is not how you test for zero. That assigns zero to hello.

You want:

``````   if (hello == 0) {
``````

you really made me laugh now xD

i'm sorry, but it was just to separate it from the code.

well, voilá!!! it worked! THANK you very much!

(if i cant have j.a.r.v.i.s., thats the closest hello i can get from the pc :))