Components and supplies
Arduino Nano 33 BLE Sense
Tools and machines
Scissors, Free Fall
Project description
Code
Arduino sketch
arduino
Classify object without using proximity, using illumination from the Arduino onboard LEDs
1/* 2 3 k-NN color classification 4 ------------------------- 5 6 This sketch classifies objects using a color sensor. 7 8 First you 'teach' the Arduino by putting an example of each object close to the color sensor. 9 After this the Arduino will guess the name of objects it is shown based on how similar 10 the color is to the examples it has seen. 11 12 This example uses a simple case of k-Nearest Neighbour (k-NN) algorithm where k=1. 13 14 HARDWARE: Arduino Nano BLE Sense 15 16 USAGE: Follow prompts in serial console. Move object close to the board to sample its color, then move it away. 17 18 Works best in a well lit area with objects of different colors. 19 20 NOTE: Make sure Serial Monitor's line ending setting is configured for "Newline" or "Both NL & CR". 21 22 23 This example code is in the public domain. 24 25*/ 26 27#include <Arduino_KNN.h> 28#include <Arduino_APDS9960.h> 29 30const int INPUTS = 3; // Classifier input is color sensor data; red, green and blue levels 31const int CLASSES = 3; // Number of objects we will classify (e.g. Apple, Banana, Orange) 32const int EXAMPLES_PER_CLASS = 1; // Number of times user needs to show examples for each object 33 34// K=1 means the classifier looks for the single closest color example it's seen previously 35const int K = 1; 36 37// Create a new KNNClassifier 38KNNClassifier myKNN(INPUTS); 39 40// Names for each class (object type) 41String label[CLASSES]; 42 43// Array to store data to pass to the KNN library 44float color[INPUTS]; 45 46// Threshold for color brightness 47const float THRESHOLD = 0.98; 48int initialAmbient = 0; 49 50void setup() { 51 52 Serial.begin(9600); 53 while (!Serial); 54 55 // Pins for the built-in RGB LEDs on the Arduino Nano 33 BLE Sense 56 pinMode(LEDR, OUTPUT); 57 pinMode(LEDG, OUTPUT); 58 pinMode(LEDB, OUTPUT); 59 60 if (!APDS.begin()) { 61 Serial.println("Failled to initialized APDS!"); 62 while (1); 63 } 64 65 initialAmbient = readAmbient(); 66 67 Serial.println("Arduino k-NN color classifier"); 68 69 // Ask user for the name of each object 70 for (int currentClass = 0; currentClass < CLASSES; currentClass++) { 71 72 Serial.println("Enter an object name:"); 73 label[currentClass] = readName(); 74 75 // Ask user to show examples of each object 76 for (int currentExample = 0; currentExample < EXAMPLES_PER_CLASS; currentExample++) { 77 78 Serial.print("Show me an example "); 79 Serial.println(label[currentClass]); 80 81 // Wait for an object then read its color 82 readColor(color); 83 84 // Add example color to the k-NN model 85 myKNN.addExample(color, currentClass); 86 87 } 88 } 89} 90 91 92void loop() { 93 94 int classification; 95 96 // Wait for the object to move away again 97 while (!APDS.proximityAvailable() || APDS.readProximity() == 0) {} 98 99 Serial.println("Let me guess your object"); 100 101 // Wait for an object then read its color 102 readColor(color); 103 104 // Classify the object 105 classification = myKNN.classify(color, K); 106 107 // Print the classification 108 Serial.println(label[classification]); 109 Serial.println(); 110 111} 112 113int readAmbient() { 114 int red, green, blue, ambient; 115 while (!APDS.colorAvailable()) {}; 116 APDS.readColor(red, green, blue, ambient); 117 return ambient; 118} 119 120void readColor(float color[]) { 121 int red, green, blue, ambient = 0, colorTotal = 0; 122 123 // Wait until shadow of object passing 124 while (readAmbient() > initialAmbient * THRESHOLD) {} 125 126 // Wait until we have a color bright enough 127 while (ambient < initialAmbient * THRESHOLD) { 128 129 // Sample if color is available and object is close 130 if (APDS.colorAvailable()) { 131 132 // Read color and proximity 133 APDS.readColor(red, green, blue, ambient); 134 colorTotal = (red + green + blue); 135 } 136 } 137 138 // Normalise the color sample data and put it in the classifier input array 139 color[0] = (float)red / colorTotal; 140 color[1] = (float)green / colorTotal; 141 color[2] = (float)blue / colorTotal; 142 143 // Print the red, green and blue percentage values 144 Serial.print(color[0]); 145 Serial.print(","); 146 Serial.print(color[1]); 147 Serial.print(","); 148 Serial.println(color[2]); 149} 150 151 152// reads a name from the Serial Monitor 153String readName() { 154 String line; 155 156 while (1) { 157 if (Serial.available()) { 158 char c = Serial.read(); 159 160 if (c == '\r') { 161 // ignore 162 continue; 163 } else if (c == '\n') { 164 break; 165 } 166 167 line += c; 168 } 169 } 170 171 return line; 172}
Comments
Only logged in users can leave comments
8bitkick
0 Followers
•0 Projects
Table of contents
Intro
8
0