At the start of the function you put the time given by millis() into an unsigned long variable.
Then the value returned by millis() minus that variable is the current time in milliseconds.
But you need a "trigger" to start the measurement of the time (if you want to read the real thing). So when you close your switch the Arduino must sense that the switch is closed. Right?
Then using the suggestion of Grumpy_Mike you can count the time. (or maybe you can use micros() instead of millis(), depending on the values of R and C)
I suspect "time" must be the interval between one calculation of the formula and the next (or perhaps the change in time over many calls to the function) so there must be a variable to keep track of the value between calls to the function. And the unit of measurement will affect the forumla as will the initial value. Some means will be required to convert the first reading of millis() to a suitable initial value for the first call to the function.
Robin2:
I suspect "time" must be the interval between one calculation of the formula and the next (or perhaps the change in time over many calls to the function) so there must be a variable to keep track of the value between calls to the function.
(...)
Hummm... I don't know if that is 100% right. To be exactly like that he has to calculate the differential equation in a different way, don't?
Robin2:
(...)
And the unit of measurement will affect the forumla as will the initial value. Some means will be required to convert the first reading of millis() to a suitable initial value for the first call to the function.
With this part I agree. If the unit is not seconds, the result will not be volts.
luisilva:
Hummm... I don't know if that is 100% right. To be exactly like that he has to calculate the differential equation in a different way, don't?
I certainly don't claim to be 100% right. I was just trying to push thoughts in a direction. But I think some element of what I said applies even if the equation is unchanged.
In any calculation time has to mean a different time from some other time. If not it just becomes a constant or a random number.
It looks to me like the op is trying to simulate the voltage you will see on a charging capacitor. Therefore time has to constantly increase past a specific trigger point where time = 0.
Another thing that I only realise now is that it can be used the value of mills() or micros() like it is. The only thing that the user must care about is to include the value of the time constant (tau=R*C) in the same unit. If he want to use the value of tau in milliseconds the value of C must be entered in miliFarads and R in Ohms, and if he want to use tau in microseconds he must to use C in microFarads and R in Ohms. Any combination that leaves to this result is valid too. That is, he can use tau in microseconds, R in kOhms and C in picoFarads too. Right?
luisilva:
Another thing that I only realise now is that it can be used the value of mills() or micros() like it is. The only thing that the user must care about is to include the value of the time constant (tau=R*C) in the same unit.
The user must also concern himself with the actual value of millis() or micros() when the function is first called as it may not be at 0 depending on what the program does before it gets to the point of calling that function. It may even be that millis() has different values at that point on different runs of the program.
loki3118:
However, I don't know how to implement time into this function.
Time is an input to the function. You evaluate the formula for a given value of time, and get a result. Evaluating for a different value of time gives a different result.
Where the values for time should come from would depend what you're using the function for.
I've done my best to implement what everyone has suggested. I've posted my code below. There is a problem with how I structured the two while loops. I believe that this isn't good coding practice but I am unaware of a proper way to implement my code.
Overview of what should be accomplished ( see the picture, a graph of voltage as a function of time):
while vout is less than or equal to vthresh, a specific function should be preformed, then once vout equals vthresh a new function should be preformed until vout equals zero.
#include <math.h>
const int inputPin = A0; // pin that the sensor is attached to
const int buttonPin = 2; // pin the pushbutton is attached to
int buttonState = 0; // variable for reading the pushbutton status
unsigned long time; // creates a time variable
float k; // volts/Amp Conversion ratio between output current and AD620 voltage output
const float istart = 2; // max current without blowing chip
const float itrip = 4; // maximum current
const float ttrip = 1.75; // seconds when running the maximum current
const float vthresh = 1.0; // threshold voltage at which the circuit trips the relay
const float tao = 5.10389; // RC time constant
const float ri = 510389; // input resistor to integrator
const float rf = 1280000; // feedback resistor
const float r2 = 500; // resistor connecting the Rg pins on the current-sensing AD620
const float Rsense = 0.01; // sense resistor
void setup()
{
Serial.begin(9600);
Serial.println("start");
Serial.println("\nvin:\ttime:\tvout:\tthreshvoltage:");
Serial.println("---------------------------------------------");
// sets up the voltage
float vin = getVoltage(inputPin); // reads voltage from the inputPin
float vout = 0; // initial output voltage is 0
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT);
// if button is pushed
if (buttonState == LOW)
{
time = micros(); // initializes the time
while(vout <= vthresh) // results in the integration of the input voltage
{
vout = ((1 - exp(-time/tao)) * (49400 + r2)^2 * rf * vin^2)/(10 * r2^2 * ri);
Serial.print(vin, 4);
Serial.print("\t");
Serial.print(time, 4);
Serial.print("\t");
Serial.print(voutincrease, 4);
Serial.print("\t");
Serial.print(vthresh, 4);
Serial.println("\t");
}
while (vout <= && vout >= 0) //results in the decay of the output voltage
{
vout = (exp(-time/tao)) * (((49400 + r2)^2 * rf * vin^2)/(10 * r2^2 * ri));
Serial.print(vin, 4);
Serial.print("\t");
Serial.print(time, 4);
Serial.print("\t");
Serial.print(voutdecay, 4);
Serial.print("\t");
Serial.print(vthresh, 4);
Serial.println("\t");
}
}
}
void loop()
{
}
float getVoltage(int pin)
{
return (analogRead(pin) * 0.004882814);
// This equation converts the 0 to 1023 value that analogRead()
// returns, into a 0.0 to 5.0 value that is the true voltage
// being read at that pin.
}
I left it blank because I wasn't sure if this was the best way to preform this action. If I put
while (vout <= vthreshold && vout >= 0) //results in the decay of the output voltage
[/code
however I do believe this would enter into the loop to decay the vout.
I don't like a few things in your code.
I don't like that you do something that is repeated in the setup(). You could do this in the loop, changing the while's by if's.
I believe that you will end with many, many values, and maybe some lines repeated. If you do this with if you can do something like what is done in the example BlinkWithoutDelay, and send to the serial line only the values that you want.
The button don't makes the system start counting time, only enables the calculation and the output.
I don't understand why you do the analogRead, as you only read this value once. That is, Are you sure that you don't need to read the analog value inside the loops?
What is the result that you have right now?
I only realise before the last replies that the result that you link in your post is not from your program.
EDIT:
You have a few other errors. In C language the operator ' ^ ' is not the power. Instead is the bitwise XOR. You need to call the function pow() to calculate the power: http://arduino.cc/en/Reference/Pow