Errors whenever I use for loops

I have tried using "for loops" multiple times and I have the same error over and over. There must be something obvious I am missing. Here is an example.

//This section is for the ultrasonic sensor.
const int trigPin = 9; 
const int echoPin = 10;
float duration, distance;

//This section is for the humidity and temp sensor
#include <dht.h>
dht DHT;
#define DHT11_PIN 7

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

void loop(){
  for (distance > 50 or DHT.humidity < 70)
  {
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);

    digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay (1000);
  }
}

On the 18th line there is an error that says "AutomaticBathroomFan:38:1: error: expected primary-expression before '}' token
exit status 1
expected ';' before ')' token" (Automatic bathroom fan is the name of my project)

Actually, I am not sure if this code will have other errors since I cannot seem to get past this one.

Look at the reference for the for loop. Your syntax bears no resemblance to the for loop syntax.

What you, probably, want there is an if structure.

What do you want to happen there?

I want it to stop the loop when it has distance less than 50 cm, or detects a humidity over 70%.

You need to read the temp, humidity and distance outside of the comparison. Then decide what to do.

You do not stop loop(). Have the processor do something if the comparison is true, something different if the comparison is false.

So basically the reverse of what it is currently?

Even then I still have the error

Then post the new code.

Post the error message. Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"

D:\Silas\Documents\Arduino\expariment\AutomaticBathroomFan\AutomaticBathroomFan.ino: In function 'void loop()':

AutomaticBathroomFan:18:42: error: expected ';' before ')' token

for (distance < 50 or DHT.humidity > 70)

                                      ^

AutomaticBathroomFan:38:1: error: expected primary-expression before '}' token

}

^

AutomaticBathroomFan:38:1: error: expected ';' before '}' token

AutomaticBathroomFan:38:1: error: expected primary-expression before '}' token

AutomaticBathroomFan:38:1: error: expected ')' before '}' token

AutomaticBathroomFan:38:1: error: expected primary-expression before '}' token

exit status 1

expected ';' before ')' token

Board at COM6 is not available

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

And the code?

//This section is for the ultrasonic sensor.
const int trigPin = 9; 
const int echoPin = 10;
float duration, distance;

//This section is for the humidity and temp sensor
#include <dht.h>
dht DHT;
#define DHT11_PIN 7

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

void loop(){
  for (distance < 50 or DHT.humidity > 70)
  {
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);

    digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay (1000);
  }
// here is where I would have more stuff happen before going back to the loop.
}

Still incorrect syntax for a for loop. And a for loop is not the right thing, any way.

If you are not going to read my posts I will help someone else. Or am I being trolled?

No just a noob. I have read the Arduino for loop syntax and can't understand it

You REALLY need to look up the correct syntax for a for loop in c. You cannot simply make up your own.

Not a for loop at all. Review the syntax.

And maybe heed the other advice to consider that you may rather be using an if statement.

Until you know the syntax, and the difference, and where one might be used instead of the other, stop wasting your time tryin' to get something to work.

Guessing is futile. Improvisation is not likely to help.

a7

When you need to make a decision (is humidity under 70% or is the distance less than 50cm) a for loop is not useful.

An if structure is what you use to make a comparison.

Here is a good tutorial on C++ (the language used by Arduino).
Here is a section on control structures (comparison and decision making).

I have reviewed the syntax and I was wrong, an if statement is correct for this application. Sorry for not researching first.

I feel your pain. You need to learn how to learn, that is you need to be able to understand how such things are described, or taught.

Share a link to an explanation of the C++ for loop that you don't understand; perhaps we can help explain the explanation.

Or we might agree it is obscure and poorly written, maybe a better one can be found.

doesn't look too bad. The way it is handled there and the terminology will be the same through all the pages on the site.

So a little language to learn before you can learn the bigger language.

a7

A for loop is generally used when you want to do something a certain number of times.

The general usage is as follows. It has 3 argument that you need to provide. These are separated by semi-colons.


for (int x = 0; x < 5; x++)
{

  // Do stuff inside the loop here.
  
  // This will loop 5 times.
  // On the first loop x will be 0 (int x = 0).
  // x is incremented (1 is added to it) at the end of each loop (x++)
  // Once the condition (x < 5) is no long true the loop will stop  
}

This may not be what you require, as you want you loop to continue until a certain condition is true. A while loop is probably more appropriate.

It was just me trying to use a for loop in the place of an if/else statement without learning the correct syntax for the loop.

A while loop also would work.