Need guidance on Timer/Delay Problems.

Hi guys . Have some questions to ask
My program is currently like this.
Code:

#include <Servo.h> 

Servo myservo;

const int inputPin = 3; 
const int ledPin1 =  4;
const int ledPin2 =  5;
const int inputPin1= 6;
const int testMode = 7;

int pos = 0;
boolean executeOnce = true;

int hour = 0;
int min = 0;
int sec = 0;


void setup() {
  myservo.attach(8);
  myservo.write(0);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(inputPin, INPUT);
  pinMode(inputPin1, INPUT);
  pinMode(testMode, INPUT);
  Serial.begin(9600);

}

void loop(){ 
  
   delay(1000);

  sec = sec + 1;
  
 if (digitalRead(testMode) == HIGH  && digitalRead(inputPin1) == HIGH)           // Off All led . Default Mode 48 hour.
   {   
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);

    }
  
 else if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == LOW && digitalRead(inputPin) == HIGH)    //24 Hour
   {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
   }
   
 else if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == LOW && digitalRead(inputPin) == LOW)     // 12 Hour
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
   }
   
 else if (digitalRead(testMode) == LOW && digitalRead(inputPin) == HIGH)           // TestMode
{ 
 digitalWrite(ledPin1, HIGH);
 delay(250);   
 digitalWrite(ledPin1, LOW);
 delay(250);
}

 if (digitalRead(testMode) == LOW && digitalRead(inputPin) == LOW)   // Testmode
 {
   if(executeOnce)
 {
 digitalWrite(ledPin2, HIGH);
 delay(500);   
 digitalWrite(ledPin2, LOW);
 delay(500);    
 
     
      for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                   // waits 15ms for the servo to reach the position 
  } 
   
    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                       // waits 15ms for the servo to reach the position 
  } 
  
executeOnce = false;
 }
}
  else if ((digitalRead(testMode) == LOW && digitalRead(inputPin) == LOW) == false) {
    executeOnce = true;
  }

Serial.print(hour);
  Serial.print("Hour");
  Serial.print(min);
  Serial.print("Min");
  Serial.print(sec);
  Serial.print("SEC");
}

If i write the program like this , there will be a 1 second delay in everything i do due to the delay(1000).
EG. when i press button for led . it will only light up / turn off after 1 second .
But i added the delay just for the timer so that it will count 1 sec by 1 sec.
If i dont add the delay(1000) , the seconds will be spamming my serial monitor as there is no delay.
Any idea on what can i do about it ? so that the delay is just for the timer. instead of affecting my LEDs/Servo/Pushbuttons.

Another question is that how can i turn my servo 180deg every 24 hour , hold for one minute and turn back to 0deg ?
i'm making a plant watering system so that the servo turn 180deg to let water flow through from the water tank for 1min
after which the servo will turn back to 0deg . and it will constantly do this every 24 hour as long as the 24 hour mode is activated

Thanks

byte runXTimes = 1;

This looks to me like code that tells it how many times it should run...

Arrch:

byte runXTimes = 1;

This looks to me like code that tells it how many times it should run...

yes . but if i remove the byte runXTimes = 1; and runXTimes--
when the condition is satisfied , the led will light and servo will turn . and it repeats even though i did not press the button again.
i have to make it till whereby the condition will work once whenever i press the button. but for my current code above ,
it works once when i press the button . when i press again its like deactivated , nothing's working.

Why do you have an else if as the first statement in your loop? else if should only come after an if block.

When that gets fixed, and assuming the issue is the same, then it's running more than once means your two conditions are being met. What exactly, are you reading a digital input from?

Arrch:
Why do you have an else if as the first statement in your loop? else if should only come after an if block.

When that gets fixed, and assuming the issue is the same, then it's running more than once means your two conditions are being met. What exactly, are you reading a digital input from?

that is just the part of my program which i have problem with . it comes after a if statement.
im reading the digital input from

const int inputPin1= 6;
const int testMode = 7;

that is just the part of my program which i have problem with

When you post ALL of your code, and describe the problem in more detail, we'll be in a better position to help you.

That code may, or may not be, where the problem really is.

PaulS:

that is just the part of my program which i have problem with

When you post ALL of your code, and describe the problem in more detail, we'll be in a better position to help you.

That code may, or may not be, where the problem really is.

i've posted the full code. the problem is at the last part . whereby if i remove the runXTime , it will keep on running the else if part
continuously . what i would like to acheive is when the else if statement is satisfied , it will run those statements inside the else if once .
i added in the runXTimes , it do run the else if statement once . but when the else if statement is satisfied again , it does not run the statement anymore , unless i reset the whole program .

you need something like this:

boolean executeOnce = true;

void setup(){

  if (conditionMet) {
    if (executeOnce) {
      //do whatever you want to happen once here
      executeOnce = false;
    }
  } 
  else if (conditionMet == false){
    executeOnce = true;
  }
}

fkeel:
you need something like this:

boolean executeOnce = true;

void setup(){

if (conditionMet) {
    if (executeOnce) {
      //do whatever you want to happen once here
      executeOnce = false;
    }
  }
  else if (conditionMet == false){
    executeOnce = true;
  }
}

thank you very much for your help ! much appreciated . =)
helped me alot.

need some help on timer/delay problems for my current prj..

ahpohbear:
need some help on timer/delay problems for my current prj..

Feel free to elaborate

Arrch:

ahpohbear:
need some help on timer/delay problems for my current prj..

Feel free to elaborate

the problem is posted on the thread . stuck with the delay as it affects my whole program .
and servo can't be programmed to coordinate with the timer. like activating the servo every 12 hours .

ahpohbear:
stuck with the delay as it affects my whole program .

I didn't understand that "sentence".

there will be a 1 second delay in everything i do due to the delay(1000) i written under void loop.
EG. when i press button for led . it will only light up / turn off after 1 second .
and for led blinking part its also abit lag due to the delay(1000) . i wrote delay (1000) for the timer ,
so that when i open the serial monitor on arduino . it will show the time sec by sec . if i remove the delay(1000),
my led and pushbutton will not lag for 1 second. but the timer will spam on the serial monitor..

void loop(){ 
 delay(1000);
 temp_in_kelvin = analogRead(0) * 0.004882812 * 100;
 temp_in_celsius = temp_in_kelvin - 2.5 - 273.15; 
 
  sec = sec + 1;
 
 if (digitalRead(testMode) == HIGH  && digitalRead(inputPin1) == HIGH)           // Off All led . Default Mode 48 hour.
   {   
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);

    }
  
 else if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == LOW && digitalRead(inputPin) == HIGH)    //24 Hour
   {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
   }
   
 else if (digitalRead(testMode) == HIGH && digitalRead(inputPin1) == LOW && digitalRead(inputPin) == LOW)     // 12 Hour
   {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
   }
   
 else if (digitalRead(testMode) == LOW && digitalRead(inputPin) == HIGH)           // TestMode
{ 
 digitalWrite(ledPin1, HIGH);
 delay(250);   
 digitalWrite(ledPin1, LOW);
 delay(250);
}

 if (digitalRead(testMode) == LOW && digitalRead(inputPin) == LOW)   // Testmode
 {
   if(executeOnce)
 {
 digitalWrite(ledPin2, HIGH);
 delay(500);   
 digitalWrite(ledPin2, LOW);
 delay(500);    
 
     
      for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                   // waits 15ms for the servo to reach the position 
  } 
   
    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5);                       // waits 15ms for the servo to reach the position 
  } 
  
executeOnce = false;
 }
}
  else if ((digitalRead(testMode) == LOW && digitalRead(inputPin) == LOW) == false) {
    executeOnce = true;
  }

  if (sec > 59) {
    sec = 0;
    minute = minute + 1;
  }
  if (minute > 59) {
    minute = 0;
    hour = hour + 1;
  }
  if (hour > 23) {
    hour = 0;
  }
  
Serial.print(hour);
  Serial.print("Hour ");
  Serial.print(minute);
  Serial.print("Minute ");
  Serial.print(sec);
  Serial.print("Sec ");
  Serial.print("Celsius: ");
  Serial.println(temp_in_celsius);
  Serial.println();

the delay 1000 is lagging my program , but if i don't add the delay 1000 my timer will be spamming and not counting second by second
What i would like to achieve is whereby i can write delay(1000) for the timer and it will not lag my pushbuttons/led lighting/blinking up.

As always, at this point, I would direct you to the BlinkWithoutDelay example sketch provided in the IDE, and Nick's page on the subject Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs.

dxw00d:
As always, at this point, I would direct you to the BlinkWithoutDelay example sketch provided in the IDE, and Nick's page on the subject Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs.

i read up on it and i guess im under a term called blocking ? how would i rewrite my program in the post so it won't block my led and pushbuttons for 1 second ?

void setup () 
  {
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  greenLEDtimer = millis ();
  redLEDtimer = millis ();
  }  // end of setup

void toggleGreenLED ()
  {
   if (digitalRead (greenLED) == LOW)
      digitalWrite (greenLED, HIGH);
   else
      digitalWrite (greenLED, LOW);

  // remember when we toggled it
  greenLEDtimer = millis ();  
  }  // end of toggleGreenLED

what's the use of writing void toggleGreenLED() after void setup() ?
and under void () loop there's this

 if ( (millis () - greenLEDtimer) >= greenLEDinterval)
     toggleGreenLED ();

^ means that it will call the program ? as in toggleGreenLED(); will activate the coding under void togglegreenled()?
i'm still new to programming and only have basic coding skills..

The BlinkWithoutDelay sketch, and Nick's page, show how to perform tasks at different times, without using delay(). In your case, you want to print the time every second. That's what you can do with the technique shown.

dxw00d:
The BlinkWithoutDelay sketch, and Nick's page, show how to perform tasks at different times, without using delay(). In your case, you want to print the time every second. That's what you can do with the technique shown.

Hi there , i;ve modified the post above your reply . Hope you can clear my doubts. Thanks

can anybody teach me what to do :confused: so stucked..

toggleGreenLED() is a function which takes no parameter and has no return type. read more bout functions here: http://arduino.cc/en/Reference/FunctionDeclaration

edit: you dont really need to write your own functions though, to figure out your problem. try studying and understanding the blink without delay example - its really all you need. specifically the millis() function should give you a tool to figure it out. read up on it here: millis() - Arduino Reference