Go Down

Topic: subroutines (Read 957 times) previous topic - next topic

guidoc

Jan 10, 2013, 10:31 pm Last Edit: Jan 10, 2013, 10:38 pm by Coding Badly Reason: 1
Hi,
I'm new here and also new in programming arduino.
First project I started is a small home control system.
I want to program a subroutine for an time relay.
When I put the commands in the void loop, everything is working except that the output comes high after reset.
When i put the same commands in a subroutine, the output stays high after pushing the buttom ones.


Code: [Select]
int drukknop = 22;
int LED = 13;
long starttijd;

void setup()
{
 pinMode(LED,OUTPUT);
 pinMode(drukknop,INPUT);
 //digitalWrite(LED,LOW);
}

void loop()
{
   if (digitalRead (drukknop) == LOW) {
       starttijd = millis();
       tijdrelais();
           }
}

void tijdrelais()
{
if ((millis()- starttijd) < 2000){digitalWrite (LED,HIGH);}
        else {digitalWrite (LED,LOW);
}
}



Moderator edit: [code] [/code] tags added.

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

guidoc

what do you mean ?

I want that after pushing the button, the output wil stay high during 5 minutes and then commes low again. (in the test it is 2 sec).

AWOL

I want to see what you have got wired and how it is wired.
That is a schematic (circuit diagram).
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

guidoc

only digital pin 22 is wired with a push button between ground and input and a pull up resistor of 1 K to 5V
The board is detecting the change of the button that's not the problem.

guidoc

Vin: 5V---*
                  |
                  \
                  R     1 K
                  /
                  |
                  |
        22----*
                  |
                  |
                     |    push button
                  | 
                  |
                  |
                  *----> Ground

wildbill

You only call tijdrelais when the button is pressed. Move it out of the body of the if.

guidoc

thanks. It's working

guidoc

the only problem is that after reset op the arduino. The relay is going on for the programmed time. Why, I don't know because I force him low in the setup.

char drukknop = 22;
char LED = 13;
long starttijd;

void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(drukknop,INPUT);
  digitalWrite(LED,LOW);
}

void loop()
{
    if (digitalRead (drukknop) == LOW) {
        starttijd = millis();
             }
            tijdrelais();
}

void tijdrelais()
{
if ((millis()- starttijd) < 10000)
       digitalWrite (LED,HIGH);
      else
       digitalWrite (LED,LOW);
}

wildbill

It's because when you reset the arduino, millis is zero and so for the first ten seconds, this will be less than 10000:
Code: [Select]
(millis()- starttijd

One way to solve it is to turn the relay on when you detect the button press and use your subroutine to turn it off when millis()- starttijd is greater than 10K


Go Up