the interval can be what ever. Ultimately it will determine the sensitivity of the result. THIS CODE DOES NOT WORK I am asking for help constructing code that will allow me to compare
(sensorValNow-SensorValLast) = result
How should the code be written to achieve this?
I was hoping i could use the bones of the "blink with out delay" sample, but i was having trouble writing a value after the interval was over
(1) make and save measurement1
(2) record time1
(3) do something else for a while
(4) make and save measurement2
(5) record the time2
(6) compare measurements 2 and 1 and if needed, compute time2 - time1
Here is what I have. It compiles, and writes a result to the serial monitor. problem is the result is always "0.00". I thought maybe declaring the sensor as an INPUT was giving me trouble so i commented it out. no change.
unsigned long time = millis();
int sensorValNow = 0;
int sensor = A0;
void setup()
{
//pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop() {
float sensorValNow;
float sensorValLast;
float result;
sensorValNow = analogRead(sensor);
{
if (millis() - time > 1000) //Has one second passed?
{
sensorValLast = sensorValNow;
time = millis(); // reset time.
result = (sensorValNow - sensorValLast);
Serial.println(result);
}
}
}
Is your question about scope? You want loop() to run for a while and don't know how to store the previous value outside loop?
Try a global variable or static variable.
I didn't try to compile this code so it could be rife with syntax errors. It's more to figure out what question you are asking. This is my guess at what I think you are asking.
Global Variable
int prevVal = 0; //This variable is global scope and can be accessed by the functions below.
int compareVals(int x, int y){
//make whatever comparison you like
if(x == y)
return 0;
else
return 1;
}
loop(){
int localVal;
localVal = digitalRead(PIN_FOR_INPUT);
if( compareVales(localVal, prevVal) ){
//do some stuff
}
prevVal = localVal;
}
Static Variable
int compareVals(int x, int y){
//make whatever comparison you like
if(x == y)
return 0;
else
return 1;
}
loop(){
static int prevVal = 0; //This variable is local scope but is saved in memory where globals are and will persist (hold the value) until the scope comes back to the function that declared it.
int localVal;
localVal = digitalRead(PIN_FOR_INPUT);
if( compareVales(localVal, prevVal) ){
//do some stuff
}
prevVal = localVal;
}
Every time loop() "loops", these variables are recreated new.
If that is not what you wish, either make those variables global (put the declarations outside of the setup() and loop() functions), or make them static, as suggested above.
But then you need to move the assignment to sensorValLast down a bit.
by just changing the float to static float, it did start producing a result. but without scaling the analog input should read from 0-1023 representing 0-5V correct? From one end of the potentiometer to the other my result is 5 or -5. how can this be if the sensor input is not scaled?
it also incriments by 1.00. so far i have seen results of 1, 2, 3, 4, 5, 0 -1,-2,-3,-4,-5