#include #include #define lightSensor A1 #define tempSensor A0 #define RED_PIN 11 #define GREEN_PIN 10 #define BLUE_PIN 9 #define DEBOUNCE_INTERVAL 60 #define AUTOMATIC 0 #define MANUAL 1 #define NR_LED_STATES 3 #define LED_OFF 255 #define LED_HIGH 0 #define LED_MED 80 #define LED_LOW 160 #define LIGHT_LOW_MED 100 #define LIGHT_MED_HIGH 300 #define TEMP_LOW_MED 190 #define TEMP_MED_HIGH 210 #define NR_TEMP_STATES 3 #define COLD 0 #define TEMPERATE 1 #define HOT 2 int brightness = 0; int temperature = TEMPERATE; int red = 0; int green = 0; int blue = 0; bool poweredOn = false; int operationMode = MANUAL; int temperatureRaw; ButtonDebounce powerButton(7, DEBOUNCE_INTERVAL); ButtonDebounce brightnessButton(6, DEBOUNCE_INTERVAL); ButtonDebounce heatButton(5, DEBOUNCE_INTERVAL); SoftwareSerial HM10(2, 3); // RX = 2, TX = 3 char appData; String inData = ""; void setBrightnessByLight() { int Lvalue = analogRead(lightSensor);// read the light if (Lvalue < LIGHT_LOW_MED) { brightness = 2; } else if (Lvalue > LIGHT_MED_HIGH) { brightness = 0; } else { brightness = 1; } } int getBrightnessValue() { switch (brightness) { case 0: return LED_LOW; case 1: return LED_MED; case 2: return LED_HIGH; default: return LED_OFF; } } void setTemperature() { int Tvalue = analogRead(tempSensor);// read the light if (Tvalue < TEMP_LOW_MED) { temperature = HOT; } else if (Tvalue > TEMP_MED_HIGH) { temperature = COLD; } else { temperature = TEMPERATE; } } void calculateLightValues() { int brightnessValue = getBrightnessValue(); red = temperature == COLD ? LED_OFF : brightnessValue; green = temperature == TEMPERATE ? brightnessValue : LED_OFF; blue = temperature == HOT ? LED_OFF : brightnessValue; } void onPowerButtonChange(const int state) { if (state % 2 == 0) { poweredOn = !poweredOn; if (!poweredOn) { analogWrite(RED_PIN, 255); analogWrite(GREEN_PIN, 255); analogWrite(BLUE_PIN, 255); } else { operationMode = AUTOMATIC; } } } void onBrightnessButtonChange(const int state) { if (state % 2 == 0) { operationMode = MANUAL; brightness = (brightness + 1) % 3; } } void onHeatButtonChange(const int state) { if (state % 2 == 0) { operationMode = MANUAL; temperature = (temperature + 1) % 3; } } void setup() { HM10.begin(9600); // set HM10 serial at 9600 baud rate sau 57600 sau 115200 pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); powerButton.setCallback(onPowerButtonChange); brightnessButton.setCallback(onBrightnessButtonChange); heatButton.setCallback(onHeatButtonChange); analogWrite(RED_PIN, 255); analogWrite(GREEN_PIN, 255); analogWrite(BLUE_PIN, 255); pinMode(2, INPUT); pinMode(3, OUTPUT); } void handleBluetoothCommand(char command) { switch (command) { case 'm': operationMode = AUTOMATIC; break; case 'M': operationMode = MANUAL; break; case 'b': operationMode = MANUAL; brightness = (brightness + 1) % 3; break; case 'B': operationMode = MANUAL; brightness = brightness == 0 ? 2 : (brightness - 1); break; case 't': operationMode = MANUAL; temperature = (temperature + 1) % 3; break; case 'T': operationMode = MANUAL; temperature = temperature == 0 ? 2 : (temperature - 1); break; case 'p': case 'P': poweredOn = !poweredOn; if (!poweredOn) { analogWrite(RED_PIN, 255); analogWrite(GREEN_PIN, 255); analogWrite(BLUE_PIN, 255); } else { operationMode = AUTOMATIC; } break; default:; } // if ( inData == "r") { // analogWrite(RED_PIN, 255); // switch OFF r LED // } // // if ( inData == "R") { // analogWrite(RED_PIN, 0); // switch ON r LED // } // // if ( inData == "g") { // analogWrite(GREEN_PIN, 255); // switch OFF g LED // } // // if ( inData == "G") { // analogWrite(GREEN_PIN, 0); // switch ON g LED // } // // if ( inData == "b") { // analogWrite(BLUE_PIN, 255); // switch OFF b LED // } // // if ( inData == "B") { // analogWrite(BLUE_PIN, 0); // switch ON b LED // } } void loop() { powerButton.update(); brightnessButton.update(); heatButton.update(); HM10.listen(); // listen the HM10 port while (HM10.available() > 0) { // if HM10 sends something then read appData = HM10.read(); inData = String(appData); // save the data in string format Serial.write(appData); } handleBluetoothCommand(inData[0]); if (!poweredOn) { return; } if (operationMode == AUTOMATIC) { setBrightnessByLight(); setTemperature(); } calculateLightValues(); analogWrite(RED_PIN, red); analogWrite(GREEN_PIN, green); analogWrite(BLUE_PIN, blue); }