Pages: [1]   Go Down
Author Topic: subroutines  (Read 841 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: January 10, 2013, 04:38:41 pm by Coding Badly » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Schematic?
Logged

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

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to see what you have got wired and how it is wired.
That is a schematic (circuit diagram).
Logged

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

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

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

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

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3726
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

thanks. It's working
Logged

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

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);
}
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3726
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
(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
Logged

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

thanks
Logged

Pages: [1]   Go Up
Jump to: