# diference between two values & retain max and min value

hi guys
i am working on a sd logger

but i cant get it to save on sd or serial the lowest and the maximum value of the input

also, how can i do that if the diference between two values is X to do Y
for example if value is of by 20 from input a and input B do this or that?

another thing, i have done that the logger creates 2 files and work on them both (one for easyer excel work)

``````// Create/Open file
myFile = SD.open("DATA-LOG.txt", FILE_WRITE);
myFile2 = SD.open("RAW.txt", FILE_WRITE);

if (myFile) {
Serial.println("Writing to file...");
// Write to file
myFile.println("test file1");
myFile.close(); // close the file
Serial.println("Done.");
``````

is there any other way to create 2 files without repeating all the code?

thanks

Post all the code. Snippets are useless.

Danny1000:
but i cant get it to save on sd or serial the lowest and the maximum value of the input

You should save all values, not just the intermediates, so the lowest and maximum will also be there.

also, how can i do that if the diference between two values is X to do Y
for example if value is of by 20 from input a and input B do this or that?

Use an if statement.

``````if (a - b == 20) { // only has a chance of working of both a and b are integers
this();
}
else {
that();
}
``````

Hi, that worked half way for me.
i still dont know how to save//retain the maximum and minim value, how can i do this to save the peaks in both ways?

here i have the following problem, the (a-b >= x) works as long the value is greater than, the function doenst work one the vale is lesser than <, i have timed the events and did try various values and had no luck, here is the full code
i need to be able to store the max and mins
also i need to be able to time a evente, lets say i have a sensor giving an output signal from 0 to 500, i need to time how much time it took to get to 500…need help here to time this event.

``````int LED =A0; //havent got the jumper cables...testing direct on the board
int pot = A2;
int CRLY= 3;
int HLA= 4;

unsigned long startMillis; // global value available in the program anywhere
unsigned long currentMillis;
const unsigned long period = 150; // the periot
unsigned long A;
unsigned long B;

void setup() {
// put your setup code here, to run once:
Serial.begin (115200);
pinMode (LED,OUTPUT);
pinMode  (pot,INPUT);
pinMode (CRLY,OUTPUT);
pinMode (HLA,INPUT);
startMillis = millis(); // inital start time
digitalWrite(LED, HIGH);
delay (60);
digitalWrite (LED,LOW);
A = millis ();
B = millis();

}

void loop() {
// put your main code here, to run repeatedly:

currentMillis = millis(); // get the current time

Serial.println();
Serial.print ("HLA ="),Serial.println(HLA);
Serial.println();
Serial.print("current millis ="),Serial.println (currentMillis);
Serial.println();
Serial.print("start millis ="),Serial.println (startMillis);
Serial.println();
// Serial.print("sensorvalue = "),Serial.println(sensorvalue);
Serial.println();
Serial.print ("A value =>>>> "),Serial.println(A);
Serial.print ("B vaule =>>>> "),Serial.println(B);

A = millis(); // take time
Serial.print("sensorvalue = "),Serial.println(sensorvalue);
delay (200); // simulate function time
B = millis(); // time taken to reach value
Serial.print ("A after read =>>>> "),Serial.println(A); //time after event
Serial.print ("B after read =>>>> "),Serial.println(B); //time after event

//startMillis=currentMillis;

if
(  A - B >= 100 && sensorvalue <= 200)  //  A - B <= x doesnt work, have tested and compared values, should work...but doesnt
digitalWrite (LED,HIGH),
delay (20),
digitalWrite (LED,LOW);

else
digitalWrite(LED,LOW);

thanks guys

}
``````

Edited (cut) code snippet:

Danny1000:

``````  A = millis(); // take time
``````

delay (200); // simulate function time
B = millis(); // time taken to reach value
if (A - B >= 100)  //  A - B <= x doesnt work, have tested and compared values, should work…

A - B <= x definitely works, but probably not how you expect it to. The thing is, B is greater than A, so you probably expect the result to be negative. Yet as A and B are unsigned long, the actual result of this calculation is 2^32 - 200 = 4294967096. That’s the magic of integer math, and the same thing we rely on to do things like

``````if (startTime - millis() > interval) {
}
``````

and not caring about when millis() overflows!

Also, do use proper variable names, not A and B. But something like startTime and endTime. Then it becomes quite natural to calculate endTime - startTime get the elapsed time…

ok, will test this and make code cleaner.
How can i log on serial, maximum values and minimum values.
also, how can i calculate the time of a value?
for example i am running a hall and i get a value up to a 100 at different speeds, how can i time the time it takes?
the way i have written the code of

A millis
function
B millis

it would just tell me how much the board took to get to B but not how much time has pased for function to reach a desired value?

while writing this i have tested some things and changing the order from a - b to b -a with help of serial to get values it shed some light over the error

here is the fix

``````  Serial.print ("Differential = "),Serial.println(B-A);

//startMillis=currentMillis;

if
(  B - A <= 51) // && sensorvalue <= 200)
digitalWrite (LED,HIGH),
delay (20),
digitalWrite (LED,LOW);
``````

For a maximum/minimum/time, just record the time when you last saw a new larger or smaller value:

``````int max = 0;
int min = 1024; // max possible - this would be for an ADC reading.
uint32_t maxTime;
uint32_t minTime;

void measureFunction() {
for (int i = 0; i < nReadings; i++) {
if (value > max) {
max = value;
maxTime = millis();
}
if (value < min) {
min = value;
minTime = millis();
}
}
}
``````

i was testing it and got some errors

error: ‘nReadings’ was not declared in this scope

for (int i = 0; i < nReadings; i++) {

^

sketch_jun19a:8:29: error: ‘takeReading’ was not declared in this scope

N of readings ok, the value i am going to give
but how do get the reading of a hall sensor to be int value = x? can it be int value = hallSensor(); ?

thank you
^

Well... duh... of course you get errors... it's an example only. Make sure you understand what it's doing first.

That takeReading() function takes a reading of the sensor, whatever sensor, and returns the value. Amend the function to match the variable to whatever your sensor reading function returns. Or replace it by an analogRead(), of whatever makes sense for your specific situation.

thanks,im still green with this, will give it a try and post the code once done

thanks