Go Down

Topic: Help with simple interval timer. (Read 2 times) previous topic - next topic

1885

Mar 03, 2013, 05:06 am Last Edit: Mar 03, 2013, 05:39 am by 1885 Reason: 1
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: [Select]

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++;
      }

     }
 
 }

cwc 1885

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

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
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Quote

Code: [Select]

if ((buttonState == HIGH)&&(count==1)) {     
            if (count==1)



count would always be 1, wouldn't it (in the second "if").
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

1885


Quote

Code: [Select]

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.
cwc 1885

1885


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.
cwc 1885

PaulS

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.

1885


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.
cwc 1885

Nick Gammon


 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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up