const int SensorPin = 32; // assign SensorPin to pin 32 int Value = 0; // sets initial value of variable "Value" to zero float Offset = 1.35; // sets value of "Offset" to 1.35 unsigned long previousPrintTime = 0; // declare variable "previousPrintTime" const unsigned long printInterval = 1000; // 1000 milliseconds or 1 second bool print = false; // set to false until printing is needed unsigned long startTime = 0; // declare variable "startTime" unsigned long elapsedTime = 0; // declare variable "elapsedTime" int pHValueCount = 0; // declare variable "pHValueCount" float totalpHValue = 0.0; // declare variable "totalpHValue" float overallMinValue = 1000; // declare variable "overallMinValue" float overallMaxValue = -1000; // declare variable "overallMaxValue" void setup() { Serial.begin(115200); // serial communication with baud rate of 115200 bits per sec pinMode(SensorPin,INPUT); // sets sensor pin as input delay(1000); // delay code for 1 sec } void loop() { Value = analogRead(SensorPin); // read analog voltage from sensor pin and set value to variable "Value" delay(1000); // delay code for 1 sec if (Serial.available() > 0){ // Lecture 18: if data input into serial port char message = Serial.read(); // read what is input and store in variable "message" if (message == 'p'){ // if message input into serial port is "p" print = true; // boolean variable set to true startTime = millis(); // start the timer pHValueCount = 0; // reset pH value count to zero every time p is entered totalpHValue = 0.0; // reset total pH to zero every time p is entered overallMinValue = 1000; // reset min to 1000 overallMaxValue = -1000; // reset max to -1000 } if (message == 's'){ // if message input into serial port is "s" float averagedpHValue = totalpHValue / pHValueCount; // avg all the pH values Serial.println("Summary"); Serial.print("Overall average pH value: "); Serial.println(averagedpHValue, 2); // print avg pH value to 2 decimal places Serial.print("Overall minimum pH value: "); Serial.println(overallMinValue, 2); // print min pH to 2 decimal places Serial.print("Overall maximum pH value: "); Serial.println(overallMaxValue, 2); // print max pH to 2 decimal places } } if (print){ // if print is true (which it is set to) unsigned long currentMillis = millis(); // returns time passed since code started if (currentMillis - previousPrintTime >= printInterval){ // if difference between total time and prev time is >= 1000 float phValue=(float)Value*3.3/4096/6; //convert the analog into millivolt phValue=3.5*phValue+Offset; //convert the millivolt into pH value totalpHValue += phValue; // adds pH value to totalpHvalue to sum all the values pHValueCount++; // add 1 to pHvaluecount which sums number of data points taken if (phValue < overallMinValue){ // if the value is less than 1000, it becomes new min overallMinValue = phValue; // minimum value is updated } if (phValue > overallMaxValue){ // if value is more than -1000, it becomes new max overallMaxValue = phValue; // max value updated } Serial.print(" pH:"); Serial.print(phValue,2); // print pH value to 2 decimal points Serial.println(" "); // add space (make it easier to read) previousPrintTime = currentMillis; // set previous print time to current millis } elapsedTime = currentMillis - startTime; // time since values started being recorded } }