if then

okay i am having a problem writing a if then statement. i need to have my statement say if light > 80 for 15min then do this. how would i go about write something like this. Thanks

When time is involved you are going to have to use variables to store state, for instance here you want to know if a certain condition has been true for 15 minutes.

This you do by regularly checking the condition, and if it becomes true (ie was false last time, is true this time) you store the current time (ie the value returned by millis ()).

Regular testing is something you code in the loop() function.

So something like:

unsigned long light_time = 0L ;

#define SECOND  1000L   // milliseconds in a second
#define MINUTE (60 * SECOND)

void loop ()
{
  if (light > 80)
  {
    if (millis () - light_time > 15 * MINUTE) // greater than 15 minutes (long constant)
    {
      ..
    }
  }
  else
    light_time = millis () ;  // can keep resetting it when condition false, a short cut
  ..
}

Note that you don't actually have to test for the condition changing if the timestamp variable is always reset when the condition is false and you are regularly rechecking - the elapsed time can only grow to 15 minutes if the condition holds for the duration.

int light;
long startTime, currentTime;

void setup()
{
   Serial.begin(9600);//Set up serial communications
   startTime = 0;
}

void loop() 
{
   light = ReadLightMeter();  // Whatever you do to measure light...
   if (light > 80 && startTime == 0)  // It is light enough to start timing?
      startTime = millis();  // Yep...
   else
      startTime = 0;  // Nope...
   currentTIme = millis();  // Get the current millisecond count
   if (currentTIme - startTime >= 900000) {  // 900000 milliseconds in 15 minutes
      Serial.println("It's been light for 15 minutes or more.");
   } else {
      Serial.println("Forecast for tonight: Dark");
   }
}

I haven't tried this, but something like this should work.

unsigned long light_time = 0UL ;
unsigned long now_time = 0UL; // IIRC default is zero but it's good to make sure

#define SECOND  1000UL   // milliseconds in a second --- all unsigned long constants must have UL attached
#define MINUTE (60UL * SECOND)

void loop ()
{
  if (light > 80) // where is light declared? is it an int? what is it for?
  {
// do you really want a time check inside another condition?
    now_time = millis(); // all now calculations will use the same number, waiting periods will be regular 
    if (now_time - light_time > 15UL * MINUTE) // greater than 15 minutes (long constant)
    {
      ..
    }
  }
  else
    light_time = now_time ;  // can keep resetting it when condition false, a short cut
  ..
}

EconJack, all time variables need to be unsigned long if you want to avoid rollover problems and don't mind halving the longest wait allowed, 49.7-some DAYS.

All unsigned long constants should end with a UL or the compiler will screw it up.

GoForSmoke: EconJack, all time variables need to be unsigned long if you want to avoid rollover problems and don't mind halving the longest wait allowed, 49.7-some DAYS.

All unsigned long constants should end with a UL or the compiler will screw it up.

You can use signed or unsigned, int or long, rollover will be OK if you use the idiom (a - b > interval). Its the subtraction that is key and it works the same for signed / unsigned. The caveat is that you must be consistent in the subtraction and not do this:

int timestamp ;
#define INTERVAL 6000

void loop ()
{
   ..  
   if (millis () - timestamp > INTERVAL)
     ...

You have to use two int variables:

int timestamp ;
#define INTERVAL 6000

void loop ()
{
   .. 
   int now = millis () ; // truncate to 16 bit 
   if (now - timestamp > INTERVAL)  // 16 bit subtract, 16 bit compare.
     ...

Signed variables halve the maximum delay you can handle, and int obviously reduces it to seconds rather than days.

So why use long where unsigned long does it better? Save on typing? How about

define Tvar unsigned long // Tvar short for timevar to save typing 3 chars and get accused of obscuration!

thank you this has help. Here is what i got right now. I know i need to go thought it and clean it up. I add so many thing and del a lot to so ill up load the finished code when i’m done.

/*
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)



EEPROM ADDRESSES
1- Ambient Light OFF
2- Ambient Light ON
3- Daylight Savings




 */
unsigned long light_time = 0L ;
unsigned long lighton_time = 0L ;

#define SECOND  1000L   // milliseconds in a second
#define MINUTE (60 * SECOND)
// include the library code:
#include <LiquidCrystal.h>
#include <EEPROM.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pagenumber = 0;
int lowbeam = 2;
int Halolights = 2;
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

const int numReadings = 10;
int lightsensor = 0;
int amblight = 0;
int amblightcurrent = 0;
int amblighton = 100;
int amblightoff = 200;
int minLight;
int maxLight;
int lightLevel;
int buttonleft = 13;
int buttonright = 9;
int relay1 = 10;
int relay2 = 7;
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

long previousMillis = 0;        // will store last time LED was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 10000;           // interval at which to blink (milliseconds)
int timereset = 0;

long viewrelay = 256;
long viewrelay2 = 256;
long viewrelay3 = 256;


void setup()
{
  Serial.begin(9600); 
  lcd.begin(16, 2);
  
  pinMode(10, OUTPUT);
  pinMode(13, INPUT);
  pinMode(9, INPUT);
  pinMode(7, OUTPUT);
  lcd.print("    honda    ");
    lcd.setCursor(0, 1);
    lcd.print("                    ");
    lcd.setCursor(0, 2);
    lcd.print("1991honda crx si ");
    
    delay(2000);
    lcd.clear();
    Serial.begin(9600);
 
 //Setup the starting light level limits
 lightLevel=analogRead(amblight);
 minLight=lightLevel-20;
 maxLight=lightLevel;
}



void loop () {

//auto-adjust the minimum and maximum limits in real time
 lightLevel=analogRead(amblight);
 if(minLight>lightLevel){
 minLight=lightLevel;
 }
 if(maxLight<lightLevel){
 maxLight=lightLevel;
 }
 
 //Adjust the light level to produce a result between 0 and 100.
 amblight = map(lightLevel, minLight, maxLight, 0, 220); 
 
 //Send the adjusted Light level result to Serial port (processing)
 Serial.println(amblight);
 
 //slow down the transmission for effective Serial communication.
 delay(100);

if (pagenumber==0) {

  lcd.setCursor(0, 0);
  lcd.print("LOWBEAM    Halos");
  lcd.setCursor(0, 3);
  
if (lowbeam==0) {
    lcd.print("ON  ");
  }
    if (lowbeam==1) {
    lcd.print("OFF ");
  }
      if (lowbeam==2) {
    lcd.print("AUTO");
      }
      
  lcd.print("       ");
  
  
    if (Halolights==0) {
    lcd.print("  ON");
  }
    if (Halolights==1) {
    lcd.print(" OFF");
  }
      if (Halolights==2) {
    lcd.print("AUTO");
  } 

}
  
        if (pagenumber==0)
        reading = digitalRead(buttonleft);
        if (reading == HIGH && millis() - time > debounce) {

 lowbeam = (lowbeam + 1);
     if (lowbeam==3)
         lowbeam=0;
}
    amblightoff = (amblightoff - 5); 
   EEPROM.write(1, amblightoff); 

if (lowbeam==1) {
   {
digitalWrite(relay2, LOW);
}  
  }
  
  
   if (lowbeam==0) {
    {
       digitalWrite(relay2, HIGH);
}  
    
   }
  
  if (lowbeam==2) {
      
       if (amblight < 80)
  {
    if (millis () - lighton_time > 5 * MINUTE) // greater than 15 minutes (long constant)
    {
      digitalWrite(relay2, HIGH);
    }
  }
    
     if (amblight > 80)
  {
    if (millis () - light_time > 15 * MINUTE) // greater than 15 minutes (long constant)
    {
      digitalWrite(relay2, LOW);
    }
  }
  else
    light_time = millis () ;  // can keep resetting it when condition false, a short cut
    digitalWrite(relay2, HIGH);
    
  }
    
  reading = digitalRead(buttonright);
        if (reading == HIGH && millis() - time > debounce) {

 Halolights = (Halolights + 1);
     if (Halolights==3)
         Halolights=0;
}
      if (Halolights==1) {
   {
      digitalWrite(relay1, LOW);
  }
  }
  
   if (Halolights==0) {
     digitalWrite(relay1, HIGH);
    {
    }
   }
  
  if (Halolights==2) {
      
        if (amblight > 80)
  {
    if (millis () - lighton_time > 5 * MINUTE) // greater than 15 minutes (long constant)
    {
      digitalWrite(relay1, HIGH);
    }
  }
       if (amblight > 80)
  {
    if (millis () - light_time > 15 * MINUTE) // greater than 15 minutes (long constant)
    {
      digitalWrite(relay1, LOW);
    }
  }
  else
    light_time = millis () ;  // can keep resetting it when condition false, a short cut
    digitalWrite(relay1, HIGH);
    
  }
  }

You need to learn about arrays and indexes. One of the example programs in your IDE covers that, those are the updated versions to match your IDE, use them. http://arduino.cc/en/Tutorial/HomePage#.UxU-E0gz3jI

Section 5 Control Structures, Arrays is 3rd down and if you don't know for-next, while, or do-while loops and switch-case then have a look at those.

Those are basic tools to organize and process mass data with.

Do you know about PROGMEM?