Problem in measuring voltage in arduino

i attempted to measure voltage at a point by opening and closing a mosfet switch with interrupt in arduino uno board. i need to measure the voltage after 100 ms from the instant of opening of the switch and to measure the voltage after 100 ms from the instant of the closing of the switch.

The interrupt exactly interrupt at every 4 seconds to open the switch but the problem is the opening time interval of the switch varies for every cycle. 20ms, 50ms etc.,. i attached the code. please help me to rectify the problem.

aurdinuo program.txt (388 Bytes)


If you post your code inline, with [ code ] tags, then you will get a lot more help.

What voltage is this system?

You have one timer that sets LOW a pin every 4 mln microseconds. No problem there.

Then you have a loop that waits 100 ms, takes a reading, sets the pin high, waits another 100 ms takes another reading, and starts all over again.

Two problems.
First: you say you want to take readings every 4 seconds, but you do it every 100 ms plus a bit.
Second: the delay(100) waits 100 ms, but the reading and setting of the port also takes time so you’re not in sync with your interrupt.

Have your interrupt set a flag, and respond to that in your loop by making the desired measurements.