Pages: [1] 2   Go Down
Author Topic: Need guidance on Timer/Delay Problems.  (Read 1608 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys . Have some questions to ask
My program is currently like this.
Code:
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
« Last Edit: July 17, 2012, 09:50:27 pm by ahpohbear » Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
byte runXTimes = 1;

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

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: July 12, 2012, 10:53:09 pm by Arrch » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
const int inputPin1= 6;
const int testMode = 7;
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46247
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 .
Logged

Offline Offline
God Member
*****
Karma: 2
Posts: 711
a, b = b, a+b
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


you need something like this:

Code:
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;
  }
}


Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


you need something like this:

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

need some help on timer/delay problems for my current prj..
Feel free to elaborate
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 .
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

stuck with the delay as it affects my whole program .

I didn't understand that "sentence".
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..
Code:
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.
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 http://gammon.com.au/blink.
Logged

Pages: [1] 2   Go Up
Jump to: