# How to find maximum and minimum values

I have photoresistor hooked up to the analog A1 in the arduino.
For a period of roughly .5 seconds I would like to collect analogRead values fom this sensor.

At the conclusion of the .5 seconds I am trying to determine the maximum value recorded, as well as the minimum value.

I have been thinking on it alot, and can't seem to figure out a method of collecting these min and max values. Also not sure if this matters, but the min values will be negative, while the max values will be positive, or least negative.

I am very appreciative if anyone has some suggestions on how to acquire these values

Thanks guys/girls

Analog values on the Arduino are positive integers in the range 0 to 1023.
Start by setting a "max" variable to 0 and a "min" variable to 1023.
Each time you read an analog value, if it is less than "min" change min to the new value. If it is greater than "max" change max to the new value.
After .5 seconds print out the value of max and min.

Pete

Analog pins can't read negative values, only 0 to 5 volts.

El_supremo wow this awesome thank you very much, I really appreciate it!

I am still a bit confused as to how to code for the time interval. I am trying write some statement that will employ the method given by El_Supremo, but starting at a given time and ending say, .5 seconds later.

This part of the code comes after an 'if' statement, so basically if that if statement is satisfied, i need to "start the clock" and collect values and return the minimum and maximum value recorded over the .5 seconds

All and every suggestion is appreciated

Post what you have so far.

here is what i have so far:
Thank you very much for suggestions

``````//sketch designed to find the max and min of values recorded
//by analogRead over some time interval.

void setup(){
Serial.begin(9600);
int maximum=0
int minimum=1023}

void loop(){
if (sensorValue<minimum){  // determine minimum value
minimum=sensorValue;
}
if (sensorValue>maximum){
maximum= sensorValue;
}

}
``````

Check out the “Blink without Delay” example that came with the IDE. That will show you how to set up the timing.

Thank you very much Jackwp and Delta_G

While there are a lot of errors in terms of braces and semicolons, you need to learn what "scope" means. Until you do, you will have a hard time getting your code to work.

Looks like you are on the right track. Any other questions?

econjack, I have read on the arduino website about scope, but you are definitely right, i am still missing something as many of the codes I write have a scope error. Would you mind trying to clarify, perhaps specifically for the code i posted and in general what the scope issues are. Thank you very much.

austincb90:
econjack, I have read on the arduino website about scope, but you are definitely right, i am still missing something as many of the codes I write have a scope error. Would you mind trying to clarify, perhaps specifically for the code i posted and in general what the scope issues are. Thank you very much.

You declare minimum and maximum in setup() and then try to use them in loop(). If you want to be able to use them in both functions, then declare them to be global.

thanks jackwp,

I would just test it now but seems i left my usb plug at theschool.
but looking at the code wouldnt there be an issue with a number, 5 for example. this would meet the criteria of being both above 0 and below 1023 so would technically classify as both a max and a min. does this just sort itself out as time runs longer.

I will post the updated code with suggestions from Delta_g in 2 minutes

Thank you

Just as econjack suggested, an error: " ‘minimum’ was not declaared in this scope" appears.
It seems I really dont understand the scope concept.

Here is the updated code with the time delay suggested by Delta_G:

Thank you for all and any input.

``````//sketch designed to find the max and min of values recorded
//by analogRead over an 800 ms time interval.
long interval=800 ; // read values for an interval of 800 milliseconds
long previousMillis = 0;

void setup(){
Serial.begin(9600);
int maximum=0;
int minimum=1023;}

void loop(){

unsigned long currentMillis = millis();
while(currentMillis - previousMillis < interval){

if (sensorValue<minimum){  // determine minimum value
minimum=sensorValue;
}
if (sensorValue>maximum){
maximum= sensorValue;
}
}

Serial.println(minimum);
Serial.print("/t")
Serial.println(maximum);
}
``````

Think of scope as “visibility”…your ability to access a variable.

global scope: a data definition that is made outside of any function. It can be used at any point in the program and lives from program start until program end (usually).

function scope: a data definition that is made within the opening and closing braces of a function. Its scope extends from its point of definition to the closing brace of the function. It cannot be used outside the function.

statement block scope: a data definition that is made within a statement block. Its scope extend from its definition to the end of the statement block.

``````int counter;    // Global scope

void setup()
{
int position;          // function scope...
// ... more code...
counter++;           // this is okay because counter has global scope
}                            // end of function scope...variable position is "dead" at this point.

void loop()
{
for (int j = 0; j< 100; j++) {            // statement block for variable j starts here
// more code...
position = 10;                  // ERROR: position defined in setup() so out of scope here. It died above...
}                                                 // j dies here

j = 26;            // ERROR: j is out of scope because it only exists in the for loop
}
``````

It will sort itself out.
Work out what happens if the next number is 100.

Pete

Awesome thank you for the excellent breakdown econjack!

cool thanks el_supremo