diff --git a/examples/Modulino_Light/Basic/Basic.ino b/examples/Modulino_Light/Basic/Basic.ino new file mode 100644 index 0000000..bf74f10 --- /dev/null +++ b/examples/Modulino_Light/Basic/Basic.ino @@ -0,0 +1,39 @@ +#include "Modulino.h" + +ModulinoLight light; + +int lux; +int ir; + +void setup() { + Serial.begin(9600); + Modulino.begin(); + light.begin(); +} + +void loop() { + light.update(); + ModulinoColor color = light.getColor(); + String colorName = light.getColorApproximate(); + Serial.print("Color near to: "); + Serial.print(colorName); + + int r = (0xFF000000 & color) >> 24; + int g = (0x00FF0000 & color) >> 16; + int b = (0x0000FF00 & color) >> 8; + + lux = light.getAL(); + ir = light.getIR(); + + Serial.print("light data: "); + Serial.print("\tRed:\t"); + Serial.print(r); + Serial.print("\tGreen:\t"); + Serial.print(g); + Serial.print("\tBlue:\t"); + Serial.print(b); + Serial.print("\tLux:\t"); + Serial.print(lux); + Serial.print("\tIR\t"); + Serial.println(ir); +} diff --git a/examples/Modulino_Light/ColorDetection/ColorDetection.ino b/examples/Modulino_Light/ColorDetection/ColorDetection.ino new file mode 100644 index 0000000..9c03ae4 --- /dev/null +++ b/examples/Modulino_Light/ColorDetection/ColorDetection.ino @@ -0,0 +1,151 @@ +#include "Modulino.h" + +ModulinoLight light; + +// before to run this sketch, change the encoders addresses in order to use more than one encoder +// and set the addresses in the constructor as shown below +ModulinoKnob knob_green; +ModulinoKnob knob_red(0x09); +ModulinoKnob knob_blue(0x0A); +ModulinoPixels leds; + + +int brightness = 100; +int red = 0; +int green = 0; +int blue = 0; + + +void setup() { + Serial.begin(9600); + Modulino.begin(); + knob_green.begin(); + knob_red.begin(); + knob_blue.begin(); + + // set knobs initial positions to 0 + knob_red.set(0); + knob_green.set(0); + knob_blue.set(0); + light.begin(); + leds.begin(); +} + +unsigned long start = millis(); +void loop() { + knobPressed(); + readColors(); + updatesColors(); +} + +void readColors() { + if (red > 255) { + red = 255; + knob_red.set(red); + } else if (red < 0) { + red = 0; + knob_red.set(red); + } + + if (green > 255) { + green = 255; + knob_green.set(green); + } else if (green < 0) { + green = 0; + knob_green.set(green); + } + + if (blue > 255) { + blue = 255; + knob_blue.set(blue); + } else if (blue < 0) { + blue = 0; + knob_red.set(blue); + } +} + +void knobPressed() { + red = knob_red.get(); + green = knob_green.get(); + blue = knob_blue.get(); + bool red_click = knob_red.isPressed(); + bool green_click = knob_green.isPressed(); + bool blue_click = knob_blue.isPressed(); + + if (red_click) { + red = 255; + knob_red.set(red); + } + + if (green_click) { + green = 255; + knob_green.set(green); + } + + if (blue_click) { + blue = 255; + knob_blue.set(blue); + } + + if (green_click && blue_click && red_click ) { + red = 0; + knob_red.set(red); + green = 0; + knob_green.set(green); + blue = 0; + knob_blue.set(blue); + } else if (red_click && green_click) { + red = 255; + knob_red.set(red); + green = 255; + knob_green.set(green); + blue = 0; + knob_blue.set(blue); + } else if (red_click && blue_click) { + red = 255; + knob_red.set(red); + green = 0; + knob_green.set(green); + blue = 255; + knob_blue.set(blue); + } else if (green_click && blue_click) { + red = 0; + knob_red.set(red); + green = 255; + knob_green.set(green); + blue = 255; + knob_blue.set(blue); + } +} + +void updatesColors() { + ModulinoColor COLOR(red, green, blue); + for (int l = 0; l < 8; l++) { + leds.set(l, COLOR, brightness); + leds.show(); + } + light.update(); + + if (millis() - start > 1000) { + char buffer [50]; + int n, a = 3; + n = sprintf (buffer, "%03d", red); + + Serial.print("Red:\t"); + Serial.print(buffer); + n = sprintf (buffer, "%03d", green); + Serial.print("\tGreen:\t"); + Serial.print(buffer); + n = sprintf (buffer, "%03d", blue); + Serial.print("\tBlue:\t"); + Serial.print(buffer); + + ModulinoColor color = light.getColor(); + String colorName = light.getColorApproximate(); + Serial.print("\tColor near to:\t"); + Serial.print(colorName); + Serial.println(); + + start = millis(); + } +} diff --git a/src/Modulino.cpp b/src/Modulino.cpp index 8a2b7e0..d00efbc 100644 --- a/src/Modulino.cpp +++ b/src/Modulino.cpp @@ -7,10 +7,13 @@ // Build before other objects to fix the Wire object ModulinoClass Modulino __attribute__ ((init_priority (101))); +ModulinoColor BLACK(0, 0, 0); ModulinoColor RED(255, 0, 0); ModulinoColor BLUE(0, 0, 255); ModulinoColor GREEN(0, 255, 0); +ModulinoColor YELLOW(255, 255, 0); ModulinoColor VIOLET(255, 0, 255); +ModulinoColor CYAN(0, 255, 255); ModulinoColor WHITE(255, 255, 255); #if __has_include("Arduino_LED_Matrix.h") diff --git a/src/Modulino.h b/src/Modulino.h index cfe420b..82ef2bd 100644 --- a/src/Modulino.h +++ b/src/Modulino.h @@ -7,6 +7,8 @@ #include "Arduino_LSM6DSOX.h" #include #include +#include "Arduino_LTR381RGB.h" +#include "Arduino.h" //#include // need to provide a way to change Wire object #ifndef ARDUINO_API_VERSION @@ -289,10 +291,13 @@ class ModulinoKnob : public Module { uint8_t match[2] = { 0x74, 0x76 }; }; +extern ModulinoColor BLACK; extern ModulinoColor RED; extern ModulinoColor BLUE; extern ModulinoColor GREEN; +extern ModulinoColor YELLOW; extern ModulinoColor VIOLET; +extern ModulinoColor CYAN; extern ModulinoColor WHITE; class ModulinoMovement : public Module { @@ -394,7 +399,111 @@ class ModulinoPressure : public Module { }; class ModulinoLight : public Module { +public: + bool begin() { + if (_light == nullptr) { + _light = new LTR381RGBClass(*((TwoWire*)getWire()), 0x53); + } + initialized = _light->begin(); + __increaseI2CPriority(); + return initialized != 0; + } + operator bool() { + return (initialized != 0); + } + bool update() { + if (initialized) { + return _light->readAllSensors(r, g, b, rawlux, lux, ir); + } + return 0; + } + ModulinoColor getColor() { + return ModulinoColor(r, g, b); + } + String getColorApproximate() { + String color = "UNKNOWN"; + float h, s, l; + _light->getHSL(r, g, b, h, s, l); + if (l > 90.0) { + return "WHITE"; + } + if (l < 10.0) { + return "BLACK"; + } + if (s < 10.0) { + if (l < 50.0) { + return "DARK GRAY"; + } else { + return "LIGHT GRAY"; + } + } + + if (h < 0) { + h = 360 + h; + } + if (h < 15 || h >= 345) { + color = "RED"; + } else if (h < 45) { + color = "ORANGE"; + } else if (h < 75) { + color = "YELLOW"; + } else if (h < 105) { + color = "LIME"; + } else if (h < 135) { + color = "GREEN"; + } else if (h < 165) { + color = "SPRING GREEN"; + } else if (h < 195) { + color = "CYAN"; + } else if (h < 225) { + color = "AZURE"; + } else if (h < 255) { + color = "BLUE"; + } else if (h < 285) { + color = "VIOLET"; + } else if (h < 315) { + color = "MAGENTA"; + } else { + color = "ROSE"; + } + + // Adjust color based on lightness + if (l < 20.0) { + color = "VERY DARK " + color; + } else if (l < 40.0) { + color = "DARK " + color; + } else if (l > 80.0) { + color = "VERY LIGHT " + color; + } else if (l > 60.0) { + color = "LIGHT " + color; + } + + // Adjust color based on saturation + if (s < 20.0) { + color = "VERY PALE " + color; + } else if (s < 40.0) { + color = "PALE " + color; + } else if (s > 80.0) { + color = "VERY VIVID " + color; + } else if (s > 60.0) { + color = "VIVID " + color; + } + return color; + } + int getAL() { + return rawlux; + } + int getLux() { + return lux; + } + int getIR() { + return ir; + } +private: + LTR381RGBClass* _light = nullptr; + int r, g, b, rawlux, lux, ir; + int initialized = 0; }; class _distance_api {