// https://www.arduino.cc/en/Reference/ArduinoAPDS9960 #include // RGB light sensor of Arduino Nano 33 BLE sense #include // connection to phyphox on smartphone char board_name[] = "RGB"; void setup() { // initialize digital pin LED_BUILTIN as an output. pinMode(LED_BUILTIN, OUTPUT); Serial.begin(9600); // while (!Serial); // blocks the program untill opening of the Serial Monitor. if (!APDS.begin()) { Serial.println("Failed to start ADPS9960 gesture sensor!"); digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH voltage level) while (1); } /* Minimum interval between two connection events allowed for a connection. It shall be less than or equal to maxConnectionInterval. This value, in units of 1.25ms, is included in the range [0x0006 : 0x0C80]. */ PhyphoxBLE::minConInterval = 24; //6 = 7.5ms /* Maximum interval between two connection events allowed for a connection. It shall be greater than or equal to minConnectionInterval. This value is in unit of 1.25ms and is in the range [0x0006 : 0x0C80]. */ PhyphoxBLE::maxConInterval = 24; //6 = 7.5ms /* Number of connection events the slave can drop if it has nothing to communicate to the master. This value shall be in the range [0x0000 : 0x01F3]. */ PhyphoxBLE::slaveLatency = 0; // /* Link supervision timeout for the connection. Time after which the connection is considered lost if the device didn't receive a packet from its peer. This value is in the range [0x000A : 0x0C80] and is in unit of 10 ms. */ PhyphoxBLE::timeout = 50; //10 = 100ms PhyphoxBLE::start(board_name); //Start the BLE server // the name of the board can be changed at the beginning of the program Serial.println(" R / G / B / a / color"); } // end setup() void loop() { // put your main code here, to run repeatedly: int r, g, b, a; float R, G, B; // check if a color reading is available float number; // case of color if (APDS.colorAvailable()) { // read the color APDS.readColor(r, g, b, a); if (a>100) { // && a<4000 R = float(r)/a; G = float(g)/a; B = float(b)/a; Serial.print(R, 3); Serial.print(" / "); Serial.print(G, 3); Serial.print(" / "); Serial.print(B, 3); Serial.print(" / "); Serial.print(a); Serial.print(" / "); if ( R<1.500 && R>0.840 && G<0.150 && G>0.00 && B<0.170 && B>0.00) { Serial.print("red"); number = 1.0; } else if ( R<0.800 && R>0.740 && G<0.180 && G>0.100 && B<0.180 && B>0.120) { Serial.print("orange"); number = 2.0; } else if ( R<0.590 && R>0.540 && G<0.370 && G>0.320 && B<0.170 && B>0.00) { Serial.print("yellow"); number = 3.0; } else if ( R<0.190 && R>0.100 && G<0.700 && G>0.550 && B<0.430 && B>0.300) { Serial.print("green"); number = 4.0; } else if ( R<0.300 && R>0.240 && G<0.400 && G>0.300 && B<0.600 && B>0.380) { Serial.print("blue"); number = 5.0; } else if ( R<0.500 && R>0.450 && G<0.350 && G>0.270 && B<0.280 && B>0.220) { Serial.print("white"); number = 6.0; } else { Serial.print("????"); number = 0.0; } Serial.println(); PhyphoxBLE::write(R, G, B, number); digitalWrite(LED_BUILTIN, HIGH); delay(300); digitalWrite(LED_BUILTIN, LOW); } // end if a } // end if color available delay(700); } // end loop()