1
2#include <LiquidCrystal.h>
3
4
5LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
6const byte leds[3] = {3, 5, 6};
7const byte potPins[3] = {A2, A3, A4};
8const byte setPin = A0;
9const byte ledT = 2;
10
11
12int potVals[3];
13int highestVal;
14int lowestVal;
15int averageVal;
16int setVal;
17int offTrackUp[3];
18int offTrackDown[3];
19byte intensity[3];
20const byte stroke = 200;
21
22void setup() {
23
24
25 lcd.begin(16, 2);
26 lcd.clear();
27
28
29 for(byte i = 0; i < 3; i++) {
30 pinMode(leds[i], OUTPUT);
31 }
32
33
34 for(byte i = 0; i < 3; i++) {
35 pinMode(potPins[i], INPUT);
36 }
37
38 pinMode(setPin, INPUT);
39 pinMode(ledT, OUTPUT);
40}
41
42void loop() {
43
44
45 highestVal = 0;
46 lowestVal = 1024;
47 for (byte i = 0; i < 3; i++) {
48 potVals[i] = analogRead(potPins[i]);
49 if(potVals[i] > highestVal) highestVal = potVals[i];
50 if(potVals[i] < lowestVal) lowestVal = potVals[i];
51 }
52
53 averageVal = (highestVal + lowestVal) / 2;
54
55
56 setVal = analogRead(setPin);
57 int depth = map(setVal, 0, 1023, 0, stroke);
58
59
60 lcd.setCursor(0,0);
61 lcd.print("Depth: ");
62 lcd.print(depth);
63 delay(200);
64 lcd.clear();
65
66
67 if (averageVal < 0.95*setVal) {
68 HandleUp ();
69 digitalWrite(ledT, HIGH);
70 }
71
72 if (averageVal > 1.05*setVal) {
73 HandleDown ();
74 digitalWrite(ledT, LOW);
75 }
76
77 if (averageVal > 0.95*setVal && averageVal < 1.05*setVal) {
78 HandleSet ();
79 }
80}
81
82void HandleUp () {
83
84 for (byte i = 0; i < 3; i++) {
85
86 offTrackUp[i] = potVals[i] - lowestVal;
87
88
89 if (offTrackUp[i] < 200) {
90 intensity[i] = map (offTrackUp[i], 0, 200, 255, 0);
91 analogWrite(leds[i], intensity[i]);
92 }
93 if (offTrackUp[i] > 200) {
94 digitalWrite(leds[i], LOW);
95 }
96 }
97}
98
99void HandleDown () {
100
101 for (byte i = 0; i < 3; i++) {
102
103 offTrackDown[i] = highestVal - potVals[i];
104
105
106 if (offTrackUp[i] < 200) {
107 intensity[i] = map (offTrackDown[i], 0, 200, 255, 0);
108 analogWrite(leds[i], intensity[i]);
109 }
110 if (offTrackDown[i] > 200) {
111 digitalWrite(leds[i], LOW);
112 }
113 }
114}
115
116void HandleSet () {
117
118 for (byte i = 0; i < 3; i++) {
119
120 digitalWrite(leds[i], potVals[i] > 0.95*setVal && potVals[i] < 1.05*setVal ? LOW : HIGH);
121 }
122}
123