Offline
Newbie
Karma: 0
Posts: 4
|
 |
« on: December 25, 2012, 10:32:01 am » |
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!! 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
Edison Member
Karma: 37
Posts: 1828
|
 |
« Reply #1 on: December 25, 2012, 10:35:26 am » |
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
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #2 on: December 25, 2012, 10:40:41 am » |
Where in the code do I use millis()??
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #3 on: December 25, 2012, 10:50:02 am » |
Where in the code do I use millis()?? Where you want to know when something happened.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 31
|
 |
« Reply #4 on: December 25, 2012, 11:00:04 am » |
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: 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
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #5 on: December 25, 2012, 11:05:33 am » |
Thanks Postholes!
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 29
Posts: 2888
I only know some basic electricity....
|
 |
« Reply #6 on: December 25, 2012, 11:29:51 am » |
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 at Learning in the Main Site and at the Playground
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #7 on: December 25, 2012, 11:33:08 am » |
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
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #8 on: December 25, 2012, 12:19:58 pm » |
it could help? 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
Faraday Member
Karma: 29
Posts: 2888
I only know some basic electricity....
|
 |
« Reply #9 on: December 25, 2012, 03:54:49 pm » |
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 at Learning in the Main Site and at the Playground
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: December 25, 2012, 08:10:23 pm » |
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
|
|
|
|
|
|