Need guidance on Timer/Delay Problems.

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

fkeel:
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

i read up on millis() - Arduino Reference , they used delay(1000); under the example

unsigned long time;

void setup(){
  Serial.begin(9600);
}
void loop(){
  Serial.print("Time: ");
  time = millis();
  //prints time since program started
  Serial.println(time);
  // wait a second so as not to send massive amounts of data
  delay(1000);
}

Nick's post mentioned

we can't afford to use:
delay (1000);
... because this blocks. That is, you can't be doing anything else during those 1000 milliseconds.
Instead we note the time, like this:
unsigned long when_eggs_started;  // declare variable
when_eggs_started = millis ();  // note the time now

im confused