]> git.piffa.net Git - arduino/blob - books/beginning_c_code/Chapter09/Listing9_5/Listing9_5.ino
first commit
[arduino] / books / beginning_c_code / Chapter09 / Listing9_5 / Listing9_5.ino
1 /*
2   Purpose: illustrate how you can use an array of pointers to 
3     functions.
4   Dr. Purdum, 8/22/2012
5 */
6 enum temperatures {TOOCOLD, TOOHOT, JUSTRIGHT};
7 enum temperatures whichAction;
8
9 const int COLD = 235;
10 const int HOT = 260;
11
12 void setup() {
13   Serial.begin(9600);         // Serial link to PC
14   randomSeed(analogRead(0));  // Seed random number generator
15 }
16
17 void loop() {
18   static void (*funcPtr[])() = {TurnUpTemp, TurnDownTemp, PourCandy};
19   static int iterations = 0;
20   int temp;
21  
22   temp = ReadVatTemp();
23   whichAction = (enum temperatures) WhichOperation(temp);
24   (*funcPtr[whichAction])();
25   if (iterations++ > 10) {
26     Serial.println("===================");
27     Serial.flush();
28     exit(0);
29   }
30 }
31
32 /*****
33   Purpose: decide whether to turn up heat, turn down heat, or if
34     vat is ready. Pourable candy is between 235 and 260.
35   Parameter list:
36     int temp      the current vat temperature
37   Return value:
38     int           0 = temp too cold, 1 = temp too high, 2 = just right
39 *****/
40
41 int WhichOperation(int temp)
42 {
43   Serial.print("temp is ");
44   Serial.print(temp);
45   if (temp < COLD) {
46     return TOOCOLD;
47   } else {
48     if (temp > HOT) {
49       return TOOHOT;
50     } else 
51       return JUSTRIGHT;
52   }
53 }
54
55 /*****
56   Purpose: simulate reading a vat's temperature. Values are 
57     constrained between 100 and 325 degrees
58   Parameter list:
59     void
60     
61   Return value:
62     int           the temperature
63 *****/
64 int ReadVatTemp()
65 {
66   return random(100, 325);
67 }
68
69 void TurnUpTemp()
70 {
71   Serial.println(" in TurnUpTemp()");
72 }
73
74 void TurnDownTemp()
75 {
76   Serial.println(" in TurnDownTemp()");
77 }
78
79 void PourCandy()
80 {
81   Serial.println(" in PourCandy()");
82 }
83 \r