To find time difference between two states

Hi all, Actually i know about millis function. My present scenario is as follows:

1.A process contains a small box(contains liquid) is inside a container. when the liquid comes below the level means it should turn on a LED(used float sensor here). i know the code to blow it. 2.when LED blows, i need to take out the box and and refill it with liquid. 3.I am having one switch(named as switch1) which defines the state either box is with container or not. if box is with container then the state is high if not then state is low. Code needed for following scenario: block a { if switch 1 is low, then i need to note down that time(named as time1). Then i take out and fill the box with liquid and i try to insert it in container. At that time switch 1 turns to high, then i need to note down that time(named as time 2). } if time2-time1 is > 10sec then i need to continue that process else i need to do go with block a.

Actually i am new to arduino, kindly help me with this code.

I’m not sure what you want to accomplish. But, I think you want to decide what the program does based on whether some particular amount of time has elapsed.

Look at the “Blink without Delay” sketch in the examples folder that came with your Arduino IDE. The path, relative to the IDE’s folder, is
  ./examples/02.Digital/BlinkWithoutDelay. I’ll recommend that you read that code, load it, test it, and play with it until you understand it. Then, the code that will manage the timing will be obvious to you.

There are various ways to measure the time between a switch changing from low to high. As you seem to be interested in a fairly long period (seconds rather than microseconds) something simple like this may be sufficient.

Every interation through loop() read the state of the switch. If it is high and it was high the previous time, do nothing. If it is low and was high the previous time record millis() into a variable called time1. If it is low and was low the previous time, do nothing If it is high and was low the last time record millis() into a variable called time2 subtract time1 from time2 do whatever is indicated by the difference.

...R

Robin,

You said the exact thing what i want. Actually my problem is not able to produce the code for that scenario, because i am new to arduino. Exact thing is i dont know how to take the previous state of a button. So can you please help me with a little bit of code. Thanks in advance.

Exact thing is i dont know how to take the previous state of a button.

Store the state each time you read it in a global or static variable.

You need to do a little of the work here or else you won't learn anything.

Try converting the steps I outlined into Arduino code and if it doesn't work post your code here and we will be happy to assist.

I presume you have worked through several of the examples that come with the Arduino IDE?

...R

I used separate variable to enter the loop. according to me it works fine. If there will be any error in my code means please let me know. Thanks in advance.

Code:

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins from the above pin mappings for LCD display
LiquidCrystal lcd(8,9,10,11,12,13);
const int switch1=7;
unsigned long time;
unsigned long time2;
void setup()
{
pinMode(switch1,INPUT_PULLUP);
lcd.begin(16, 2);// Beggining LCD display
}

void loop()
{
static int a;
static int b;
lcd.print(“void loop”);
lcd.clear();
if(digitalRead(switch1)==0 and a == 0)
{
lcd.print(“reading time1”) ;
delay(4000);
lcd.clear();
time = millis();
a=1;
b=1;
}
if(digitalRead(switch1)==1 and b == 1)
{
lcd.print(“reading time2”) ;
delay(4000);
lcd.clear();
time2 = millis();
a=0;
b=0;
if((time2 - time) > 10000)
{
lcd.print(“led off”);
delay(4000);
lcd.clear();
}
if((time2 - time) < 10000)
{
lcd.print(“led on”);
delay(4000);
lcd.clear();
}

}

}

  delay(4000);
  lcd.clear();
  time2 = millis();

Isn't it more sensible to timestamp the event before the the delay?

Please use code tags when posting code.

Glad you have it working.

I don't have an LCD so I can't try your code. And the LCD is irrelevant to your query. It's a good idea to use short sketches that don't use specialist parts when you are seeking advice so that others can try your code (unless of course the question is about the device).

...R

Inorder to check my code i used delay there.. And i used LCD to check my output, hence i used it.. Thankyou for your replies...