1
1
/*
2
2
3
- Perceptron example
3
+ Color_Perceptron.ino
4
4
5
- Hardware: Arduino Nano BLE Sense
5
+ Perceptron color classification example
6
6
7
- Usage: Follow the prompts in the Serial Monitor and show two objects of different colors to the color sensor onboard the Arduino.
8
- The sketch will train a perceptron using these examples, and then classify objects you show it in the future.
7
+ Hardware: Arduino Nano 33 BLE Sense
8
+
9
+ Usage: Follow the prompts in the Serial Monitor and show two objects of different colors to the color sensor onboard the Arduino.
10
+ The sketch will train a perceptron using these examples, and then classify objects you show it in the future.
9
11
10
12
*/
11
13
12
14
#include < Arduino_APDS9960.h>
13
15
#include < Arduino_Perceptron.h>
14
16
15
- const int NUM_INPUTS = 3 ; // Classifier input is color sensor data; red, green and blue levels
16
- const int CLASSES = 2 ; // The perceptron only has 2 possible classes (-1 and 1)
17
- const int EXAMPLES = 50 ; // Number of samples for each object
18
- const int MAX_EPOCHS = 250 ; // Maximum training iterations
19
- const float LEARNING_RATE = 0.001 ; // Perceptron learning rate
20
- const int THRESHOLD = 5 ; // Color sensor light threshold
17
+ const int NUM_INPUTS = 3 ; // Classifier has 3 inputs - red, green and blue levels
18
+ const int CLASSES = 2 ; // The perceptron only has 2 possible classes (-1 and 1)
19
+ const int EXAMPLES = 50 ; // Number of samples for each object
20
+ const int MAX_EPOCHS = 250 ; // Maximum training iterations
21
+ const float LEARNING_RATE = 0.001 ; // Perceptron learning rate
22
+ const int THRESHOLD = 5 ; // Color sensor light threshold
21
23
22
24
float color[NUM_INPUTS];
23
25
String label[CLASSES] = {" object A" , " object B" };
@@ -35,8 +37,8 @@ void setup() {
35
37
36
38
Serial.println (" Arduino perceptron" );
37
39
38
- // Get examples for the two possible outputs, -1 and 1
39
- // ---------------------------------------------------
40
+ // Get color examples for the two objects
41
+ // --------------------------------------
40
42
for (int output = -1 ; output < 2 ; output += 2 ) {
41
43
42
44
Serial.print (" Show me " );
@@ -61,6 +63,7 @@ void setup() {
61
63
int epoch = 0 ;
62
64
float accuracy = 0 ;
63
65
66
+ // Iterate until the perceptron is 99% accurate or we hit max epochs
64
67
while (epoch < MAX_EPOCHS && accuracy < 0.99 ) {
65
68
accuracy = perceptron.train ();
66
69
epoch++;
@@ -74,24 +77,23 @@ void setup() {
74
77
75
78
76
79
void loop () {
77
-
78
80
int output;
79
81
80
82
// Wait for the object to move away again
81
83
while (!APDS.proximityAvailable () || APDS.readProximity () == 0 ) {}
82
-
83
84
Serial.println (" Let me guess your object" );
84
85
85
86
// Wait for an object then read its color
86
87
readColor (color);
87
-
88
88
output = perceptron.classify (color);
89
89
90
90
Serial.print (" You showed me " );
91
91
printLabel (output);
92
92
}
93
93
94
+
94
95
void printLabel (int output) {
96
+ // Perceptron output can only be -1 or 1
95
97
if (output == -1 ) {
96
98
Serial.println (label[0 ]);
97
99
} else {
0 commit comments