Loops

Hi all

I'm new to the cool world of Arduino

i have made a box that checks the humidity of a room if the humidity is to high it will on a dehumidifier thought a relay, once the humidity become more normal the dehumidifier will turn off.
i would like to check 5 time before it shuts down
how it starts up is using a light sensor. once someone gose into the room and turn on the light it starts up.

my question is how can i repeat the script 5 time before it gose back to checking if lights on?
i think its loops or i could just repeat the script 5 times.
i been looking at loops and what i see is the night rider effect using multi leds tho a cool effect not really what im looking for.

any ideas?

thanks for yer time

Dave

any ideas?

Well, you could give us an idea of the complexity of your code by posting it.

thanks AWOL for the quick replay

its at home and also its ... on 2 boards main part and the light sensor on the other

its a mash up of the air temperature and humidity measurements sciprt and the blinking light script.

if its helps the main body of the script is the air temperature and humidity measurements sciprt here
http://arduino-info.wikispaces.com/TemperatureHumidity

i remove alot of it only to have the humidity
then used 2 ifs if humidity< 80%
digitalWrite (led,HIGH); this turn on the relay
delay (200)
digitalWrite (led,LOW); this turn off the relay

the other if is the same as above sept its a >

i have this working which is cool

i think im just making alot of confustion here so will post up the scipt when i get back home.

Can you clarify the requirement a little? I think I'm reading that what you want to do is control the humidity in the room when the light is on. But given that, I don't see what the relevance of the five iterations is. Also, I can't decide what you mean by shut down - stop the humidifier, or stop controlling it?

my question is how can i repeat the script 5 time

As you guessed it is the for() structure.

i been looking at loops and what i see is the night rider effect using multi leds tho a cool effect not really what im looking for.

Try and separate the contents of the repeating code in the loop from the structure of the loop itself.
An example of loops has to do something inside the loop, you substitute what you want to do in this loop, the structure is like this:-

for(int i=0; i<5; i++){
// put the stuff you want to do 5 times here
}
for(int i = 0; i<5; i++){

Cool thanks Grumpy_Mike and AWOL

i will bw putting that in

i have the scripts as far as i have got it

1st is the main

/* YourDuino.com Example Software Sketch
DHT11 Humidity and Temperature Sensor test
Credits: Rob Tillaart
http://arduino-direct.com/sunshop/index.php?l=product_detail&p=162
terry@yourduino.com */

/-----( Import needed libraries )-----/
#include <dht11.h>

/-----( Declare objects )-----/
dht11 DHT11;

/-----( Declare Constants, Pin Numbers )-----/
#define DHT11PIN 2

void setup() /----( SETUP: RUNS ONCE )----/
{
Serial.begin(9600);
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();

pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
}/--(end setup )---/

void loop() /----( LOOP: RUNS CONSTANTLY )----/
{
Serial.println("\n");

int chk = DHT11.read(DHT11PIN);

Serial.print("Read sensor: ");
switch (chk)
{
case 0: Serial.println("OK"); break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}

Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);

Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);

if (DHT11.humidity < 80) {
// do stuff if the condition is true
digitalWrite(12, HIGH); // set the ON_Relay on
delay(1000);
digitalWrite(12, LOW); // set the ON_Relay off
} else {
// do stuff if the condition is false
digitalWrite(12, LOW); // set the ON_Relay off
}
if (DHT11.humidity > 80) {
// do stuff if the condition is true
digitalWrite(13, HIGH); // set the OFF_Relay on
delay(1000);
digitalWrite(13, LOW); // set the OFF_Relay off
} else {
// do stuff if the condition is false
digitalWrite(13, LOW); // set the OFF_Relay off

}

delay(500);
}/* --(end main loop )-- */

/-----( Declare User-written Functions )-----/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}

// dewPoint function NOAA
// reference: Algorithms - Schlatter and Baker
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: Dew point - Wikipedia
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}

/* ( THE END ) */
this working which is cool

2nd script is the light sensor this is on a different board

/* Photocell simple testing sketch.

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground

For more information see www.ladyada.net/learn/sensors/cds.html */

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider

void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
pinMode(13, OUTPUT);

}

void loop(void) {
photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading

// We'll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
} else if (photocellReading > 499) {
Serial.println(" - DO NOTHING");
} else if (photocellReading < 500) {
Serial.println(" - START TESTING");
// START THE MAIN SCRIPT
}
delay(50);
}

and this is working to

Please modify the all the posts. Select the code and then hit the # icon. Then re save it.

Then post exactly what your problem is.

and finally i have put the 2 togther tho this isnt working coming up with errors but will workon this.

1 quick question

on the light sensor script there is a

void setup(void)

is the naming txt in the ( ) which is void is this calling the setup a name. so if i wanted to go back to the top of that script i would use the name void?

anyway here is mash up of both

/* YourDuino.com Example Software Sketch
DHT11 Humidity and Temperature Sensor test
Credits: Rob Tillaart
http://arduino-direct.com/sunshop/index.php?l=product_detail&p=162
terry@yourduino.com */

/* Photocell simple testing sketch.

Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground

For more information see www.ladyada.net/learn/sensors/cds.html */

/-----( Import needed libraries )-----/
#include <dht11.h>

/-----( Declare objects )-----/
dht11 DHT11;

/-----( Declare Constants, Pin Numbers )-----/
#define DHT11PIN 2

int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider

void setup(void) /----( SETUP: RUNS ONCE )----/
{
Serial.begin(9600);
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();

pinMode(13, OUTPUT);
pinMode(12, OUTPUT);

// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}/--(end setup )---/

void loop(void) /----( LOOP: RUNS CONSTANTLY )----/

photocellReading = analogRead(photocellPin);

Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading

if (photocellReading < 10) {
} else if (photocellReading < 500) {
Serial.println(" - START TESTING");
{
Serial.println("\n");

int chk = DHT11.read(DHT11PIN);

Serial.print("Read sensor: ");
switch (chk)
{
case 0: Serial.println("OK"); break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}

Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);

Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);

if (DHT11.humidity < 80) {
// do stuff if the condition is true
digitalWrite(12, HIGH); // set the ON_Relay on
delay(1000);
digitalWrite(12, LOW); // set the ON_Relay off
} else {
// do stuff if the condition is false
digitalWrite(12, LOW); // set the ON_Relay off
}
if (DHT11.humidity > 80) {
// do stuff if the condition is true
digitalWrite(13, HIGH); // set the OFF_Relay on
delay(1000);
digitalWrite(13, LOW); // set the OFF_Relay off
} else {
// do stuff if the condition is false
digitalWrite(13, LOW); // set the OFF_Relay off

}
} else if (photocellReading > 499) {
Serial.println(" - DO NOTHING");
}
delay(500);
}
delay(50);

}/* --(end main loop )-- */

/-----( Declare User-written Functions )-----/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}

// dewPoint function NOAA
// reference: Algorithms - Schlatter and Baker
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: Dew point - Wikipedia
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}

/* ( THE END ) */

sorry new to all of this

/* YourDuino.com Example Software Sketch
   DHT11 Humidity and Temperature Sensor test
   Credits: Rob Tillaart
   http://arduino-direct.com/sunshop/index.php?l=product_detail&p=162
   terry@yourduino.com */
   
/* Photocell simple testing sketch. 
 
Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
 
For more information see www.ladyada.net/learn/sensors/cds.html */
   
/*-----( Import needed libraries )-----*/
#include <dht11.h>

/*-----( Declare objects )-----*/
dht11 DHT11;

/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN 2


int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the analog resistor divider


void setup(void)   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
  
  pinMode(13, OUTPUT);  
  pinMode(12, OUTPUT); 
  
  // We'll send debugging information via the Serial monitor
  Serial.begin(9600); 
}/*--(end setup )---*/

void loop(void)   /*----( LOOP: RUNS CONSTANTLY )----*/

  photocellReading = analogRead(photocellPin);  
 
  Serial.print("Analog reading = ");
  Serial.print(photocellReading);     // the raw analog reading
 
  if (photocellReading < 10) {
 } else if (photocellReading < 500) {
    Serial.println(" - START TESTING");
{
  Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);

if (DHT11.humidity < 80) {
   // do stuff if the condition is true
    digitalWrite(12, HIGH);   // set the ON_Relay on
    delay(1000);
    digitalWrite(12, LOW);   // set the ON_Relay off
} else {
   // do stuff if the condition is false
    digitalWrite(12, LOW);   // set the ON_Relay off
}
if (DHT11.humidity > 80) {
   // do stuff if the condition is true
    digitalWrite(13, HIGH);   // set the OFF_Relay on
    delay(1000);
    digitalWrite(13, LOW);   // set the OFF_Relay off
} else {
   // do stuff if the condition is false
    digitalWrite(13, LOW);   // set the OFF_Relay off
    
}
 } else if (photocellReading > 499) {
    Serial.println(" - DO NOTHING");
}
  delay(500);
 } 
   delay(50);
  
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
	return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
	return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
	double A0= 373.15/(273.15 + celsius);
	double SUM = -7.90298 * (A0-1);
	SUM += 5.02808 * log10(A0);
	SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
	SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
	SUM += log10(1013.246);
	double VP = pow(10, SUM-3) * humidity;
	double T = log(VP/0.61078);   // temp var
	return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
	double a = 17.271;
	double b = 237.7;
	double temp = (a * celsius) / (b + celsius) + log(humidity/100);
	double Td = (b * temp) / (a - temp);
	return Td;
}

/* ( THE END ) */

this is where im at thos this mash up is not working

what im trying to do is joing the light senor script to the DHT11 script

the idea is the light sensor is triggered and the main DHT11 script which will repeat 5 times then stop and will go back to the start and wait for the light trigger to be set off again

I haven't got that library and you didn't supply a link to it so I can't compile it all. However, you messed up on the loop() function, that line should read:-
void loop() { /----( LOOP: RUNS CONSTANTLY )----/

Sorry here is the link
It's at the bottom of the page
http://arduino-info.wikispaces.com/DHT11-Humidity-TempSensor

As for the void loop()
The void loop (void) was in the light sensor script I haven't seen that before so wad wondering if that was some type of way to name the setup

Thanks for help grumpy Mike

No it was the { that was missing. It is late here I will take a look in the morning.

Ah ok

Thanks Mike XD

This compiles, you had two Serial.begin()
Missed a { from the loop
Also had a else without a paired if()
Using lots of if() else if() constructs is bad, it is easy to get lost and hard to see what is going on. Use the switch statement for these sort of constructs.

/* YourDuino.com Example Software Sketch
   DHT11 Humidity and Temperature Sensor test
   Credits: Rob Tillaart
   http://arduino-direct.com/sunshop/index.php?l=product_detail&p=162
   terry@yourduino.com */
   
/* Photocell simple testing sketch. 
 
Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
 
For more information see www.ladyada.net/learn/sensors/cds.html */
   
/*-----( Import needed libraries )-----*/
#include <dht11.h>

/*-----( Declare objects )-----*/
dht11 DHT11;

/*-----( Declare Constants, Pin Numbers )-----*/
#define DHT11PIN 2


int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the analog resistor divider


void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("DHT11 TEST PROGRAM ");
  Serial.print("LIBRARY VERSION: ");
  Serial.println(DHT11LIB_VERSION);
  Serial.println();
  
  pinMode(13, OUTPUT);  
  pinMode(12, OUTPUT); 
  
  // We'll send debugging information via the Serial monitor
//  Serial.begin(9600); // Don't do this twice
}/*--(end setup )---*/

void loop()  { /*----( LOOP: RUNS CONSTANTLY )----*/

  photocellReading = analogRead(photocellPin);  
 
  Serial.print("Analog reading = ");
  Serial.print(photocellReading);     // the raw analog reading
 
  if (photocellReading < 10) { } // do nothing
  else if (photocellReading < 500) {
    Serial.println(" - START TESTING");
    {
   Serial.println("\n");

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);

if (DHT11.humidity < 80) {
   // do stuff if the condition is true
    digitalWrite(12, HIGH);   // set the ON_Relay on
    delay(1000);
    digitalWrite(12, LOW);   // set the ON_Relay off
} else {
   // do stuff if the condition is false
    digitalWrite(12, LOW);   // set the ON_Relay off
}
if (DHT11.humidity > 80) {
   // do stuff if the condition is true
    digitalWrite(13, HIGH);   // set the OFF_Relay on
    delay(1000);
    digitalWrite(13, LOW);   // set the OFF_Relay off
} else {
   // do stuff if the condition is false
    digitalWrite(13, LOW);   // set the OFF_Relay off
    
 }
}
    if (photocellReading > 499) {   // removed the else from the start of this line
    Serial.println(" - DO NOTHING");
}
  delay(500);
 } 
   delay(50);
  
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
	return 1.8 * celsius + 32;
}

//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
	return celsius + 273.15;
}

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm 
double dewPoint(double celsius, double humidity)
{
	double A0= 373.15/(273.15 + celsius);
	double SUM = -7.90298 * (A0-1);
	SUM += 5.02808 * log10(A0);
	SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
	SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
	SUM += log10(1013.246);
	double VP = pow(10, SUM-3) * humidity;
	double T = log(VP/0.61078);   // temp var
	return (241.88 * T) / (17.558-T);
}

// delta max = 0.6544 wrt dewPoint()
// 5x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
	double a = 17.271;
	double b = 237.7;
	double temp = (a * celsius) / (b + celsius) + log(humidity/100);
	double Td = (b * temp) / (a - temp);
	return Td;
}

/* ( THE END ) */

While void loop(void) is not wrong it is not necessary and you can just use void loop()

Brilliant thanks Mike

i will put this in and test it out.

yer your right about the ifs i think i could use 1 just for humidity simply if above 80% turn on on_relay else turn on off_relay.

also when i started to bring the light sensor script into the main one got lost quite quickly.

im guessing this isnt a good way round these sort of things.

i come from a 3d background and use a program call softimage which has something called ice and i do the same thing make small chunks of the program then bring them altogether.

anyway enough rambling

thanks for your help on this Mike

Hey
just to give yer a heads up

i have uplaoded it and works brill

i have removed a few lines in the script that wasnt needed

just neeed to get some light readings and humidity readings and its done.

once again cheers for your help Mike