-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.cpp
121 lines (107 loc) · 2.75 KB
/
colors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* colors.cpp
* Purple Mountain Majesties
* - Alexander Lea, Connor Nightingale, Eddy Zhang, Jacob Carulli
* 4/8/2024
*
*/
#include "colors.h"
#include <Arduino.h>
#include "pins.h"
#include "utils.h"
/* Function: analogToColor
* Description: Estimates color based on thresholds of ADC
* Parameters:
* - reading: analog reading from color sensor
* Returns: predicted color of ground below sensor
*/
color analogToColor(int reading) {
if (reading > 580)
return yellow;
if (reading > 390 && reading < 415)
return red;
if (reading > 300 && reading < 335)
return blue;
else
return black;
}
/* Function: getColor
* Description: Get reading of color sensor and return predicted color
* Parameters:
* - None
* Returns: predicted color of ground below sensor
*/
color getColor() {
return analogToColor(analogRead(Pins::colorIn));
}
/* Function: getColor
* Description: Get multiple readings of color sensor and return predicted color
* Parameters:
* - None
* Returns: predicted color of ground below sensor
*/
color getColorPrecise() {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += analogRead(Pins::colorIn);
}
int reading = sum / 10;
return analogToColor(reading);
}
/* Function: detectColor
* Description: Get reading of color sensor based on red/blue flashing
* Parameters:
* - None
* Returns: predicted color of ground below sensor
*/
color detectColor() {
float blueValue, redValue;
//blue is high, red is low
digitalWrite(Pins::redSensorLED, LOW);
digitalWrite(Pins::blueSensorLED, HIGH);
delay(50);
//read blue value
blueValue = analogRead(Pins::colorIn);
blueValue = blueValue / 1023 * 5;
//blue is low, red is high
digitalWrite(Pins::blueSensorLED, LOW);
digitalWrite(Pins::redSensorLED, HIGH);
delay(50);
//read red value
redValue = analogRead(Pins::colorIn);
redValue = redValue / 1023 * 5;
// turn both leds off
digitalWrite(Pins::blueSensorLED, LOW);
digitalWrite(Pins::redSensorLED, LOW);
Serial.print("Analog Red: "); Serial.print(redValue);
Serial.print(" Analog Blue: "); Serial.println(blueValue);
if (redValue < 4.2 && blueValue >= 4.1) {
return blue;
} else if (redValue >= 4 && blueValue <= 4.5) {
return red;
} else if (redValue >= 4.2 && blueValue >= 4.1) {
return yellow;
} else {
return black;
}
}
/* Function: colorToString
* Description: Get name of color as String from color enum
* Parameters:
* - toPrint: color as enum
* Returns: name of color as String
*/
String colorToString(color toPrint) {
switch (toPrint) {
case red:
return "red";
case blue:
return "blue";
case yellow:
return "yellow";
case black:
return "black";
case none:
return "none";
}
}