Skip to content
This repository was archived by the owner on Jun 30, 2022. It is now read-only.

Commit b5577d4

Browse files
authored
Added bias + clean up
1 parent d0b8c3f commit b5577d4

File tree

4 files changed

+115
-120
lines changed

4 files changed

+115
-120
lines changed

Color_Perceptron/Color_Perceptron.ino

+49-56
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
/*
22
3-
Perceptron example
3+
Perceptron example
44
5-
Hardware: Arduino Nano BLE Sense
5+
Hardware: Arduino Nano BLE Sense
66
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+
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.
9+
10+
The color sensor works better in a well lit area
911
1012
*/
1113

1214
#include <Arduino_APDS9960.h>
1315
#include <Arduino_Perceptron.h>
1416

15-
const int NUM_INPUTS = 3; // Classifier input is color sensor data; red, green and blue levels
17+
const int NUM_INPUTS = 3; // Red, green and blue
1618
const int CLASSES = 2; // The perceptron only has 2 possible classes
17-
const int EXAMPLES = 30; // Number of samples for each object
18-
const int MAX_EPOCHS = 100; // Maximum training iterations
19+
const int EXAMPLES = 30; // Number of samples for each object
20+
const int NUM_EPOCHS = 100; // Maximum training iterations
1921
const float LEARNING_RATE = 0.001; // Perceptron learning rate
2022
const int THRESHOLD = 5; // Color sensor light threshold
2123

22-
float color[NUM_INPUTS];
24+
float input[NUM_INPUTS];
25+
float weights[NUM_INPUTS+1]; // Weights for each color input + the bias
2326
String label[CLASSES] = {"object A", "object B"};
2427

2528
Perceptron perceptron(NUM_INPUTS, LEARNING_RATE);
@@ -29,29 +32,26 @@ void setup() {
2932
while (!Serial);
3033

3134
if (!APDS.begin()) {
32-
Serial.println("Failled to initialized APDS!");
35+
Serial.println("Failed to initialize APDS color sensor!");
3336
while (1);
3437
}
3538

3639
Serial.println("Arduino perceptron");
3740

38-
// Get color samples for our two objects
39-
// -------------------------------------
40-
for (int output = 0; output < 2; output ++) {
41+
// Get example color data
42+
// ----------------------
43+
for (int thisClass = 0; thisClass <= 1; thisClass++) {
4144

4245
Serial.print("Show me ");
43-
Serial.println(label[output]);
46+
Serial.println(label[thisClass]);
4447

4548
// Wait for the object to move away again
4649
while (!APDS.proximityAvailable() || APDS.readProximity() == 0) {}
4750

51+
// Get n color samples from the object
4852
for (int n = 0; n < EXAMPLES; n++) {
49-
50-
// Sample object color - perceptron input
51-
readColor(color);
52-
53-
// Add example color to the perceptron training set
54-
perceptron.addExample(color, output);
53+
readColor(input); // red, green and blue
54+
perceptron.addExample(input, thisClass); // Add to training set
5555
}
5656
}
5757

@@ -64,23 +64,21 @@ void setup() {
6464
Serial.print("Start weights:");
6565
printWeights();
6666

67-
while (epoch < MAX_EPOCHS && accuracy < 0.99) {
67+
for (int epoch = 0; epoch < NUM_EPOCHS; epoch++) {
6868
accuracy = perceptron.train();
69-
epoch++;
7069
Serial.print("Accuracy: ");
7170
Serial.println(accuracy);
7271
}
7372

7473
Serial.print("End weights:");
7574
printWeights();
7675

77-
7876
Serial.println("Training complete\n");
7977
}
8078

8179

82-
// Classify objects shown
83-
// ----------------------
80+
// Classify objects shown
81+
// ----------------------
8482
void loop() {
8583

8684
int output;
@@ -90,52 +88,47 @@ void loop() {
9088

9189
Serial.println("Let me guess your object");
9290

93-
// Wait for an object then read its color
94-
readColor(color);
91+
readColor(input);
9592

96-
// CLassify the object using the perceptron
97-
output = perceptron.classify(color);
93+
output = perceptron.classify(input);
9894

9995
Serial.print("You showed me ");
10096
Serial.println(label[output]);
10197
}
10298

10399

104-
void printWeights() {
105-
float weights[NUM_INPUTS];
106-
perceptron.getWeights(weights);
107-
108-
Serial.print(weights[0]);
109-
Serial.print(",");
110-
Serial.print(weights[1]);
111-
Serial.print(",");
112-
Serial.println(weights[2]);
113-
}
114-
115100
void readColor(float color[]) {
116-
int red, green, blue, proximity, colorTotal = 0;
117-
118-
// Wait until we have a color bright enough
119-
while (colorTotal < THRESHOLD) {
101+
int red, green, blue;
120102

121-
// Sample if color is available and object is close
103+
// Loop until we have a color sample bright enough
104+
while (red+green+blue < THRESHOLD) {
105+
// Sample if color sensor is available and the object is close
122106
if (APDS.colorAvailable() && APDS.proximityAvailable() && APDS.readProximity() == 0) {
123-
124-
// Read color and proximity
125107
APDS.readColor(red, green, blue);
126-
colorTotal = (red + green + blue);
127108
}
128109
}
129110

130-
// Normalise the color sample data and put it in the classifier input array
131-
color[0] = (float)red / colorTotal;
132-
color[1] = (float)green / colorTotal;
133-
color[2] = (float)blue / colorTotal;
111+
// Put color sample data in input array
112+
color[0] = (float)red;
113+
color[1] = (float)green;
114+
color[2] = (float)blue;
134115

135116
// Print the red, green and blue values
136-
Serial.print(color[0]);
137-
Serial.print(",");
138-
Serial.print(color[1]);
139-
Serial.print(",");
140-
Serial.println(color[2]);
117+
printArray(color,NUM_INPUTS);
118+
}
119+
120+
121+
void printWeights() {
122+
perceptron.getWeights(weights,NUM_INPUTS+1); // input weights plus the bias
123+
printArray(weights,NUM_INPUTS+1);
124+
}
125+
126+
127+
void printArray(float myArray[], int length) {
128+
Serial.print(myArray[0]);
129+
for (int n = 1; n < length; n++) {
130+
Serial.print(",");
131+
Serial.print(myArray[n]);
132+
}
133+
Serial.println();
141134
}
+49-56
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
/*
22
3-
Perceptron example
3+
Perceptron example
44
5-
Hardware: Arduino Nano BLE Sense
5+
Hardware: Arduino Nano BLE Sense
66
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+
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.
9+
10+
The color sensor works better in a well lit area
911
1012
*/
1113

1214
#include <Arduino_APDS9960.h>
1315
#include <Arduino_Perceptron.h>
1416

15-
const int NUM_INPUTS = 3; // Classifier input is color sensor data; red, green and blue levels
17+
const int NUM_INPUTS = 3; // Red, green and blue
1618
const int CLASSES = 2; // The perceptron only has 2 possible classes
17-
const int EXAMPLES = 30; // Number of samples for each object
18-
const int MAX_EPOCHS = 100; // Maximum training iterations
19+
const int EXAMPLES = 30; // Number of samples for each object
20+
const int NUM_EPOCHS = 100; // Maximum training iterations
1921
const float LEARNING_RATE = 0.001; // Perceptron learning rate
2022
const int THRESHOLD = 5; // Color sensor light threshold
2123

22-
float color[NUM_INPUTS];
24+
float input[NUM_INPUTS];
25+
float weights[NUM_INPUTS+1]; // Weights for each color input + the bias
2326
String label[CLASSES] = {"object A", "object B"};
2427

2528
Perceptron perceptron(NUM_INPUTS, LEARNING_RATE);
@@ -29,29 +32,26 @@ void setup() {
2932
while (!Serial);
3033

3134
if (!APDS.begin()) {
32-
Serial.println("Failled to initialized APDS!");
35+
Serial.println("Failed to initialize APDS color sensor!");
3336
while (1);
3437
}
3538

3639
Serial.println("Arduino perceptron");
3740

38-
// Get color samples for our two objects
39-
// -------------------------------------
40-
for (int output = 0; output < 2; output ++) {
41+
// Get example color data
42+
// ----------------------
43+
for (int thisClass = 0; thisClass <= 1; thisClass++) {
4144

4245
Serial.print("Show me ");
43-
Serial.println(label[output]);
46+
Serial.println(label[thisClass]);
4447

4548
// Wait for the object to move away again
4649
while (!APDS.proximityAvailable() || APDS.readProximity() == 0) {}
4750

51+
// Get n color samples from the object
4852
for (int n = 0; n < EXAMPLES; n++) {
49-
50-
// Sample object color - perceptron input
51-
readColor(color);
52-
53-
// Add example color to the perceptron training set
54-
perceptron.addExample(color, output);
53+
readColor(input); // red, green and blue
54+
perceptron.addExample(input, thisClass); // Add to training set
5555
}
5656
}
5757

@@ -64,23 +64,21 @@ void setup() {
6464
Serial.print("Start weights:");
6565
printWeights();
6666

67-
while (epoch < MAX_EPOCHS && accuracy < 0.99) {
67+
for (int epoch = 0; epoch < NUM_EPOCHS; epoch++) {
6868
accuracy = perceptron.train();
69-
epoch++;
7069
Serial.print("Accuracy: ");
7170
Serial.println(accuracy);
7271
}
7372

7473
Serial.print("End weights:");
7574
printWeights();
7675

77-
7876
Serial.println("Training complete\n");
7977
}
8078

8179

82-
// Classify objects shown
83-
// ----------------------
80+
// Classify objects shown
81+
// ----------------------
8482
void loop() {
8583

8684
int output;
@@ -90,52 +88,47 @@ void loop() {
9088

9189
Serial.println("Let me guess your object");
9290

93-
// Wait for an object then read its color
94-
readColor(color);
91+
readColor(input);
9592

96-
// CLassify the object using the perceptron
97-
output = perceptron.classify(color);
93+
output = perceptron.classify(input);
9894

9995
Serial.print("You showed me ");
10096
Serial.println(label[output]);
10197
}
10298

10399

104-
void printWeights() {
105-
float weights[NUM_INPUTS];
106-
perceptron.getWeights(weights);
107-
108-
Serial.print(weights[0]);
109-
Serial.print(",");
110-
Serial.print(weights[1]);
111-
Serial.print(",");
112-
Serial.println(weights[2]);
113-
}
114-
115100
void readColor(float color[]) {
116-
int red, green, blue, proximity, colorTotal = 0;
117-
118-
// Wait until we have a color bright enough
119-
while (colorTotal < THRESHOLD) {
101+
int red, green, blue;
120102

121-
// Sample if color is available and object is close
103+
// Loop until we have a color sample bright enough
104+
while (red+green+blue < THRESHOLD) {
105+
// Sample if color sensor is available and the object is close
122106
if (APDS.colorAvailable() && APDS.proximityAvailable() && APDS.readProximity() == 0) {
123-
124-
// Read color and proximity
125107
APDS.readColor(red, green, blue);
126-
colorTotal = (red + green + blue);
127108
}
128109
}
129110

130-
// Normalise the color sample data and put it in the classifier input array
131-
color[0] = (float)red / colorTotal;
132-
color[1] = (float)green / colorTotal;
133-
color[2] = (float)blue / colorTotal;
111+
// Put color sample data in input array
112+
color[0] = (float)red;
113+
color[1] = (float)green;
114+
color[2] = (float)blue;
134115

135116
// Print the red, green and blue values
136-
Serial.print(color[0]);
137-
Serial.print(",");
138-
Serial.print(color[1]);
139-
Serial.print(",");
140-
Serial.println(color[2]);
117+
printArray(color,NUM_INPUTS);
118+
}
119+
120+
121+
void printWeights() {
122+
perceptron.getWeights(weights,NUM_INPUTS+1); // input weights plus the bias
123+
printArray(weights,NUM_INPUTS+1);
124+
}
125+
126+
127+
void printArray(float myArray[], int length) {
128+
Serial.print(myArray[0]);
129+
for (int n = 1; n < length; n++) {
130+
Serial.print(",");
131+
Serial.print(myArray[n]);
132+
}
133+
Serial.println();
141134
}

0 commit comments

Comments
 (0)