Pages: [1]   Go Down
Author Topic: Help with simple interval timer.  (Read 1926 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've copied and modified the script in this post with an attempt to create an interval timer.
Right now I'm only looking for a beginning and an endding time.  Two inputs.
A single or a series of push button spring loaded switches are attached to
5V and ground along with PIN 2.  I also used a 1K resistor prior to ground.


When I push on the button I get a stream of data from PIN 2.
How can I collect just two inputs?
Code:
const int buttonPin = 2;     // the number of the pushbutton pin
unsigned long timeTotal = 0;
unsigned long time0;
unsigned long time1;
int count = 0;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  // initialize the LED pin as an output:

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if ((buttonState == HIGH)&&(count==1)) {    
            if (count==1)
              {time1 = micros();    time1 = micros();
                Serial.println("BUTTON DOWN 1");
                   count=0;
              }
    

      }
  
  
  
  if ((buttonState == HIGH)&&(count==0)) {    
       if (count==0) {time0 = micros();
            Serial.println("BUTTON DOWN 0");
          count++;
       }
 
      }
  
  }
« Last Edit: March 02, 2013, 11:39:05 pm by 1885 » Logged

cwc 1885

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Read this before posting a programming question


Code tags please.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I push on the button I get a stream of data from PIN 2.
How can I collect just two inputs?

Read this about detecting transitions:  http://www.gammon.com.au/switches
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
if ((buttonState == HIGH)&&(count==1)) {     
            if (count==1)

count would always be 1, wouldn't it (in the second "if").
Logged


USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
if ((buttonState == HIGH)&&(count==1)) {     
            if (count==1)

count would always be 1, wouldn't it (in the second "if").

Ya.  I'm nit sure why I did that.  Bad move.
Logged

cwc 1885

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When I push on the button I get a stream of data from PIN 2.
How can I collect just two inputs?

Read this about detecting transitions:  http://www.gammon.com.au/switches

thanks for the link.  this is excellent reading!!
give me some time to play around then I'll post if I solve the problem.
Logged

cwc 1885

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49372
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
A single or a series of push button spring loaded switches are attached to
5V and ground along with PIN 2.  I also used a 1K resistor prior to ground.
This description makes no sense. A schematic would be necessary.

Using the internal pullup resistor, and connecting one leg to ground and one leg to the pin would be simpler.
Logged

USA
Offline Offline
Newbie
*
Karma: 0
Posts: 24
Uno Otto Otto Cinque
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
A single or a series of push button spring loaded switches are attached to
5V and ground along with PIN 2.  I also used a 1K resistor prior to ground.
This description makes no sense. A schematic would be necessary.

Using the internal pullup resistor, and connecting one leg to ground and one leg to the pin would be simpler.

Thanks for the help.
This is the simple schematic.  I've still got a lot to learn about Arduino.  I rely do not understand the concept of a pullup resistor.[among other things]
All I want to do is have an arduino become and interval timer.  I'm also experimenting with photo resistors and IR.


* button_schem.png (25.75 KB, 528x453 - viewed 30 times.)
Logged

cwc 1885

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

 I rely do not understand the concept of a pullup resistor.

The resistor gives the pin a "default" state, rather than an undefined state, if the switch is open.
Logged


Pages: [1]   Go Up
Jump to: