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