/* HOW TO GET THIS TO WORK: 1. Put this code in a own tab.(name does not mather) 2. Write "cal();" in the setup, and "runCal("ANALOG PIN USED","BUTTON PIN");" in the start of the loop. 3. have a button. 4. While Holding the button down get the sensor to read from max to min val, when you unleash the button the max and min valu will be stored, if you press the button again after 8 sec the values will reset and calibrate again. 5. the led on your board(pin13) will light up while calibrating OUTPUT: SMOOTHED VALUE OF THE ANALOG PIN = call for "analogVal();" in your sketch. MINIMUM VALUE = call for "minVal();" in your sketch. MAXIMUM VALUE = call for "maxVal();" in your sketch. */ //============================================================================================= const int numReadings = 10; int readings[numReadings]; // VARIALABLES FOR SMOOTHING int index = 0; // int total = 0; // int average = 0; // int sensorValue = 0; // VARIALBLES FOR CALIBRATION int sensorMin = 1023; // int sensorMax = 0; // long lastCal; // //const int sensorPin = ; //PINs const int ledPin = 13; // //const int button = ; // //========================================================================SETUP void cal() { pinMode(ledPin, OUTPUT); Serial.begin(9600); // initialize all the readings to 0: for (int thisReading = 0; thisReading < numReadings; thisReading++) readings[thisReading] = 0; } //===========================================================================LOOP // SMOOTHING void runCal(int sensorPin, int button) { // Serial.print("RAW:"); // Serial.print(analogRead(sensorPin)); // subtract the last reading: total= total - readings[index]; // read from the sensor: readings[index] = analogRead(sensorPin); // add the reading to the total: total= total + readings[index]; // advance to the next position in the array: index = index + 1; // if we're at the end of the array... if (index >= numReadings) // ...wrap around to the beginning: index = 0; // calculate the average: average = total / numReadings; Serial.print(" AVERAGE:"); Serial.print(average); //=======================================================================CALIBRATING if (digitalRead(button)==HIGH) { if(millis()-lastCal>8000) { sensorMax=0; sensorMin=1023; } digitalWrite(ledPin,HIGH); sensorValue = average; // record the maximum sensor value if (sensorValue > sensorMax) { sensorMax = sensorValue; } // record the minimum sensor value if (sensorValue < sensorMin) { sensorMin = sensorValue; } lastCal = millis(); }else{ digitalWrite(ledPin,LOW); } Serial.print(" LOW:"); Serial.print(sensorMin); Serial.print(" MAX:"); Serial.println(sensorMax); delay(1); } //========================================================================END int analogVal(){ return average; } int minVal(){ return sensorMin; } int maxVal(){ return sensorMax; }