i don't know where's the mistake!

Hey everyone,
Lately i’ve been working on the Arduino basics. So i was trying to set an LDR with a laser
and i wanted the door to be opened " only one time" and later it should be ignored, i’m pertty sure that i concted pins correctly and the code i wrote was counted as correct by IDE but the result i have

i click reset on the board, the door motor, get opened every few seconds, here’s my code i think the wrong is the way i wrote if conditions, even so i used earlier if …/else if … but the IDE always counted mistake " else without a previous if "

const int Laser = 3;
const int ledactive = 4;
const int ledready = 5;
const int door = 6;
int sensorReading;
boolean x = false;
void setup()
{
  Serial.begin(9600);
  pinMode(Laser, OUTPUT);
  pinMode(ledactive, OUTPUT);
  pinMode(ledready, OUTPUT);
  pinMode(door, OUTPUT);
  digitalWrite(ledready, HIGH);
  digitalWrite(Laser, HIGH);
  x = false;
}
void loop()

{
  sensorReading = analogRead(0);
  Serial.println(sensorReading);
  if (sensorReading < 800 && x == false);
  {
    digitalWrite(door, HIGH);
    digitalWrite(ledactive, HIGH);
    digitalWrite(ledready, LOW);
    delay (200);
    digitalWrite(door, LOW);
    x = true;
  }
  if (sensorReading < 800 && x == true);
  {
    digitalWrite(ledready, HIGH);
    delay(1000);
    digitalWrite(ledready, LOW);
    delay(1000);
  }
  if (sensorReading > 800 && x == true);
  {
    digitalWrite(ledready, HIGH);
  }
  if (sensorReading > 800 && x == false);
  {
    digitalWrite(ledready, HIGH);
  }
}
  if (sensorReading < 800 && x == true);

If statements rarely end with ;s.

It should be,

if(xyx)
{
//code
}

and not

if(xyz);
{
//code
}

By the way, what door? which door?

If statements rarely end with ;s.

Oh, I don't know, it seems quite common in queries here :)

Here a statement of that type generates a warningwarning: suggest braces around empty body in an 'if' statement [-Wempty-body] But who cares about warnings... ;)

Whandall: Here a statement of that type generates a warningwarning: suggest braces around empty body in an 'if' statement [-Wempty-body] But who cares about warnings... ;)

We need compilers that instead of giving warnings, make a robotic arm bitch slap the programmer in the face. That will teach the programmers give them the due attention!!!

Compiler warnings have become pretty much what the yellow lights are in traffic: they are there just to be ignored!

AlxDroidDev: Compiler warnings have become pretty much what the yellow lights are in traffic: they are there just to be ignored!

It's pretty difficult for a beginner not to ignore them since they are turned off by default in the Arduino IDE.

I can not get into my car, it was delivered with closed doors.

A more apt metaphor would be that you just started learning to drive this week, you're learning how to steer and use the gas pedal, clutch and gear shift. There's a little knob hidden in the glove compartment that enables different functions of the check engine light and nobody told you about it, in fact it's not even mentioned in the manual of the car. The knob is set in the factory so that the check engine light is disabled.

I get the idea but can you tell me what is the difference between the both i mean the one with ";' and the one without? like examples when we use each.

Srijal97 my room's door lol i wanted it to be open when i pass by laser, but i want it to be done only one time then gets ignored until the next rest

thank you all for the answer lol, i didn't tought i may recieve answers so fast :)

There are no examples of putting a semicolon at the end of an if statement. That is not ever done.

If the condition in an if statement is true, it will run the next statement. If you put a semicolon, then the semicolon IS the next statement. It is an empty statement, but it still counts. SO it means, if (this) then do nothing. That's never going to be what you want. If it was, you could just leave the if statement out.

Without the semicolon it will run the next line. If you use brackets, you can make that next line include several lines of code.

takirap:
i click reset on the board, the door motor, get opened every few seconds,

It would be much clearer if you split up the tests - something like

if (x == false) {
if (sensorReading < 800) {

}
else {

 }
}
else {
if (sensorReading < 800) {

}
else {

 }

}

Then you could add some Serial.println() statemenst to show where the code has got to - to see if the tests are working as you think.

Aside from the code, your problem could also be caused by a power supply problem. If the door motor is drawing power from the Arduino (which is should not do) it can cause the voltage to drop and the Arduino to reset.

…R

#Delta_G i kinda understood, i find it hard to understand without exmps i mean like (code <=> result) also i’m not that good in english, but thank you so much !

Robin2:
Aside from the code, your problem could also be caused by a power supply problem. If the door motor is drawing power from the Arduino (which is should not do) it can cause the voltage to drop and the Arduino to reset.

…R

#Robin2, i will try the way you said and let you know if it work, and for the power supply i use 9v charger, and the motor isn’t directly connected to the board, it’s connected to an opto-isolator with a different power source ! and thank you for the code ::

I don’t know what kind of example you mean but here a try:

if(someCondition) ;  //  <-- That semicolon is the statement that will run if someCondition is true
     doSomething();  //<-- this statement is NOT under the control of that if.

it is an empty statement. It does nothing. The if doesn’t control the next line because if can only control one statement. In this case it is the empty semicolon.

if(someCondition)
     doSomething();   // <-- without the semicolon on the if, this is the next statement

So now doSomething() is the next statement, so it is now under control of the if.

If you still have trouble understanding, then just take it as a rule. If statement should NEVER have a semicolon on the end of it. Never.

Delta_G i get the point of difference now, the examples you gave was completely clear, thank you so much i really appreciate you help :)

takirap:
#Delta_G i kinda understood, i find it hard to understand without exmps i mean like (code <=> result) also i’m not that good in english, but thank you so much !

You already have an example of what happens if you use a semicolon. It is in the code that you wrote. Take out the semicolon and you will have an example of how you should do it.

Try this with and without the semicolon

void setup()
{
  Serial.begin(115200);
  if (1 == 2);
  {
    Serial.println("1 equals 2");
  }
  Serial.println("Finished");
}

void loop()
{
}

Hi guys ! sorry for the late answer, but i had some troubles with ARDUINO driver on windows so i had to deal with data then set up Ubuntu, really thank you all for the help, i used this code and it worked perfectly simple & easy to deal with

if (x == false) {
if (sensorReading < 800) {

}
else {

 }
}
else {
if (sensorReading < 800) {

}
else {

 }

}

Then you could add some Serial.println() statemenst to show where the code has got to - to see if the tests are working as you think.

Aside from the code, your problem could also be caused by a power supply problem. If the door motor is drawing power from the Arduino (which is should not do) it can cause the voltage to drop and the Arduino to reset.

…R
[/quote]

UKHeliBob:

void setup()

{
  Serial.begin(115200);
  if (1 == 2);
  {
    Serial.println(“1 equals 2”);
  }
  Serial.println(“Finished”);
}

void loop()
{
}

#UKHeliBob : The exmp you sent me was really awesome and helped so much to understand what was going on thank you so much and for everyone tryed to help ! :slight_smile:

In your off time, read this: http://www.gammon.com.au/forum/?id=12153