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.
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.
//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.
}
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.
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.