Pages: [1]   Go Down
Author Topic: Time between button press  (Read 738 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have wired a microswitch to Pin 1 of the Mega. I need to use it as ON/OFF toggle to drive a load through TIP122, and display the time load was switched ON for. Can someone help me in modifying the Toggle LED code to measure time (in seconds) between ON/OFF?
Thanks!!
Code:
int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers
long dur_on = 0;    //the duration between ON/OFF

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  reading = digitalRead(inPin);
  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;


  }
  dur_on = (millis() - time)/1000;
  Serial.println(dur_on);
  digitalWrite(outPin, state);


  previous = reading;
}
Logged

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

Use millis() to record the time it was pressed. On the next press subtract that time from the new value of millis() to get the time elapsed
Logged

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

Where in the code do I use millis()??
Logged

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

Quote
Where in the code do I use millis()??
Where you want to know when something happened.
Logged

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

If I understand your question right, this might do the trick. I just added some code to your loop.
I didn't run this, there could be a bug or a problem with my logic, however something like this
might steer you in the direction you're looking to go:


Code:
int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = LOW;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers
long dur_on = 0;    //the duration between ON/OFF

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
reading = digitalRead(inPin);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce)
{
time = millis();

if (state == HIGH)
state = LOW;
else
state = HIGH;
}

// if the input is high then remember the time and print duration
// else ignore time reading and continue with loop
if ((reading == HIGH) && (millis() - time > debounce))
{
dur_on = (millis() - time)/1000;
Serial.println(dur_on);
}

digitalWrite(outPin, state);


previous = reading;
}
Logged

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

Thanks Postholes!
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4012
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You know that your accuracy will be no better than 2x the switch debounce?

If you need better then perhaps think about light beam interrupt or other short-or-no-bounce sensor?

 
Logged

Examples can be found in your IDE.

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

You know that your accuracy will be no better than 2x the switch debounce?

If you need better then perhaps think about light beam interrupt or other short-or-no-bounce sensor?

 


It's not a high accuracy application, so I am using the Bounce library to debounce for about 200ms.. That serves the purpose
Logged

Casorezzo, Milan, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it could help?

Code:
const int inPin = 2;
const int outPin= 13;
int elapsedtime=0;

int ledstate =LOW;
int switchstate = LOW;
int previous = LOW;

long prevtime = 0;
long debounce = 200;
long dur_on = 0; //duration between on/off

void setup(){
  pinMode(inPin,INPUT);
  pinMode(outPin,OUTPUT);
  Serial.begin(9600);
}

void loop(){

  switchstate=digitalRead(inPin);
  if ((millis()-prevtime)>debounce){
    if(switchstate!=previous && switchstate==HIGH){
      if (ledstate==LOW){
        digitalWrite(outPin,HIGH);
        prevtime=millis();
        ledstate=HIGH;
      }
      else if(ledstate==HIGH){
        digitalWrite(outPin,LOW);
        ledstate=LOW;
        Serial.print("time between On and off state");
        elapsedtime=((millis()-prevtime)/1000);
        Serial.println(elapsedtime);
         prevtime=millis();
      }
    }
  }
}
     
       
 

it works, but the value printed in the serial monitor, is always an integer, and even initializing the "elpasedttime" as a float, always ineger values are returned.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4012
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You know that your accuracy will be no better than 2x the switch debounce?

If you need better then perhaps think about light beam interrupt or other short-or-no-bounce sensor?

 


It's not a high accuracy application, so I am using the Bounce library to debounce for about 200ms.. That serves the purpose

It is -only- a problem if you expect more accuracy. I meant that in a goal-engineering sense.

Logged

Examples can be found in your IDE.

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

Quote
it works, but the value printed in the serial monitor, is always an integer, and even initializing the "elpasedttime" as a float, always ineger values are returned.
You are subtracting one integer value from another, and dividing the integer result by an int. Why you would expect a non-integer result is the mystery. Now, if you divided by 1000.0, instead...
Logged

Pages: [1]   Go Up
Jump to: