]> git.piffa.net Git - arduino/blob - books/beginning_c_code/Chapter08/Listing8_2/Listing8_2.ino
first commit
[arduino] / books / beginning_c_code / Chapter08 / Listing8_2 / Listing8_2.ino
1 /*
2   Purpose: find the minimum and maximum values of an array of 
3     data values
4   
5   Dr. Purdum, August 13, 2012
6  */
7 #include <stdio.h>
8 #define READINGSPERDAY 24
9 #define VERYHIGHTEMPERATURE 200
10 #define VERYLOWTEMPERATURE -200
11
12 int todaysReadings[] = {62, 64, 65, 68, 70, 70, 71, 72, 74, 75, 76, 78,
13                79, 79, 78, 73, 70, 70, 69, 68, 64, 63, 61, 59};
14 void setup() {                
15   // So we can communicate with the PC
16   Serial.begin(9600);   
17 }
18
19 void loop() {
20   int lowTemp;
21   int hiTemp;
22   int retVal;
23
24   Serial.println("=== Before function call:");
25   Serial.print("The lvalue for lowTemp is: ");  
26   Serial.print((long) &lowTemp, DEC);
27   Serial.print(" and the rvalue is ");
28   Serial.println((long) lowTemp, DEC);
29   Serial.print("The lvalue for hiTemp is: ");  
30   Serial.print((long) &hiTemp, DEC);
31   Serial.print(" and the rvalue is ");
32   Serial.println((long) hiTemp, DEC);
33   
34   retVal = CalculateMinMax(todaysReadings, &lowTemp, &hiTemp);
35   Serial.println("=== After the function call:");
36   Serial.print("The lvalue for lowTemp is: ");  
37   Serial.print((long) &lowTemp, DEC);
38   Serial.print(" and the rvalue is ");
39   Serial.println((long) lowTemp, DEC);
40   Serial.print("The lvalue for hiTemp is: ");  
41   Serial.print((long) &hiTemp, DEC);
42   Serial.print(" and the rvalue is ");
43   Serial.println((long) hiTemp, DEC);
44   Serial.println("\n");
45
46   Serial.flush();  // Make sure all the data is sent...
47   exit(0);
48
49 }/*****
50         Purpose: Get the daily temperature reading (READINGSPERDAY) and 
51                 set the minimum and maximum temperatures for the day.
52         Parameter list:
53                int temps[]                   the array of temperatures
54                 int *minTemp            pointer to the minimum temperature value
55                 int *maxTemp            pointer to the maximum temperature value
56
57         Return value:
58                 int                     the number of readings processed
59 *****/
60 int CalculateMinMax(int temps[], int *minTemp, int *maxTemp)
61 {
62    int j;
63    *minTemp = VERYHIGHTEMPERATURE;      // Make the minimum temperature ridiculously high
64    *maxTemp = VERYLOWTEMPERATURE;  // Make the maximum temperature ridiculously low
65    for (j = 0; j < READINGSPERDAY; j++) {
66       if (temps[j] >= *maxTemp) {
67         *maxTemp = temps[j];
68       }
69       if (temps[j] <= *minTemp) {
70         *minTemp = temps[j];
71       }
72    }
73    return j;
74 }
75 \r