for(int x=0; x< 10; x++){---}

Hi guys,
I am trying to figure out the correct use of this

for(int x=0; x< 10; x++)

I am trying to blink LED in a relay when the input pin is not sensing a signal

full code here

not sure how int x get values from

for(int x=0; x< 10; x++){ 
digitalWrite(LED,HIGH); 
delay(1000); 
digitalWrite(LED,LOW); 
delay(500); 
}

not sure how int x get values from

You are not sure how x gets values from what?

That for loop has a local variable, x, initialized to 0. While x is less than 10 (is 0, 1, 2, 3, ..., 9), the body of the loop is executed, and then x is incremented (by 1).

Thanks Paul. May be full code will make sense

int tempF = 0;
int setPoint;

void setup() { 
  sensors.begin();
  dht.begin(); 

inMode(LED,OUTPUT); 
  pinMode(Relay, OUTPUT); 
  Serial.begin(9600); //Begin serial communication
  Serial.println("Arduino Humidistat // LCD Set Point Version"); //Print a message 
} 

void loop() { 

float tempOut; //create a variable to hold the temperature value

sensors.requestTemperatures();  

tempOut = (sensors.getTempFByIndex(0));

float F = (tempOut * 18 + 5)/10 + 32; 
delay(5000); // Wait 5 seconds between measurements.

// Reading temperature or humidity takes about 250 ms
// Sensor readings may also be up to 2 seconds

delay(100); 
float h = dht.readHumidity(); 

// Check if any reads failed and exit early (to try again). 

if (isnan(h)) { 
Serial.println("Failed to read from DHT sensor!"); 
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,30);
display.print("Fault: DHT22");}

if (tempOut <= -54) { 
Serial.println("Failed to read from OUTDOOR sensor!"); 
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,30);
display.print("Fault: DS18B20");
// If the sensor fails to read successfully execute for loop 
// and flash the RELAY LED for 10 times
// a total of 15 seconds 

for(int x=0; x< 10; x++){ 
digitalWrite(LED,HIGH); 
delay(1000); 
digitalWrite(LED,LOW); 
delay(500); 
} 
return;

I am sure it is a basic question but ,how does int x get value 0. int x is not declared anywhere before.
or this is a standard function for counting.

for(int x=0; x is defined and initialised right there.

Its scope is limited to the for loop.

(The code you just posted is incomplete)

not sure how int x get values

What do you want to do with them and where ?

UKHeliBob:
What do you want to do with them and where ?

Thanks AWOL,you are right its the part of a long code sequence.

UKHelibob & AWOL here is the link to my full code.Will appreciate if you could share your thoughts on it.

“how does int x get value 0”

[1]

int x is the declaration of the variable x.
int x is NOT the variable.
x is the variable.
x is declared as an int (or integer).
x=0 is the assigning of 0 to the variable x

int x is declaring x to be an integer variable
int x=0 is the declaration AND assignation of x

[2]

for(int x=0; x< 10; x++)

This means … for x = 0 to 9 step 1.
The for loop will loop 10 times (0,1,2,3,4,5,6,7,8,9).
x will be incremented by 1 at the end of each loop (x++ means x=x+1).
x will start out as 0 (x=0).
x will continue to be incremented until x becomes 10.
When x=10, the loop is completed and finished with, and the code line after the loop is executed.

Steve

Thanks Steve.

what I am not understanding is the syntax I guess.

How does If (isnan(h)) and if (tempOut <= -55 starts the loop

If (isnan(h)) { 
Serial.println("Failed to read from DHT sensor!"); 
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,30);
display.print("Fault: DHT22");}

if (tempOut <= -55) { 
Serial.println("Failed to read from OUTDOOR sensor!"); 
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,30);
display.print("Fault: DS18B20");
// If the sensor fails to read successfully execute for loop 
// and flash the RELAY LED for 10 times
// a total of 15 seconds 

for(int x=0; x< 10; x++){ 
digitalWrite(LED,HIGH); 
delay(1000); 
digitalWrite(LED,LOW); 
delay(500); 
} 
return; /code]

Please use code tags when posting.

This question does not make any sense. Nothing "counts".

How does the other two if statements starts the counting ?

if (isnan(tempOut <= -80)) {

What’s that?

AWOL it was an error with copying from old codes, fixed now.

jremington ,sorry for using incorrect word. It is changed too.How does it starts the loop?

The code you posted will not compile and is lacking at least one "}", which could be put in many different places, so it is impossible to say what the code was intended to do.

How does If (isnan(h)) and if (tempOut <= -55 starts the loop

The if(isnan(h)) statement does NOT cause any looping behavior. If the condition (the part in parentheses) is true, the body of the statement is executed. If the condition is false, the body of the statement is skipped.

The same is true of the other if statement.

Here is the full code that could compiled

#include <DHT.h> 
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

// initialize the library with the numbers of the interface pins
// OLED display TWI address
#define OLED_ADDR   0x3C

Adafruit_SSD1306 display(-1);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif



// DS18B20 Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
#define DHTPIN 4 // AM2302 is plugged into pin 4 
#define DHTTYPE DHT22 // DHT 22 (AM2302) 
#define Relay 3 // Pin to Activate relay
#define LED 13 // Pin to show relay is on (blue)
DHT dht(DHTPIN, DHTTYPE); 
#define DEADBAND 3
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

int tempF = 0;
int setPoint;

void setup() { 
  sensors.begin();
  dht.begin(); 
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();

  pinMode(LED,OUTPUT); 
  pinMode(Relay, OUTPUT); 
  Serial.begin(9600); //Begin serial communication

  Serial.println("Arduino Humidistat // LCD Set Point Version"); //Print a message 
} 

void loop() { 

float tempOut; //create a variable to hold the temperature value
sensors.requestTemperatures();  
tempOut = (sensors.getTempFByIndex(0));
delay(5000); // Wait 5 seconds between measurements.

// Reading temperature or humidity takes about 250 ms
// Sensor readings may also be up to 2 seconds

delay(100); 
float h = dht.readHumidity(); 

// Check if any reads failed and exit early (to try again). 

if (isnan(h)) { 
Serial.println("Failed to read from DHT sensor!"); 
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,30);
display.print("Fault: DHT22");}
if (tempOut <= -55)) { 
Serial.println("Failed to read from OUTDOOR sensor!"); 
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(27,30);
display.print("Fault: DS18B20");

// If the sensor fails to read successfully execute for loop 
// and flash the RELAY LED for 10 times
// a total of 15 seconds 

for(int x=0; x< 10; x++){ 
digitalWrite(LED,HIGH); 
delay(1000); 
digitalWrite(LED,LOW); 
delay(500); 
} 
return; 
} 
// Read DS18B20 and convert to humidity values between 25 and 50% 
// setPoint = the set point for the target humidity level 
{ 
  
  if (tempOut <= -20) {
    setPoint=20;
    }
    else if (tempOut > -20 && tempOut <=-10) {  
      setPoint=25;
    }
    else if (tempOut > -10 && tempOut <=0) {  
      setPoint=30;
    }
    else if (tempOut > 0 && tempOut <=10) {  
      setPoint=35;
    }
    else if (tempOut > 10 && tempOut <=20) {  
      setPoint=40;
    }
    else  {  
      setPoint=45;

    }
      Serial.print ("Humidity Set Point is: ");
      Serial.println (setPoint);
   
// Test to see if the humidity is less than the set point 
// if so turn on relay 
// If not turn off relay

if(h < setPoint + DEADBAND) { 
digitalWrite(Relay, LOW); 
digitalWrite(LED, HIGH); 
Serial.println ("Humidifier on");
delay(120000); 
} 
else { 
digitalWrite(Relay, HIGH); 
digitalWrite(LED,LOW); 
Serial.println ("Humidifier off");
} 
Serial.println (setPoint); 
Serial.print("Humidity: "); 
Serial.print(h); 
Serial.print("%"); 
Serial.print(" Temperature Outside: "); 
Serial.print(tempOut); 
Serial.println(" degC "); 
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(2,2);
display.print("SP:  ");
display.print(setPoint);
display.print("%");
display.print(" RH: ");
display.print(h);
display.print("%");
display.setCursor(0,1);
display.print("OutTemp: ");
display.print(tempOut);
display.print(" degF");
  }
}

Here is the full code

Are you sure ? Shouldn't there be at least one library #included, and where is the sensors object instantiated ?

I am still not sure what problem you have. Can you blink an LED 10 times in a program that does only that ?

Thank you for posting the complete code. In the future, use CNTR-T (^T) in the Arduino monitor to fix the indenting before posting.

It appears from the code posted in reply #13 that the LED will blink if this condition is true:

if (tempOut <= -55)) {

How often do you expect that to happen?

Note the possibly unintended return statement that follows the “blink” loop. That will cause the program to exit the loop() function.

jremington:
Thank you for posting the complete code. In the future, use CNTR-T (^T) in the Arduino monitor to fix the indenting before posting.

It appears from the code posted in reply #13 that the LED will blink if this condition is true:

if (tempOut <= -55)) {

How often do you expect that to happen?

Note the possibly unintended return statement that follows the “blink” loop. That will cause the program to exit the loop() function.

Thanks remington! I will follow that next time.

I like to blink as long as the error from reading sensor persists. Originally thought blink should be for blink 10 times for error from both SENSORS.

But perhaps what’s ideal to do will be blink the LED 10 times for tempOut <= -55 and perhaps 5 times for isnan(h) and in sequence of 10 and 5 for error reading from both.


#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

// initialize the library with the numbers of the interface pins
// OLED display TWI address
#define OLED_ADDR   0x3C

Adafruit_SSD1306 display(-1);

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif



// DS18B20 Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
#define DHTPIN A0 // AM2302 is plugged into pin 4 
#define DHTTYPE DHT11 // DHT 22 (AM2302) 
#define Relay 3 // Pin to Activate relay
#define LED 13 // Pin to show relay is on (blue)
DHT dht(DHTPIN, DHTTYPE);
#define DEADBAND 3
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

int tempF = 0;
int setPoint;

void setup() {
  sensors.begin();
  dht.begin();
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  display.clearDisplay();
  display.display();

  pinMode(LED, OUTPUT);
  pinMode(Relay, OUTPUT);
  Serial.begin(9600); //Begin serial communication
  Serial.println("Arduino Humidistat // LCD Set Point Version"); //Print a message
}

void loop() {

  float tempOut; //create a variable to hold the temperature value
  sensors.requestTemperatures();
  tempOut = (sensors.getTempFByIndex(0));
  float F = (tempOut * 18 + 5) / 10 + 32; //https://forum.arduino.cc/index.php?topic=139006.0
  delay(5000); // Wait 5 seconds between measurements.

  // Reading temperature or humidity takes about 250 ms
  // Sensor readings may also be up to 2 seconds

  delay(100);
  float h = dht.readHumidity();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) && (tempOut <= -54)) {
    Serial.println("Failed sensors!");
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 2);
    display.print("Fault: NO SENSORS");

    for (int x = 0; x < 10; x++) {
      digitalWrite(LED, HIGH);
      delay(500);
      digitalWrite(LED, LOW);
      delay(100);
    }
    return;
  }
  else if (isnan(h)) {
    Serial.println("Failed to read from DHT sensor!");
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(27, 30);
    display.print("Fault: DHT22");

    for (int x = 0; x < 5; x++) {
      digitalWrite(LED, HIGH);
      delay(1000);
      digitalWrite(LED, LOW);
      delay(500);
    }

  }
  if (tempOut <= -54) { //lower threshold FOR TEMP for DS18B20 IS -55C
    Serial.println("Failed to read from OUTDOOR sensor!");
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(27, 30);
    display.print("Fault: DS18B20");
    // If the sensor fails to read successfully execute for loop
    // and flash the RELAY LED for 10 times
    // a total of 15 seconds

    for (int x = 0; x < 10; x++) {
      digitalWrite(LED, HIGH);
      delay(1000);
      digitalWrite(LED, LOW);
      delay(500);
    }
    return;
  }
  // Read DS18B20 and convert to humidity values between 25 and 50%
  // setPoint = the set point for the target humidity level
  {

    if (F <= -20) {
      setPoint = 20;
    }
    else if (F > -20 && tempOut <= -10) {
      setPoint = 25;
    }
    else if (F > -10 && tempOut <= 0) {
      setPoint = 30;
    }
    else if (F > 0 && tempOut <= 10) {
      setPoint = 35;
    }
    else if (F > 10 && tempOut <= 20) {
      setPoint = 40;
    }
    else  {
      setPoint = 45;

    }
    Serial.print ("Humidity Set Point is: ");
    Serial.println (setPoint);

    // Test to see if the humidity is less than the set point
    // if so turn on relay
    // If not turn off relay

    if (h < setPoint + DEADBAND) {
      digitalWrite(Relay, LOW);
      digitalWrite(LED, HIGH);
      Serial.println ("Humidifier on");
      delay(120000);
    }
    else {
      digitalWrite(Relay, HIGH);
      digitalWrite(LED, LOW);
      Serial.println ("Humidifier off");
    }
    Serial.println (setPoint);
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print("%");
    Serial.print(" Temperature Outside C: ");
    Serial.print(tempOut);
    Serial.println(" degC ");
    Serial.print(" Temperature Outside F: ");
    Serial.print(F);
    Serial.println(" degF ");
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0, 2);
    display.print("Hum SP:  ");
    display.setCursor(48, 2);
    display.print(setPoint);
    display.setCursor(84, 2);
    display.print("%");
    display.setCursor(0, 12);
    display.print("RH Room: ");
    display.setCursor(48, 12);
    display.print(h);
    display.setCursor(84, 12);
    display.print("%");
    display.setCursor(0, 24);
    display.print("OutTemp: ");
    display.setCursor(48, 24);
    display.print(F);
    display.setCursor(84, 24);
    display.print((char)247);
    display.setCursor(90, 24);
    display.print("F");
    display.setCursor(0, 36);
    display.print("OutTemp:");
    display.setCursor(48, 36);
    display.print(tempOut);
    display.setCursor(84, 36);
    display.print((char)247);
    display.setCursor(90, 36);
    display.print("C");
  }
}

Made some progress :slight_smile:

However issue with OLED display ,its not showing the absent sensors.

However issue with OLED display ,its not showing the absent sensors.

Want to try again? The OLED display isnt't showing sensors. It shows data.

  float tempOut; //create a variable to hold the temperature value
  sensors.requestTemperatures();
  tempOut = (sensors.getTempFByIndex(0));

It is not necessary to use two statements to declare and initialize a variable. The value in the variable is the temperature in Celsius. The name of the variable should reflect that, especially if F is going to be the temperature in Fahrenheit.

Why aren't you printing F and h, to have some data to use to debug your program? Why do you calculate the temperature in Fahrenheit and the base your decisions on the temperature in Celsius? Which system DO you use?

Thanks Paul.
That code did not help with getting correct temperature. So I followed the post here and removed most of older codes
https://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

DeviceAddress outsideThermometer = {0x28, 0x3A, 0x9B, 0x45, 0x92, 0x07, 0x02, 0x2F};

void loop() {

float tempOut = sensors.getTempC(outsideThermometer);

//float tempOut; //create a variable to hold the temperature value
//sensors.requestTemperatures();
//tempOut = (sensors.getTempFByIndex(0));}

PaulS:
Why aren't you printing F and h, to have some data to use to debug your program? Why do you calculate the temperature in Fahrenheit and the base your decisions on the temperature in Celsius? Which system DO you use?

I am used to Degree Celsius but everywhere around its Fareheit,so kind of like both available.I am more familiar with Farenheit on weather reports